文章目錄
- 1. 簡介
- 2. 代碼實作
-
- 2.1 涉及的子產品及整體步驟
-
- 2.1.1 涉及的子產品
- 2.1.2 整體步驟
- 2.2 源代碼
-
- 2.2.1 Github位址
- 2.2.2 配置資訊位址
- 2.3 eureka-server-singleton
- 2.4 config-server-ha
- 2.5 config-client-bus
-
- 2.5.1 整體實作
- 2.5.2 pom.xml
- 2.5.3 bootstrap.yml
- 2.5.4 application-8775.yml
- 2.6 config-repository
-
- 2.6.1 config-client-bus-test.properties配置檔案
- 3. 驗證
-
- 3.1 建立SpringBoot啟動類
-
- 3.1.1 EurekaServerSingletonApplication
- 3.1.2 ConfigServerHaApplication-8772
- 3.1.3 ConfigServerHaApplication-8773
- 3.1.4 ConfigClientBusApplication-8775
- 3.1.5 ConfigClientBusApplication-8776
- 3.2 啟動
- 3.3檢視eureka服務資訊界面
- 3.4 讀取遠端配置資訊
-
- 3.4.1 檢視指定配置項的值
- 3.4.2 檢視指定配置項修改後的值
- 4. 思考
- 5. 補充
-
- 5.1 資料
1. 簡介
Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.
事件、消息總線,用于在叢集(例如,配置變化事件)中傳播狀态變化,可與Spring Cloud Config聯合實作熱部署。
基于RabbitMQ實作上一篇筆記中的配置手動重新整理,RabbitMQ的安裝請自行搜尋,本片記錄中的RabbitMQ使用的都是預設配置。
2. 代碼實作
2.1 涉及的子產品及整體步驟
2.1.1 涉及的子產品
- eureka-server-singleton:eureka服務釋出注冊中心
- config-server-ha:配置中心服務端,通過指定不同端口啟動兩個執行個體模拟服務端叢集
- config-client-bus:建立的通過HA版服務配置中心通路遠端配置資訊子產品,也可以使用原有子產品
- config-repository:放置于GitHub的配置,
是對應的本次測試的儲存配置資訊的檔案名稱config-client-bus-test.properties
2.1.2 整體步驟
- GitHub建立存放配置資訊的config-repository目錄與配置資訊
,可通過demo中的config-client-bus-test.properties
子產品關聯GitHub上的配置。config-repository
- 實作eureka-server-singleton:eureka服務釋出注冊中心,與前面沒有任何差別
- 實作config-server-ha:關鍵是啟動Spring Cloud Config Server功能,指定配置倉庫的配置資訊
- 實作config-client-bus:從配置倉庫讀取配置資訊,引入spring-boot-starter-actuator,spring-cloud-starter-bus-amqp
- 通過為config-client-bus指定8775、8776端口實作多執行個體啟動
- 修改Github上遠端配置檔案config-client-bus-test.properties,通過8775或者8776重新整理配置,再次通路兩個執行個體讀取的相同配置項的值,觀察修改是否生效
- config-repository中添加
:放置4步驟的配置資訊config-client-bus-test.properties
2.2 源代碼
2.2.1 Github位址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 配置資訊位址
配置資訊在dev-20180827、與master分支都有,但是代碼中使用的是dev-20180827分支的代碼,如果想修改值驗證,記得确認用戶端bootstrap.properties中配置資訊與想要通路的倉庫位址一緻。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository
2.3 eureka-server-singleton
與Spring Cloud 之服務發現與調用-Ribbon#2.3 eureka-server-singleton 沒有任何差別
2.4 config-server-ha
與十:Spring Cloud 之配置中心HA版-config 沒有任何差別
2.5 config-client-bus
2.5.1 整體實作
- pom.xml檔案中引入依賴
、spring-cloud-starter-config
、spring-cloud-starter-netflix-eureka-client
、config-client-bus-test.properties
spring-boot-starter-actuator
- bootstrap.yml中指定Config Server連接配接資訊以及需要通路配置中心的具體配置資訊
- application-8775.yml、application-8776.yml指定目前子產品的配置資訊,通過profile指定不同端口模拟啟動多執行個體
- ConfigClientBusApplication正常Spring Boot啟動類
2.5.2 pom.xml
<?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">
<parent>
<artifactId>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client-bus</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
</project>
2.5.3 bootstrap.yml
下方資訊與配置中心的配置檔案的對應關系見後續驗證部分
spring:
application:
name: config-client-bus
cloud:
config:
label: dev-20180827
profile: test
discovery:
enabled: true
serviceId: config-server-ha
2.5.4 application-8775.yml
application-8776.yml 隻是端口不一樣
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: bus-refresh
server:
port: 8775
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.6 config-repository
儲存配置檔案
2.6.1 config-client-bus-test.properties配置檔案
foo=config-client-bus foo value updated.
3. 驗證
3.1 建立SpringBoot啟動類
簡單建立Spring Boot啟動類即可
3.1.1 EurekaServerSingletonApplication
最簡單的方式添加一個SpringBoot啟動類型的啟動類就行。
3.1.2 ConfigServerHaApplication-8772
3.1.3 ConfigServerHaApplication-8773
參考上一節,修改Active profiles:8773
3.1.4 ConfigClientBusApplication-8775
參考上一節,指定Active profiles:8775。
3.1.5 ConfigClientBusApplication-8776
參考上一節,指定Active profiles:8776。
3.2 啟動
-
EurekaServerSingletonApplication
-
ConfigServerHaApplication-8772
-
ConfigServerHaApplication-8773
-
ConfigClientBusApplication-8775
-
ConfigClientBusApplication-8776
3.3檢視eureka服務資訊界面
3.4 讀取遠端配置資訊
3.4.1 檢視指定配置項的值
- 浏覽器中輸入位址:http://localhost:8775/readFooProp
- 預期:讀取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 檔案中
foo=config-client-bus foo value updated.
- 實際傳回結果:
3.4.2 檢視指定配置項修改後的值
- 本地修改資源檔案
配置項config-client-bus-test.properties
,并push到遠端倉庫,檢視 https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 有沒有成功(這裡可以換成自己的Github測試一下)修改後的值foo=config-client-bus foo value
foo=config-client-bus foo value updated again 2018-09-20.
- 再次通路 http://localhost:8775/readFooProp,傳回值不變,值為:
config-client-bus foo value updated.
- 通路http://localhost:8775/actuator/bus-refresh 手動重新整理配置項,可看到背景重新請求Github位址,拉取配置。注意:這裡需要使用post請求,并且header中的Content-Type值為application/json。可以使用Intellij 的HTTP Client。
- 觀察8775控制台輸出的日子,發現重新拉取了遠端的配置資訊.
- 再次通路 http://localhost:8775/readFooProp,傳回值變成修改後的值
,說明成功讀取到新配置值再次通路 http://localhost:8775/readFooProp,傳回值變成修改後的值config-client-bus foo value
,說明成功讀取到新配置值config-client-bus foo value
- 通路http://localhost:8776/readFooProp,傳回值變成修改後的值
,說明成功讀取到新配置值config-client-bus foo value
- 通過消息總線實作變更配置資訊同步功能生效
4. 思考
- 消息總線是如何接收消息并廣播的
- SpringCloud消息總線還支援哪些消息中間件
- 如果基于消息中線設計一套基于事件驅動的系統架構,需要解決哪些核心關鍵點
5. 補充
5.1 資料
https://springcloud.cc/spring-cloud-bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html