概述
分布式自动刷新配置功能
Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新
Bus支持两种消息代理:RabbitMQ和Kafka
Spring Cloud Bus 能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道
RabbitMQ环境配置
1、安装Erlang
2、安装RabbitMQ
3、启动管理功能
4、测试是否安装成功
启动服务,访问http://localhost:15672
默认账号:guest
默认密码:guest
动态刷新全局广播
配置中心服务端添加消息总线支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: git@github.com:666liuyuhe/spring-cloud-config.git # Github上的仓库地址
# 搜索目录
search-paths:
- spring-cloud-config
label: master # 读取分支
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7003/eureka
# rabbitMQ相关配置,暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
客户端添加消息总线支持
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config # 配置文件名称
profile: dev # 后缀 config-dev.yml
uri: http://localhost:3344 # 配置中心地址
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
eureka:
client:
service-url:
defaultZone: http://localhost:7003/eureka
# 暴露监控端口
management:
endpoints:
web:
exposure:
include: "*"
测试
修改GitHub上的配置文件
向配置中心3344发一次POST请求
一次修改,广播通知,处处生效
动态刷新定点通知
不想全部通知,只想定点通知
测试
修改GitHub上的配置文件
发送POST请求给配置中心,实现定点通知
实现了定点通知客户端3355