Spring boot2X负载均衡和反向代理实现过程解析
这篇文章主要介绍了Springboot2X负载均衡和反向代理实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
zuul是netflix开源的一个APIGateway服务器
所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序。
作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。
实现反向代理
1.服务注册发现中心Consul
启动
consulagent-dev
2.服务端
provider和provider1
springboot版本2.2.1.RELEASE
(1)添加依赖
1.8 Greenwich.SR3 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
(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)测试方法
packagecom.xyz.provider.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassdemoController{ @RequestMapping("/hello") publicStringHello(){ return"hello,provider"; } }
(4)启动类
packagecom.xyz.provider; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication publicclassProviderApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(ProviderApplication.class,args); } }
provide1的
server.port=8011
测试方法
packagecom.xyz.provider1.controller; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; @RestController publicclassdemoController{ @RequestMapping("/hello") publicStringHello(){ return"hello,anotherprovider"; } }
3.网关
- zuul
- Springboot版本2.1.8.RELEASE
上面用的Springboot版本为2.2.1.RELEASE
但是启动时遇到了报错,因此改成了这个版本
(1)添加依赖
1.8 Greenwich.SR2 org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
(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)启动类
packagecom.xyz.zuul; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.cloud.client.discovery.EnableDiscoveryClient; importorg.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication publicclassZuulApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(ZuulApplication.class,args); } }
启动Consul
启动provider、provider1
启动zuul
访问http://127.0.0.1:8090/api/hello
结果输出:
hello,provider和hello,anotherprovider
结果交替出现的,负载均衡器采用的是轮询的方式
示例 https://gitee.com/babybeibeili/zuul_consul.git
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。