【Spring Cloud学习笔记】08:消息总线(Spring Cloud Bus)
教程:http://blog.csdn.net/forezp/article/details/70148235
Spring cloud bus通过轻量消息代理连接各个分布的节点。本例使用 RabbitMq,使用前需要先弄好 RabbitMq。
一、改造 Config Client 模块
- 修改 learn-sc-config-client 的 pom.xml 添加 spring-cloud-starter-bus-amqp 依赖
- 修改 learn-sc-config-client 的代码,给需要刷新参数值的类加上 @RefreshScope 注解,例:ValueController 类。
- 修改learn-sc-config-client 的 application.yaml 文件为,(其中 rabbitmq 默认就是下面的配置,默认配置的时候可以不配置):
spring: application: name: learn-sc-config-client cloud: config: label: master profile: dev rabbitmq: host: localhost port: 5672 username: guest password: guest management: security: enabled: false server: port: 8600
二、测试
- 启动 learn-sc-server-config 注册中心
- 启动 learn-sc-config-server 模块并等待一小会儿(不然,启动3的时候会报错)
- 启动 learn-sc-config-client 模块
- 访问 http://localhost:8600/foo ,页面显示:foo version 1
- 提交代码修改 git 仓库中 foo 参数的值,例:foo=foo version 2
- 访问 http://localhost:8600/foo ,页面显示:foo version 1
- 使用工具发送 POST 请求至 http://localhost:8600/bus/refresh
- 访问 http://localhost:8600/foo ,页面显示:foo version 2
三、代码
Github 地址:https://github.com/nianqinianyi/demo-spring-cloud
对应分支:https://github.com/nianqinianyi/demo-spring-cloud/tree/lsc08