SpringCloud系列——Bus 消息总线
前言
SpringCloud Bus使用轻量级消息代理将分布式系统的节点连接起来。然后可以使用此代理广播状态更改(例如配置更改)或其他管理指令。本文结合RabbitMQ+GitHub的Webhook实现上篇博客Config配置中心(SpringCloud系列——Config 配置中心)的自动刷新
官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/2.1.0.RC3/single/spring-cloud-bus.html
RabbitMQ安装
参考:https://www.cnblogs.com/zhangweizhong/p/5689209.html
大概步骤:
1、先去下载Erlang、RabbitMQ
2、先安装Erlang环境、配置ERLANG_HOME环境变量、安装RabbitMQ
3、激活RabbitMQ 管理插件,登录http://localhost:15672,使用默认账号登录,用户名、密码都是:guest
代码编写
确保RabbitMQ中间件可用之后我们开始对上一篇的项目进行调整,我这里直接使用默认账号连接RabbitMQ,5672是RabbitMQ的监听端口
Config Server
maven引入bus-amqp
<!-- bus-RabbitMQ --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
配置文件添加对RabbitMQ的相关配置
#bus-rabbitMQ spring.cloud.bus.trace.enabled=true spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
同时,配置中心担任调用/bus-refresh的任务
#暴露端点 management.endpoints.web.exposure.include=bus-refresh
Config Client
客户端同时也要连接到消息总线
maven引jar
<!-- bus-RabbitMQ --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
配置文件添加
#bus-rabbitMQ spring.cloud.bus.trace.enabled=true spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
效果演示
启动所有项目
Config Server访问 http://localhost:1112/myspringboot-dev.properties/
Config Server访问 http://localhost:10087/index
前往GitHub修改 huanzi.qch.config.server.username: 张三2
修改完之后回去刷新Config Server跟Config Client,server能实时更新获取最新数据,client不能实时获取最新数据,读的是缓存
Config Server post调用 http://localhost:1112/actuator/bus-refresh
打印空?没关系,刷新Config Client的http://localhost:10087/index,已经获取到了最新数据
WebHook
GitHub的webhook允许在某些事件发生时通知外部服务。当指定的事件发生时,我们将向您提供的每个url发送POST请求。
登录GitHub,前往创建一个WebHook
需要能被外网访问,所以这里演示不了...
这样,在每次push之后都会触发webhook事件,从而实现Config配置中心的自动刷新
总结
更多springcloud bus配置请查阅官方文档
代码开源
代码已经开源、托管到我的GitHub、码云:
版权声明
捐献、打赏

支付宝

微信
交流群

