Spring Cloud Config 是 Spring Cloud 提供的一個用于集中化管理應用程序各個環境下的配置屬性的解決方案。它支持統一管理配置,并且可以在不重啟應用的情況下動態地更新配置信息,提高開發和運維效率。
主要特點
? 集中管理配置:可以將不同環境下(如開發、測試、生產)的應用配置集中存儲在一個地方,比如Git倉庫,便于管理和版本控制。
? 動態刷新配置:支持在不重啟應用的情況下,動態刷新配置信息,只需要引入spring-boot-starter-actuator依賴,并暴露 refresh 端點即可
實現對指定配置項的刷新。
? 多種存儲方式:雖然默認使用Git作為后端存儲,但也支持其他存儲方式,如Subversion、本地文件系統等,通過簡單的配置就可以切換。
? 加密與解密:提供對配置內容的加密與解密功能,增加了配置的安全性,特別是對于敏感信息的保護。
簡單使用示例
創建配置文件
在一個 Git 倉庫中創建配置文件 {application}-{profile}.yml 或者 {application}-{profile}.properties ,存放微服務的配置項。比如 myapp-dev.yml 或者 myapp-dev.properties ,表示的是微服務 myapp 在 dev 環境下的配置項。
創建 Config Server
pom.xml 添加依賴:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId>
</dependency>
Spring Boot 應用的主類上添加 @EnableConfigServer 注解以啟用 Config Server 功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}
在 application.properties 或 application.yml 中指定 Git 倉庫的位置,Config Server 將從這里讀取配置文件。
server:port: 8888 # 默認端口為8888spring:cloud:config:server:git:uri: https://github.com/your-repo/config-repo # 替換為配置文件的Git倉庫地址
創建 Config Client
在 pom.xml 文件中添加依賴:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在 bootstrap.properties 或 bootstrap.yml 中配置客戶端應用去連接到 Config Server 并獲取配置信息。
spring:application:name: myapp # 與 Config Server 拉取的 Git 的配置文件名匹配cloud:config:uri: http://localhost:8888 # Config Server的地址
這樣就可以在應用程序代碼中通過 @Value 注解或者直接注入 Environment 對象來訪問從 Config Server 獲取的配置值:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@Value("${your.config.key:default_value}")private String configValue;@GetMapping("/test")public String test() {returnthis.configValue;}
}
核心原理
Spring Cloud Config 的核心組件:
配置存儲(比如Git):
? 用戶將配置文件 push 到 Git 倉庫,配置文件按照 {application}-{profile}.yml 或者 {application}-{profile}.properties 格式命名。
Config Server:
? 配置服務器,負責從后端存儲(如 Git )中讀取配置,并提供給客戶端訪問。需要在項目中添加spring-cloud-config-server依賴,并使用@EnableConfigServer注解啟用。
? 每次 Config Client 請求獲取配置信息的時候,Config Server 會從 Git 把最新配置信息拉到本地文件系統(本地 Git 倉庫),然后從本地讀取并返回。當遠程倉庫無法獲取的時候,會直接從本地返回。
? 連接 Git 倉庫的配置:
? spring.cloud.config.server.git.uri:Git倉庫位置。
? spring.cloud.config.server.git.searchPaths:配置倉庫路徑下的相對搜索位置,可以配置多個。
? spring.cloud.config.server.git.username:訪問Git倉庫的用戶名。
? spring.cloud.config.server.git.password:訪問Git倉庫的用戶密碼。
Config Client:
? 配置客戶端,連接到配置服務器獲取配置信息,并根據這些配置啟動或運行。需要添加spring-cloud-starter-config依賴來接入配置中心服務。
? Cloud Client 在啟動的時候,默認會從工程的 classpath 中加載配置信息并啟動應用。只有當配置了 spring.cloud.config.uri 的時候,客戶端才會嘗試連接 Config Server 服務端來獲取遠程配置信息并初始化 Spring 環境配置。
? 備注:必須將該參數配置在 bootstrap.propertites、環境變量或其他優先級高于應用 Jar 包內的配置信息中,才能正確加載到遠程配置( Spring boot 的配置加載解有優先級)。
? Config Server 中存儲了多個 Config Client 的配置,因此 Config Client 需要配置聲明獲取哪一個 Client 的配置文件:
? spring.cloud.config.uri:配置服務端 Config Server 的地址。可以把 Config Server 作為一個普通的微服務應用,納入 Eureka 的服務治理體系中,這樣就可以通過配置中心的服務名(而不是具體 ip )來獲取配置信息。
? spring.application.name:應用名,對應配置文件規則中的 {application} 部分。
? spring.cloud.config.profile:環境名,對應配置文件規則中的 {profile} 部分,比如 pre 、gray 、online 。
? spring.cloud.config.label:指定 Git 的 branch、tag 或 commit ID,不填寫則默認為獲取 Git 的 master 分支配置。
Spring Cloud Config 動態更新配置原理:
核心概念:
? Environment 和 PropertySource:在 Spring 中,Environment 是一個關鍵接口,它代表了當前應用程序正在運行的環境,并且包含了多個 PropertySource 實例。每個 PropertySource 都是一個潛在的屬性來源,如系統屬性、環境變量或外部配置文件等。Spring Cloud Config Client 會將從 Config Server 獲取到的配置作為 PropertySource 添加到 Environment 中。
? @RefreshScope 注解:這是 Spring Cloud 提供的一個特殊 Bean 作用域。標記為 @RefreshScope 的 Bean 在啟動時會被代理,當觸發刷新操作時,這些 Bean 會重新初始化它們的狀態,以反映最新的配置值,特別適用于那些需要根據最新配置立即做出反應的組件。
刷新機制:
? /actuator/refresh 端點:Spring Boot Actuator 提供了一個 /actuator/refresh 端點,允許用戶手動觸發配置的刷新操作。當發送 POST 請求到這個端點時,Spring Cloud 會掃描所有標注了 @RefreshScope 的 Bean,并重新加載它們的配置。
? Spring Cloud Bus:為了簡化在微服務架構中廣播配置變更通知的過程,可以使用 Spring Cloud Bus。它本質上是一個消息總線,可以用來傳播狀態變化(如配置更新)。通過向任意服務實例發送 /actuator/bus-refresh 請求,可以觸發整個集群的服務配置更新,而不需要逐個調用每個服務的刷新端點。
動態刷新配置步驟:
? 客戶端請求刷新:可以通過 HTTP POST 請求訪問 /actuator/refresh 端點來手動觸發刷新過程。
? 獲取最新配置:一旦收到刷新請求,Spring Cloud Config Client 會嘗試從 Config Server 獲取最新的配置信息。
? 更新 Environment:獲取到的新配置將被添加到現有的 Environment 中,覆蓋舊的配置值。
? 重刷 @RefreshScope Beans:對于那些被 @RefreshScope 注解標記的 Bean,Spring 將重新初始化它們,使它們能夠感知到最新的配置變化。
? @RefreshScope 是 Spring Cloud 中用于實現配置動態刷新的機制,其原理是通過創建一個代理對象來替代被標記為 @RefreshScope 的 Bean,并采用延遲初始化策略(即僅在首次訪問時根據最新的配置信息創建并初始化該 Bean)。當配置發生變更并觸發刷新操作時,Spring Cloud 會發布刷新事件,RefreshScope 捕獲此事件后將相關 Bean 的緩存視為過期并清除;下次訪問這些 Bean 時,系統基于更新后的配置重新創建和初始化它們,從而實現在不重啟應用的情況下響應配置變化的能力。