Spring Cloud Bus (消息总线)将分布式系统的节点与轻量级消息代理连接起来。可用于广播状态更改(例如配置热更新)或其他管理指令。消息中间件目前只支持RabbitMQ和Kafka。可与Spring Cloud Stream结合使用。
基于Config与RabbitMQ实现。
一、引入依赖
在配置中心以及其他所有相关服务加入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
二、修改配置文件
1、配置中心
server:
port: 3344
spring:
application:
name: config-center-service
cloud:
config:
label: main # 分支
server:
git:
uri: https://github.com/xxx/spring-cloud-config.git # github仓库路径
username: xxx # github用户名
password: xxx # github密码
search-paths:
- spring-cloud-config # 项目名
rabbitmq:
host: 192.168.10.128
port: 5672
username: admin
password: admin
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 暴露bus刷新的监控端点
management:
endpoints:
web:
exposure:
include: "bus-refresh"
2、其他服务
spring:
cloud:
config:
# 找到分支为main的配置文件config-dev.yml
label: main # 分支
name: config # 配置文件名称
profile: dev # 环境
uri: http://localhost:3344 # 配置中心地址
rabbitmq:
host: 192.168.10.128
port: 5672
username: admin
password: admin
# 暴露监控端口
management:
endpoints:
web:
exposure:
include: "*"
三、刷新配置中心服务
1、刷新全部
curl -X POST http://localhost:3344/actuator/bus-refresh
2、刷新某一个
curl -X POST http://localhost:3344/actuator/bus-refresh/payment-service:8001
后面跟服务名与端口号。
3、刷新某一个集群
curl -X POST http://localhost:3344/actuator/bus-refresh/payment-service:**
注:用到了配置文件内的属性的类上要加上热刷新注解:
@RefreshScope