微服务 SpringCloud Bus消息总线
1. Bus概述
1.1 Bus简介
官网地址:https://docs.spring.io/spring-cloud-bus/docs/current/reference/html/
注意:Bus支持两种消息代理:RabbitMQ和Kafka
1.2 Bus总线基本原理
2. RabbitMQ环境配置
具体配置信息参考:微服务 消息中间件 RabbitMQ
3. Bus动态刷新全局广播
3.1 Bus两种设计思路
- 利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置
- 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置(更加推荐)
3.2 Bus技术选型
图二的架构显然更加合适,图一不适合的原因如下
1.打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
2.破坏了微服务各节点的对等性.
3.有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改.
需要配置ConfigServer,然后配置所有客户端ConfigClient,在去修改Github上配置文件内容。
此时仍然需要手动刷新ConfigServer,但是因为配置有bus所以,ConfigServer会通知所有ConfigClient,也就是一次刷新,处处生效。
git源码地址:https://github.com/zrj-coder/cloudboot3
3.3 配置configServer
引入依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml配置
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
uri: https://github.com/zrj-coder/springcloud-config.git #GitHub上面的git仓库名字
####搜索目录
search-paths:
- springcloud-config
####读取分支
label: master
#rabbitmq相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
3.4 配置configClient1
引入依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml配置
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3.5 配置configClient2
引入依赖
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml配置
server:
port: 3366
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3.6 发送post请求
发送Post请求到ConfigServer:
curl -X POST "http://localhost:3344/actuator/bus-refresh"
3.7 验证结果
就是所有客户端都获取到最新配置。
4. Bus动态刷新定点通知
全局通知是发送ConfigServer的post请求后所有客户端都能获取到最新配置。
定点刷新通知也是发送ConfigServer的post请求,但是只有指定客户端获取到最新配置信息。
公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类指定需要更新配置的服务或实例。
我们这里以刷新运行在3355端口上的config-client为例,只通知ConfigClient3355,不通知ConfigClient3366
curl -X POST “http://localhost:3344/actuator/bus-refresh/config-client:3355”