Spring boot2X負(fù)載均衡和反向代理實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring boot2X負(fù)載均衡和反向代理實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
zuul 是netflix開源的一個(gè)API Gateway 服務(wù)器
所有從設(shè)備或網(wǎng)站來的請求都會(huì)經(jīng)過Zuul到達(dá)后端的Netflix應(yīng)用程序。
作為一個(gè)邊界性質(zhì)的應(yīng)用程序,Zuul提供了動(dòng)態(tài)路由、監(jiān)控、彈性負(fù)載和安全功能。
實(shí)現(xiàn)反向代理
1.服務(wù)注冊發(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=8010 spring.application.name=service-provider spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.service-name=${spring.application.name} spring.cloud.consul.discovery.heartbeat.enabled=true management.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; @RestController public 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 @SpringBootApplication public 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; @RestController public 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=8090 spring.application.name=service-zuul spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.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=always zuul.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 @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }
啟動(dòng)Consul
啟動(dòng)provider、provider1
啟動(dòng) zuul
訪問http://127.0.0.1:8090/api/hello
結(jié)果輸出:
hello,provider和hello,another provider
結(jié)果交替出現(xiàn)的,負(fù)載均衡器采用的是輪詢的方式
示例 https://gitee.com/babybeibeili/zuul_consul.git
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot 單文件上傳的實(shí)現(xiàn)步驟
這篇文章主要介紹了springboot實(shí)現(xiàn)單文件上傳的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-02-02一個(gè)JAVA小項(xiàng)目--Web應(yīng)用自動(dòng)生成Word
前段時(shí)間接到一個(gè)Web應(yīng)用自動(dòng)生成Word的需求,現(xiàn)整理了下一些關(guān)鍵步驟拿來分享一下。2014-05-05springBoot 插件工具熱部署 Devtools的步驟詳解
這篇文章主要介紹了springBoot 插件工具 熱部署 Devtools,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java編寫時(shí)間工具類ZTDateTimeUtil的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫時(shí)間工具類ZTDateTimeUtil,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11使用MultipartFile實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了使用MultipartFile實(shí)現(xiàn)文件上傳功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05