Spring boot2X集成zuul与consul实现负载均衡和反向代理
2024-10-20 07:50:07
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=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)启动类
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.网关
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=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)启动类
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);
} }
启动Consul
启动provider、provider1
启动 zuul
访问http://127.0.0.1:8090/api/hello
结果输出:
hello,provider和hello,another provider
结果交替出现的,负载均衡器采用的是轮询的方式
示例 https://gitee.com/babybeibeili/zuul_consul.git
最新文章
- .NET MVC Razor模板预编译(二)
- Docker Container 配置独立IP
- javascript 中的 bind (编辑中。。。。)
- APACHE 在windows下的配置
- LAMP平台部署及应用
- Enabling CORS in WCF
- linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
- spring 集成mongo配置
- Linux 6.4 partprobe出现warning问题
- 关于移动web教程免费发布
- [usaco18Feb] New Barns
- [转载]Fiddler为所欲为第四篇 直播源抓取与接口分析 [四]
- LOJ #2142. 「SHOI2017」相逢是问候(欧拉函数 + 线段树)
- tedu训练营day01
- Linux 查看内存状态
- Apache和Nginx负载均衡集群及测试分析
- B1048 数字加密
- 21.5.3 Updatable and Insertable Views
- 自定义相机下使用clippingNode注意事项
- 右值引用与转移语义(C++11)