您好,登錄后才能下訂單哦!
Springboot2X中怎么實現負載均衡和反向代理,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
zuul 是netflix開源的一個API Gateway 服務器
所有從設備或網站來的請求都會經過Zuul到達后端的Netflix應用程序。
作為一個邊界性質的應用程序,Zuul提供了動態路由、監控、彈性負載和安全功能。
實現反向代理
1.服務注冊發現中心Consul
啟動
consul agent -dev
2.服務端
provider和provider1
spring boot 版本 2.2.1.RELEASE
(1)添加依賴
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(2)配置
server.port=8010spring.application.name=service-providerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.health-check-path=/actuator/healthspring.cloud.consul.discovery.service-name=${spring.application.name}spring.cloud.consul.discovery.heartbeat.enabled=truemanagement.endpoints.web.exposure.include=*management.endpoint.health.show-details=always
(3)測試方法
package com.xyz.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class demoController { @RequestMapping("/hello") public String Hello(){ return "hello,provider"; }}
(4)啟動類
package com.xyz.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient@SpringBootApplicationpublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}
provide1的
server.port=8011
測試方法
package com.xyz.provider1.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class demoController { @RequestMapping("/hello") public String Hello(){ return "hello,another provider"; }}
3.網關
zuul Spring boot版本 2.1.8.RELEASE
上面用的Spring boot版本為 2.2.1.RELEASE
但是啟動時遇到了報錯,因此改成了這個版本
(1)添加依賴
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(2)添加配置
server.port=8090spring.application.name=service-zuulspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.service-name=${spring.application.name}spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}management.endpoints.web.exposure.include=*management.endpoint.health.show-details=alwayszuul.routes.api.path=/api/**zuul.routes.api.serviceId=service-provider
(3)啟動類
package com.xyz.zuul;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy@SpringBootApplicationpublic class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); }}
啟動Consul
啟動provider、provider1
啟動 zuul
訪問http://127.0.0.1:8090/api/hello
結果輸出:
hello,provider和hello,another provider
關于Springboot2X中怎么實現負載均衡和反向代理問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。