Chapter2 訊息匯流排 ConfigClient配置自動重新整理
Chapter2 訊息匯流排ConfigClient配置自動重新整理
Spring Cloud Bus:
Spring Cloud Bus提供了批量重新整理配置的機制,它使用輕量級的訊息代理(例如RabbitMQ、Kafka等)連線分散式系統的節點,這樣就可以通過Spring Cloud Bus廣播配置的變化或者其他的管理指令。使用Spring Cloud Bus後的架構如圖所示。
準備工作:
需要安裝rabbitMQ,安裝rabbitMQ的詳情請參考:https://blog.csdn.net/qq_35098526/article/details/80009424
環境搭建
建立三個模組:
eureka-server eureka服務註冊中心
config-server 配置中心服務端
config-client 配置中心客戶端
1. eureka-server 模組pom檔案配置如下,這裡跟第一章節一樣 ,不多說。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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.harry</groupId>
<artifactId>eureka-server
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>EUREKA 服務註冊中心</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<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>
2. config-server 配置中心服務端pom配置,這裡引入了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"> <modelVersion>4.0.0</modelVersion> <groupId>com.harry</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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> <spring-cloud.version>Finchley.M9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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-test</artifactId> <scope>test</scope> </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> <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>
application.yml 配置
eureka: client: serviceUrl: defaultZone: http://localhost:8889/eureka/ spring: application: name: config-server cloud: config: server: git: uri: https://gitee.com/honghh/spring-cloud-config # 配置git倉庫地址 searchPaths: config-doc # 配置倉庫路徑 # username: # 訪問git倉庫的使用者名稱 # password: # 訪問git倉庫的使用者密碼 # label: #配置倉庫的分支 server: port: 8989
在application.yml可以看到引入了git倉庫地址,如果倉庫是私有的,需要輸入訪問的賬號和密碼,這個地址是我自己測試用的,是公開的,可以不輸入賬號密碼。searchPaths:config-doc# 配置倉庫路徑如果倉庫路徑是根目錄則不需要寫這個,如果是多級目錄,可以寫成A/B/C格式。
2. config-client 配置中心客戶端這裡需要注意pom檔案中引入了spring-cloud-starter-bus-amqp,spring-boot-starter-actuator,spring-retry
<?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.harry</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>2.0.1.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> <spring-cloud.version>Finchley.M9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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-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-actuator</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-test</artifactId> <scope>test</scope> </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> <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>
bootstrap.yml 配置
eureka: client: serviceUrl: defaultZone: http://localhost:8889/eureka/ server: port: 8881 spring: application: name: config-client cloud: config: label: master profile: dev discovery: enabled: true service-id: config-server rabbitmq: host: localhost port: 5672 username: guest password: guest #management: # security: # enabled: false #spring boot1.5.x 版本配置 management: endpoints: web: exposure: include: bus-refresh
這裡寫入rabbitMQ的配置和management的配置,我們可以看到有一段註釋的程式碼,解析看下面注意有坑①。
啟動專案
依次啟動eureka-server、confg-cserver,啟動兩個config-client,埠為:8881
1.訪問http://localhost:8881/hi瀏覽器顯示:
2這時我們去程式碼倉庫將config-client-dev.properties下的foo的值改為“foo version 4”,即改變配置檔案foo的值。如果是傳統的做法,需要重啟服務,才能達到配置檔案的更新。此時,我們只需要傳送post請求:http://localhost:8881/bus/refresh,你會發現config-client會重新讀取配置檔案。可以看到控制檯列印日誌如圖。注意有坑 ①
3. 再次請求http://localhost:8881/hi
到這裡操作就已經完成。接下來我們講一下遇到的問題
注意有坑
① 上面提到請求 http://localhost:8881/bus/refresh但是你在執行的時候遇到報404的錯誤
圖5.1
圖5.2
首先,你要確認是post請求,如圖5.1是get請求,是會報404的錯誤的
但是圖5.2中用的是post請求為什麼還會報錯
這裡就是我所說的坑看專案中的pom檔案我用的是spring boot 2.0.1.RELEASE版本,查資料看到一篇文章 https://ask.csdn.net/questions/684123
詳情如下:
SpringBoot2.0Config客戶端自動重新整理時沒有/bus/refresh端點
Spring Cloud ConfigClient的配置自動重新整理
目前的部落格和視訊都是Spring Boot1.5.x的,安裝rabbitmq,匯入依賴,配置yml,新增@RefreshScope,啟動Config client時日誌都會打印出/bus/refresh這個埠,但是到spring boot2.0就不再列印這個埠,也無法訪問。即使yml配置暴露actuator的所有埠,也就只有/actuator/refresh這個埠(這個埠是隻更新一個的)。
請問Spring boot 2.0如何實現Config Client自動重新整理配置
Spring boot 2.0的改動較大,/bus/refresh全部整合到actuador裡面了,所以之前1.x的management.security.enabled全部失效,不適用於2.0
適用於2.0的配置是這樣的:
management:
endpoints:
web:
exposure:
include: bus-refresh
另外註解
@RefreshScope
需要在配置的頁面加上,就是說附帶@Value的頁面加上此註解
請求重新整理的頁面由原來1.5.x的localhost:8888/bus/refresh
變成:http://localhost:8888/actuator/bus-refresh
注意:config-server和config-client的配置都得加上
management:
endpoints:
web:
exposure:
include: bus-refresh
所以這就是上面為什麼這樣改的原因,執行重新整理的的請求為:
http://localhost:8881/actuator/bus-refresh
下載地址:
程式碼我已同步到碼雲git :https://gitee.com/honghh/spring-cloud-config