SpringCloud学习(八)消息总线(Spring Cloud Bus)(Finchley版本)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。
准备工作
本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq下载。至于怎么使用 rabbitmq,搜索引擎下。
改造config-client
在pom文件加上起步依赖spring-cloud-starter-bus-amqp,完整的配置文件如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的三个配置,具体如下:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
ConfigClientApplication启动类代码如下:
/**
* @author: Tu9ohost
*/
@SpringBootApplication
@RestController
@EnableEurekaClient
@EnableDiscoveryClient
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}
依次启动eureka-server、confg-cserver,启动两个config-client,端口为:8881、8882。
访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
foo version 23
这时我们去代码仓库将foo的值改为“foo version 24”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现config-client会重新读取配置文件
这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
foo version 24
另外,/actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务。
分析
当git文件更改的时候,通过pc端用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。
最新文章
- php伪静态--隐藏地址实际路径方法
- dvd管理系统
- 003.安装nginx(lnmp)
- [转]How to add new table in NopCommerce
- 3.nodejs权威指南--文件
- Cassandra1.2文档学习(18)—— CQL数据模型(下)
- 深入研究Block捕获外部变量和__block实现原理
- HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题
- 常用 NHibernate.Criterion
- 刚开始学python——数据结构——“自定义队列结构“
- Runtime的使用
- 012 pandas与matplotlib结合制图
- vue 全局组件【原】
- PC上图标显示异常 白色框框处理方案
- Codeforces 835C - Star sky - [二维前缀和]
- .net 读取/保存 文件 到 局域网 服务器
- PHP中奖概率实现
- java取得当前日期增加一天或多天
- Java虚拟机_运行时数据区
- flask 中 session的源码解析
热门文章
- 透彻网络流-wfx-最大流
- Linux+CLion+树莓派远程编译时,Cmake编译出现undefined reference to &#39;dlopen&#39;的解决办法
- vmware中桥接模式和NAT的区别
- 【java设计模式】-02工厂模式
- 一、微服务(Microservices)【翻译】
- C++ #include<;algorithm>;
- 利用Apache shiro SimpleHash 加密字符串
- redis服务及其配置与应用(window 环境下)
- django 快速实现注册(四)
- python桶排序代码