Spring Cloud Config 統一配置中心實戰:Git 後端存儲、客戶端自動刷新與 Bus 總線擴展
架構概覽
[Git/SVN Repo] <--配置文件--> [Config Server] <--HTTP/REST--> [Config Client 1]
[Config Client 2]
[Config Client N]
|
[Spring Cloud Bus] (可選:廣播刷新)
模塊劃分
模塊 職責 關鍵依賴
config-server配置服務端,掛載 Git 倉庫,暴露 //[/] 端點 spring-cloud-config-server
config-client微服務客戶端,啟動時拉取配置,支持 /actuator/refresh 熱更新 spring-cloud-starter-config, spring-boot-starter-actuator
config-repoGit 倉庫(或本地目錄),存放 application.yml, service-a-dev.yml 等 無
Config Server 核心配置
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/your-org/config-repo # 或 file:///local/path
username: xxx
password: xxx
default-label: main
search-paths: '{application}' # 支持多項目隔離
profiles:
active: native # 本地開發可用 native profile 指向 classpath:config
Config Client 核心配置
spring:
application:
name: service-a
cloud:
config:
uri: https://localhost:8888 # 或通過 Eureka 發現: discovery.enabled=true, service-id=config-server
profile: dev
label: main
fail-fast: true # 配置獲取失敗快速啟動失敗
retry:
initial-interval: 2000
max-attempts: 6
multiplier: 1.5
management:
endpoints:
web:
exposure:
include: refresh,health,info # 暴露 refresh 端點
endpoint:
health:
show-details: always
業務代碼使用 @RefreshScope 實現熱更新
@RestController
@RefreshScope // 關鍵:標註此注解的 Bean 會在 /refresh 後重新綁定配置
public class ConfigController {
@Value("${app.greeting:Hello}")
private String greeting;
@GetMapping("/greeting")
public String greeting() {
return greeting;
}
}
手動觸發單實例刷新
curl -X POST https://localhost:8080/actuator/refresh
# 響應:["app.greeting"] 表示該屬性已更新
分布式場景引入 Spring Cloud Bus 自動廣播刷新
架構變更
[Git Repo] --> [Config Server] --> [Message Broker (RabbitMQ/Kafka)] --> [Client 1]
[Client 2]
[Client N]
# 任一 Client 執行 /bus/refresh,總線廣播 RefreshRemoteApplicationEvent,所有 Client 同步刷新
依賴與配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId> # RabbitMQ
</dependency>
<!-- 或 kafka: spring-cloud-starter-bus-kafka -->
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: busrefresh # 暴露 /actuator/busrefresh
一鍵刷新全集群
# 僅需對任一 Client 或 Config Server 執行
curl -X POST https://localhost:8080/actuator/busrefresh
# 或指定目標服務
curl -X POST https://localhost:8080/actuator/busrefresh/service-a:dev
生產環境優化建議
高可用 Config Server :多實例部署 + Eureka 註冊,客戶端配置 discovery.enabled=true。
配置加密 :敏感值(密碼、密鑰)在 Git 中存儲密文 {cipher}xxx,Server 配置 RSA 密鑰對自動解密。
權限控制 :Spring Security 保護 /actuator/refresh、/busrefresh 端點。
配置版本回滾 :Git 天然支持,label 指向 tag/commit/branch。
本地覆蓋 :spring.cloud.config.override-none=true 防止遠程配置覆蓋本地測試配置。
對比:Spring Cloud Config vs 百度 Disconf
維度 Spring Cloud Config Disconf
生態集成 Spring Cloud 全家桶無縫整合 獨立框架,需適配
動態刷新 @RefreshScope + Bus 廣播 原生支持 ZooKeeper 推送
多環境隔離 Profile + Label 天然支持 支持但配置較繁瑣
社區活躍度 高(Spring 官方維護) 中(百度開源,近年更新慢)
學習曲線 低(Spring 風格) 中
總結
Spring Cloud Config 提供了開箱即用的分布式配置中心能力,配合 Spring Cloud Bus 可實現毫秒級全集群配置熱更新。雖然在極大規模、超高頻配置變更場景下不如專業配置中心(Apollo、Nacos)高效,但對於中小型微服務體系,其零侵入、原生集成、Git 版本化優勢極大,是首選方案。