业余草 SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

Spring Cloud Bus 将分布式的节点用轻量的消息代理链接起来。它能够用于广播配置文件的更改或者服务之间的通信,也能够用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。html

1、准备工做

本文仍是基于上一篇文章来实现。按照官方文档,咱们只须要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说咱们须要装rabbitMq,点击rabbitmq下载。至于怎么使用 rabbitmq,搜索引擎下。git

2、改造config-client

在pom文件加上起步依赖spring-cloud-starter-bus-amqp,完整的配置文件以下:github

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-config</artifactId>
 5         </dependency>
 6  
 7         <dependency>
 8             <groupId>org.springframework.boot</groupId>
 9             <artifactId>spring-boot-starter-web</artifactId>
10         </dependency>
11  
12         <dependency>
13             <groupId>org.springframework.cloud</groupId>
14             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
15         </dependency>
16  
17         <dependency>
18             <groupId>org.springframework.cloud</groupId>
19             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
20         </dependency>
21  
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-actuator</artifactId>
25         </dependency>

在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并须要加上spring.cloud.bus的三个配置,具体以下:web

1  
2 spring.rabbitmq.host=localhost
3 spring.rabbitmq.port=5672
4 spring.rabbitmq.username=guest
5 spring.rabbitmq.password=guest
6  
7 spring.cloud.bus.enabled=true
8 spring.cloud.bus.trace.enabled=true
9 management.endpoints.web.exposure.include=bus-refresh

ConfigClientApplication启动类代码以下:spring

 1 @SpringBootApplication
 2 @EnableEurekaClient
 3 @EnableDiscoveryClient
 4 @RestController
 5 @RefreshScope
 6 public class ConfigClientApplication {
 7  
 8     /**
 9      * http://localhost:8881/actuator/bus-refresh
10      */
11  
12     public static void main(String[] args) {
13         SpringApplication.run(ConfigClientApplication.class, args);
14     }
15  
16     @Value("${foo}")
17     String foo;
18  
19     @RequestMapping(value = "/hi")
20     public String hi(){
21         return foo;
22     }
23 }

依次启动eureka-server、confg-cserver,启动两个config-client,端口为:888一、8882。浏览器

访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:微信

foo version 3架构

这时咱们去代码仓库将foo的值改成“foo version 4”,即改变配置文件foo的值。若是是传统的作法,须要重启服务,才能达到配置文件的更新。此时,咱们只须要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现config-client会从新读取配置文件app

Paste_Image.png

从新读取配置文件:分布式

Paste_Image.png

这时咱们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:

foo version 4

另外,/actuator/bus-refresh接口能够指定服务,即便用”destination”参数,好比 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的全部服务。

3、分析

此时的架构图: 
Paste_Image.png

当git文件更改的时候,经过pc端用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其余服务传递,从而使整个微服务集群都达到更新配置文件。

本文源码下载:

https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter8

5、参考资料

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

业余草微信公众号

感谢您的关注!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群进群密码:xttblog,想加微信群的朋友,能够微信搜索:xmtxtt,备注:“xttblog”,添加助理微信拉你进群。备注错误不会赞成好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合做可添加助理微信进行沟通!

相关文章
相关标签/搜索