阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Spring Cloud Bus

120次阅读
没有评论

共计 2913 个字符,预计需要花费 8 分钟才能阅读完成。

1、Spring Cloud Bus 概述

Spring Cloud Bus 主要用于管理和传播分布式项目中的消息,它利用消息中间件的广播机制传播消息。它通过轻量消息代理连接各个分布点;通过分布式的启动器对 Spring Boot 应用进行扩展;用 Amqp 消息代理作为通道来建立应用之间的通信频道。它目前支持 Kafka 和 RabbitMQ。

2、用 Spring Cloud Bus 刷新配置信息

2.1、添加服务器端的依赖和配置,并启用支持

2.1.1、添加依赖

<!--Spring Cloud Config 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--Spring Cloud Bus 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <!--Rabbtimq 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>

2.1.2、添加配置

在配置文件中添加对 Spring Cloud Bus、Bus 跟踪和 Bus 刷新端点的支持

spring.application.name=config-server
server.port=50027
# 配置 git 仓库的地址
spring.cloud.config.server.git.uri=https://github.com/lingfengxeon/spring-config
# git 仓库地址下的相对地址,可以配置多个,用, 分割。spring.cloud.config.server.git.search-paths=config repositories
# git 仓库的账号
username=
# git 仓库的密码
password=
#启用 Bus 支持
spring.cloud.bus.enabled=true
#启用 Bus 的 trace 跟踪支持
spring.cloud.bus.trace.enabled=true
#启用 Bus 的刷新端点
management.endpoints.web.exposure.include=bus-refresh

2.1.3、启用配置支持

在启动类中添加注解 @EnableConfigServer 以支持 Config Server

2.2、添加客户端的依赖和配置,并启用支持

2.2.1、添加依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Spring Cloud Config 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--Spring Cloud Bus 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <!--Rabbtimq 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <!--Actuator 的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

2.2.2、添加配置

在 bootstrap.properties 中添加 Spring Cloud Bus 及跟踪支持

# 启用 Bus 支持
spring.cloud.bus.enabled=true
#启用 Bus 的跟踪支持
spring.cloud.bus.trace.enabled=true

在 application.properties 中添加 Spring Cloud Bus 端点的刷新支持

management.endpoints.web.exposure.include=bus-refresh

2.2.3、添加更新位置

在控制器类上添加注解 @RefreshScope,否则客户端在接收到服务器端的更新消息后不知道更新到什么位置

@RestController @RefreshScope// 自动刷新 public class HelloController {// 获取 app 的版本 @Value("${app.version}") private String version; // 获取端口号 @Value("${server.port}") private String port; // 获取 Server 端参数 message 值 @Value("${message}") private String message; @GetMapping("/hello") public String hello(){String str="version:"+this.version+"port:"+this.port+message; return str; } }

2.3、启动并刷新客户端的配置

2.3.1、启动客户端

访问 RabbitMQ 的管理界面 http://localhost:15672

Spring Cloud Bus

2.3.2、刷新客户端配置信息

步骤:

1、修改配置文件,然后将其发布到 Git 中

app.version=dev
message=Spring Cloud Config
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=50029

2、访问:http://localhost:50028/hello

Spring Cloud Bus

3、使用 POST 方式提交空消息到 http://localhost:50028/actuator/bus-refresh

Spring Cloud Bus

4、访问:http://localhost:50028/hello

Spring Cloud Bus

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计2913字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中