第八篇: 消息總線(Spring Cloud Bus)

轉自 http://blog.csdn.net/forezp/article/details/70148235
本文出自方誌朋的博客

spring Cloud Bus 將分佈式的節點和輕量的消息代理連接起來。這可以用於廣播配置文件的更改或者其他的管理工作。一個關鍵的思想就是,消息總線可以爲微服務做監控,也可以作爲應用程序之間相互通訊。本文要講述的是用AMQP實現通知微服務架構的配置文件的更改。

一、準備工作

本文還是基於上一篇文章來實現。按照官方文檔,我們只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;這就是說我們需要裝rabbitMq,點擊rabbitmq下載。至於怎麼使用 rabbitmq,搜索引擎下。


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.forezp</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>config-client</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<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.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>


在配置文件中加入:


spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
# spring.rabbitmq.username=
# spring.rabbitmq.password=


如果rabbitmq有用戶名密碼,輸入即可。

依次啓動eureka-server、confg-cserver,啓動兩個config-client,端口爲:8881、8882。

訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:

foo version 3


這時我們去代碼倉庫將foo的值改爲“foo version 4”,即改變配置文件foo的值。如果是傳統的做法,可以需要重啓服務,才能達到配置文件的更新。此時,我們只需要用post請求:http://localhost:8881/bus/refresh:


[img]http://dl2.iteye.com/upload/attachment/0126/2201/3ac92634-072e-3267-9181-204e7a5a4492.png[/img]


重新讀取配置文件:


[img]http://dl2.iteye.com/upload/attachment/0126/2203/ee9860e7-ef4a-3b0d-8670-db61b573b392.png[/img]

這時我們再訪問http://localhost:8881/hi 或者http://localhost:8882/hi 瀏覽器顯示:


foo version 4


另外,/bus/refresh接口可以指定服務,即使用”destination”參數,比如 “/bus/refresh?destination=customers:**” 即刷新服務名爲customers的所有服務,不管ip。

三、分析

此時的架構圖:

[img]http://dl2.iteye.com/upload/attachment/0126/2199/2326237f-e67d-3f85-9fb2-92f38b9f1ad5.png[/img]


當Git文件更改的時候,通過pc端用post 向端口爲8882的config-client發送請求/bus/refresh/;此時8882端口會發送一個消息,由消息總線向其他服務傳遞,從而使整個微服務集羣都達到更新配置文件。

四、其他

可以用作自定義的Message Broker,只需要spring-cloud-starter-bus-amqp, 然後再配置文件寫上配置即可,同上。

Tracing Bus Events:
需要設置:spring.cloud.bus.trace.enabled=true,如果那樣做的話,那麼Spring Boot TraceRepository(如果存在)將顯示每個服務實例發送的所有事件和所有的ack,比如:(來自官網)


{
"timestamp": "2015-11-26T10:24:44.411+0000",
"info": {
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "stores:8081",
"destination": "*:**"
}
},
{
"timestamp": "2015-11-26T10:24:41.864+0000",
"info": {
"signal": "spring.cloud.bus.sent",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "customers:9000",
"destination": "*:**"
}
},
{
"timestamp": "2015-11-26T10:24:41.862+0000",
"info": {
"signal": "spring.cloud.bus.ack",
"type": "RefreshRemoteApplicationEvent",
"id": "c4d374b7-58ea-4928-a312-31984def293b",
"origin": "customers:9000",
"destination": "*:**"
}
}

本文源碼下載:
[url]
https://github.com/forezp/SpringCloudLearning/tree/master/chapter8
[/url]

五、參考資料

spring_cloud_bus
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章