本文共 1666 字,大约阅读时间需要 5 分钟。
之前使用actuator监控中心完成刷新功能,但是在Config Client服务端需要发送POST请求来手动刷新,如果Config Client有很多的话,那么需要一个一个地发送POST请求,这显然是不现实的做法。
使用消息队列中的Topic, 通过消息实现通知。
目前Spring Cloud Bus消息总线只是实现了对RabbitMQ以及Kafka的支持。 参考《RabbitMQ教程》 https://blog.csdn.net/hellozpc/article/details/81436980在pom文件加上起步依赖spring-cloud-starter-bus-amqp。
org.springframework.cloud spring-cloud-starter-bus-amqp
在bootstrap.yml添加rabbitmq的配置
spring: application: name: config-client cloud: bus: enabled: true trace: enabled: true rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest#开启所有端点management: endpoints: web: exposure: include: "*"
保证Rabbit的服务开启。
RabbitMQ后台管理:http://127.0.0.1:15672
springCloudBus
在Git上修改配置文件的内容进行测试。
然后发送post请求:curl -X POST http://localhost:8096/actuator/bus-refresh
会发现所有config-client的配置都更新了。 更新文件到git服务器,发送post请求/bus/refresh,实例将消息发送到springCloudBus的交换机,由于这个实例的队列页绑定到交换机,所以其他实例也获取到了更新的通知,然后去Config Server获取最新的数据。
在前面实现的架构中,config-client实例应该是一个具体业务相关的服务,但是我们让它负责了发送更新的消息到RabbitMQ。
这其实是违反了微服务架构中的职责单一的原则。 其实这个架构是可以改进的,就是将原有的Config Server不仅仅提供配置查询的服务,而且还要负责更新消息的发送。导入依赖。
org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-bus-amqp
在application.yml添加rabbitmq的配置。
然后config-server服务上发送actuator/bus-refresh
POST请求就可以将配置更新到所有的config-client服务上。