您好,登錄后才能下訂單哦!
Springboot2X中怎么實(shí)現(xiàn)負(fù)載均衡和反向代理,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
zuul 是netflix開(kāi)源的一個(gè)API Gateway 服務(wù)器
所有從設(shè)備或網(wǎng)站來(lái)的請(qǐng)求都會(huì)經(jīng)過(guò)Zuul到達(dá)后端的Netflix應(yīng)用程序。
作為一個(gè)邊界性質(zhì)的應(yīng)用程序,Zuul提供了動(dòng)態(tài)路由、監(jiān)控、彈性負(fù)載和安全功能。
實(shí)現(xiàn)反向代理
1.服務(wù)注冊(cè)發(fā)現(xiàn)中心Consul
啟動(dòng)
consul agent -dev
2.服務(wù)端
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)測(cè)試方法
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)啟動(dòng)類
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
測(cè)試方法
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.網(wǎng)關(guān)
zuul Spring boot版本 2.1.8.RELEASE
上面用的Spring boot版本為 2.2.1.RELEASE
但是啟動(dòng)時(shí)遇到了報(bào)錯(cuò),因此改成了這個(gè)版本
(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)啟動(dòng)類
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); }}
啟動(dòng)Consul
啟動(dòng)provider、provider1
啟動(dòng) zuul
訪問(wèn)http://127.0.0.1:8090/api/hello
結(jié)果輸出:
hello,provider和hello,another provider
關(guān)于Springboot2X中怎么實(shí)現(xiàn)負(fù)載均衡和反向代理問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。