關鍵詞:Spring Cloud Config、配置中心、遠程倉庫、動態刷新、加密解密
? 摘要
在微服務架構中,隨著服務數量的增加,統一管理各服務的配置信息變得尤為重要。傳統的本地配置文件方式難以滿足多環境、多實例、集中化的需求。
Spring Cloud Config 是 Spring Cloud 提供的一個分布式配置中心解決方案,支持將配置信息集中存儲在 Git、SVN 或本地文件系統中,并提供給各個微服務動態獲取和更新。
本文將圍繞 Spring Cloud Config 的核心功能與使用場景 展開講解:
- Spring Cloud Config 原理概述
- 搭建 Config Server 服務端
- 微服務集成 Config Client 客戶端
- 配置文件的命名規則與多環境支持
- 動態刷新配置(@RefreshScope)
- 加密與解密敏感信息
- 實戰案例:結合 Eureka、Git 實現配置中心集群部署
📌 一、Spring Cloud Config 簡介
🔹 1. 什么是 Spring Cloud Config?
Spring Cloud Config 是一個用于為分布式系統中的多個微服務提供統一外部配置管理的服務組件。它支持從遠程 Git/SVN 存儲庫或本地目錄中讀取配置文件。
🔹 2. 主要特性
特性 | 描述 |
---|---|
集中式管理 | 所有服務的配置統一管理,避免分散維護 |
多環境支持 | 支持 dev、test、prod 等不同 profile 配置 |
動態刷新 | 結合 Spring Cloud Bus + RabbitMQ/Kafka 實現配置熱更新 |
安全控制 | 支持加密解密敏感信息(如數據庫密碼) |
📌 二、搭建 Spring Cloud Config Server
🔹 1. 添加依賴(Maven)
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
</dependencies>
🔹 2. 啟動類添加注解
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}
🔹 3. 配置 application.yml
示例:連接 GitHub 倉庫
server:port: 8888
spring:cloud:config:server:git:uri: https://github.com/yourname/config-repo.gitsearch-paths: config-data # 可選,指定子目錄
🔹 4. 啟動服務并訪問配置
啟動后可以通過以下 URL 獲取配置:
http://localhost:8888/{application}/{profile}/{label}
例如:
http://localhost:8888/user-service/dev/master
📌 三、微服務作為 Config Client 接入
🔹 1. 添加依賴(Maven)
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
🔹 2. 創建 bootstrap.yml(注意不是 application.yml)
spring:application:name: user-servicecloud:config:uri: http://localhost:8888profile: devlabel: master
🔹 3. 使用配置項
@Value("${user.config}")
private String userConfig;@GetMapping("/config")
public String getConfig() {return "當前配置值:" + userConfig;
}
📌 四、配置文件命名規則詳解
Spring Cloud Config 根據以下格式查找配置文件:
/{application}-{profile}.yml
/{application}-{profile}.properties
例如:
user-service-dev.yml
order-service-prod.yml
你還可以通過 /config-data
目錄下的文件結構組織多服務、多環境配置。
📌 五、動態刷新配置(@RefreshScope)
🔹 1. 添加 Actuator 依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
🔹 2. 在 Bean 上添加 @RefreshScope 注解
@Component
@RefreshScope
public class UserProperties {@Value("${user.config}")private String userConfig;// getter/setter
}
🔹 3. 觸發刷新(POST 請求)
curl -X POST http://localhost:8080/actuator/refresh
📌 六、加密與解密敏感信息
🔹 1. 啟用加密支持
確保已安裝 Java Cryptography Extension (JCE),然后在配置中心項目中添加:
encrypt:key: my-secret-key
🔹 2. 加密明文內容
發送請求加密數據:
curl -X POST --data-urlencode "data=mydbpassword" http://localhost:8888/encrypt
返回類似:
{ "value": "ENC(AES/GCM/PBE加密后的字符串)" }
🔹 3. 在配置文件中使用加密值
spring:datasource:password: ENC(加密后的字符串)
📌 七、實戰案例:結合 Eureka 和 Git 實現配置中心集群
場景說明:
- 搭建兩個 Config Server 實例,分別運行在 8888 和 8889
- 使用同一個 Git 倉庫
- 微服務通過 Eureka 注冊發現 Config Server 地址
- 實現高可用與負載均衡
示例:Eureka + Config Client 配置
spring:cloud:config:discovery:enabled: trueservice-id: config-server
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
? 總結
以下是微服務配置的主要點:
模塊 | 技能點 |
---|---|
Spring Cloud Config 原理 | 配置中心的作用與工作原理 |
Config Server 搭建 | Git/SVN 配置、YAML 文件命名規范 |
Config Client 接入 | 微服務如何加載遠程配置 |
動態刷新機制 | 使用 @RefreshScope 實現熱更新 |
加密解密配置 | 對數據庫密碼等敏感信息進行安全處理 |
高可用部署 | 結合 Eureka 實現集群部署與負載均衡 |
📚 參考資料
- Spring Cloud Config 官方文檔
- Spring Boot Actuator 文檔