業餘草 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郵箱。商務合做可添加助理微信進行溝通!

相關文章
相關標籤/搜索