走進Spring Cloud之十一 SpringCloud bus 訊息匯流排重新整理配置(Greenwich版本)
阿新 • • 發佈:2018-11-30
走進Spring Cloud之十一 SpringCloud bus 訊息匯流排重新整理配置(Greenwich版本)
SpringCloud Bus
在微服務中,我們將使用輕量級訊息代理,通過一個共用的訊息主題,讓系統中所有微服務都連上來,主題中的訊息會被所有監聽者消費,所以稱為訊息匯流排。spring cloud bus將分散式的節點用輕量的訊息代理連線起來。它可以用於廣播配置檔案的更改或者服務之間的通訊,也可以用於監控。
改造config-client
pom.xml
修改pom.xml新增spring-cloud-starter-bus-amqp依賴
<?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>scexample</artifactId>
<groupId>com.pubutech</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<packaging>jar</packaging>
<dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</project>
bootstrap.yml
增加rabbitmq的配置支援
spring:
cloud:
config:
name: springcloud-config #對應{application}部分
profile: pro #對應{profile}部分
#uri: http://localhost:8888/ #配置中心的具體地址
label: master #對應git的分支。如果配置中心使用的是本地儲存,則該引數無用
discovery:
service-id: config-server #指定配置中心的service-id,便於擴充套件為高可用配置叢集。
enabled: true #開啟Config服務發現支援
#配置重試機制
retry:
initial-interval: 2000
max-attempts: 2000
max-interval: 2000
multiplier: 1.2
fail-fast: true
bus:
#動態重新整理配置
refresh:
enabled: true
#跟蹤匯流排事件
trace:
enabled: true
rabbitmq:
host: 192.168.199.133
port: 5672
username: test
password: test123
virtual-host: /
eureka:
client:
service-url:
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。預設是http://localhost:8761/eureka ;多個地址可使用 , 分隔。
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
#關閉安全認證
management:
security:
enabled: false
#refresh接入點顯式暴露出來
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh
改造config-server
pom.xml
修改pom.xml新增spring-cloud-starter-bus-amqp依賴
<?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>scexample</artifactId>
<groupId>com.pubutech</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</project>
application.yml
增加rabbitmq的配置支援
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/Jaysong2012/scexample # 配置git倉庫的地址
search-paths: springcloud-config-repo # git倉庫地址下的相對地址,可以配置多個,用,分割。
username: # git倉庫的賬號(私有庫必填)
password: # git倉庫的密碼(私有庫必填)
label: master #配置倉庫的分支
rabbitmq:
host: 192.168.199.133
port: 5672
username: test
password: test123
#關閉安全認證
management:
security:
enabled: false
#refresh接入點顯式暴露出來
endpoints:
web:
exposure:
include: refresh,health,info,bus-refresh
eureka:
client:
service-url:
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。預設是http://localhost:8761/eureka ;多個地址可使用 , 分隔。
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
啟動測試
這裡我們在8889 和88902個埠分別執行2個config-client
訪問 http://localhost:8761/
訪問 http://localhost:8889/writer
修改遠端github倉庫配置
通知更新
curl -v -X POST http://localhost:8889/actuator/bus-refresh