场景&意义

在不重启服务的情况下更新配置,但逐一调用服务的api去修改是很不科学的。所以可以利用消息队列发布订阅的模型,让所有为服务来订阅这个事件,当这个事件发生改变了,就可以通知所有微服务去更新它们的内存中的配置信息。

spring-cloud-bus 消息总线就就是在spring-cloud-config-server端发出refresh,触发所有微服务更新配置。

搭建demo项目

  • spring cloud
    • eureka
    • config-bus (集成了config 配置中心 & bus 消息总线)
    • web-api

demo项目结构说明demo项目结构说明

更新配置&加载

  1. commit&push git配置 到git服务器
  2. 调用refresh接口刷新服务器配置
1
2
3
4
5
# springboot 2.x
curl -X POST "192.168.9.97:8868/actuator/bus-refresh"

# springboot 1.x
curl -X GET "192.168.9.97:8868/bus/refresh"

灰度更新

灰度更新是为了在多节点集群中,只更新部分服务的配置用于参数测试,而其他服务依旧使用之前的配置。
spring cloud bus 通过 destination 参数指定需要更新对服务来刷新配置

1
2
3
4
5
# springboot 2.X
curl -X POST "192.168.9.97:8868/actuator/bus-refresh/{destination}"

#springboot 1.X
curl -X GET "192.168.9.97:8868/bus/refresh?destination={ApplicationContext ID}"

destination 参数为 ApplicationContext ID 例如本例中的 api-gateway:8869 或者 api-gateway:**

通过配置 Git 仓库 WebHooks 来自动调用接口

git 配置修改后自动回调。

git 回调配置git 回调配置