2. Spring Cloud Config 概述
Spring Cloud Config 是一个典型的客户端-服务器架构的配置中心解决方案。它的核心工作流程如下:
- 集中式服务器:从外部数据源读取配置数据
- HTTP 接口暴露:通过标准 HTTP 接口提供配置查询服务
- 客户端自动集成:Spring Boot 应用可自动连接配置服务器
- 无缝属性使用:客户端应用像使用本地属性一样使用远程配置
关键特性:配置服务器提供的配置数据在客户端应用中会被视为标准属性源(Property Source),这意味着你可以通过 @Value
注解或 @ConfigurationProperties
直接注入使用。
3. Git 存储方案
Git 是 Spring Cloud Config 最常用的配置存储方案,主要优势包括:
- 灵活性:支持多种文件格式(包括二进制文件)
- 安全性:细粒度的读写权限控制
- 审计能力:完整的变更历史追踪
- 标准化:无论自建还是使用第三方 Git 服务,操作方式统一
- 分布式特性:天然适合云原生和微服务架构
但 Git 并非万能:当你的配置已存储在关系型数据库等其他系统时,迁移到 Git 可能得不偿失。接下来我们重点探讨非 Git 方案。
4. 非 Git 配置源实现
4.1 文件系统方案
使用本地文件系统作为配置源是最简单的替代方案,但需注意分布式部署时的文件共享问题。
启用步骤:
- 在
application.properties
中指定搜索路径:# 类路径资源 spring.cloud.config.server.native.search-locations=resources/other.properties # 外部文件路径 spring.cloud.config.server.native.search-locations=file:///external/path/other.properties
- 启动时激活
native
配置:-Dspring.profiles.active=native
踩坑提醒:生产环境务必使用分布式文件系统(如 NFS),避免单点故障。
4.2 JDBC 方案
通过关系型数据库存储配置,适合已有数据库基础设施的团队。
实现步骤:
- 添加依赖(若不存在):
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency>
- 配置数据库连接:
支持任何 JDBC 兼容数据库spring.datasource.url=jdbc:mysql://dbhost:3306/springconfig spring.datasource.username=dbuser spring.datasource.password=dbpassword spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- 创建标准表结构:
CREATE TABLE PROPERTIES ( APPLICATION VARCHAR(100), PROFILE VARCHAR(100), LABEL VARCHAR(100), KEY VARCHAR(100), VALUE VARCHAR(500) );
- 激活 JDBC 配置:
-Dspring.profiles.active=jdbc
4.3 Redis 方案
适合需要高性能配置读取的场景,支持多环境隔离。
配置步骤:
- 添加依赖:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> </dependency>
- 配置 Redis 连接:
spring.redis.host=localhost spring.redis.port=6379
- 存储配置数据:
# 基础配置 HMSET application sample.property.name1 "somevalue" sample.property.name2 "anothervalue" # 开发环境配置 HMSET application-dev sample.property.name1 "dev-value" sample.property.name2 "dev-another"
- 激活 Redis 配置:
-Dspring.profiles.active=redis
验证数据:
HGETALL application
# 返回
{
"sample.property.name1": "somevalue",
"sample.property.name2": "anothervalue"
}
4.4 密钥管理方案
云服务商提供的密钥管理服务(如 AWS Secrets Manager)是存储敏感配置的理想选择。
AWS 实现步骤:
- 添加 starter 依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId> <version>2.2.6.RELEASE</version> </dependency>
- 配置
bootstrap.yml
:aws: secretsmanager: default-context: application prefix: /config profile-separator: _ fail-fast: true name: ConfigServerApplication enabled: true
- 在 AWS 控制台创建密钥:
- 基础密钥路径:
/config/application/database_credentials
- 开发环境密钥:
/config/application/database_credentials_dev
- 基础密钥路径:
4.5 S3 方案
利用云存储服务(如 AWS S3)存储配置文件,适合需要版本控制的场景。
配置步骤:
- 添加 AWS SDK:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3outposts</artifactId> <version>1.12.150</version> </dependency>
- 配置访问凭证:
amazon.s3.access-key=your-access-key amazon.s3.secret-key=your-secret-key
- 指定 S3 存储桶:
spring: cloud: config: server: awss3: region: us-east-1 bucket: config-bucket
- 激活 S3 配置:
-Dspring.profiles.active=awss3
文件命名规范:
- 默认配置:
application.properties
- 环境配置:
application-dev.properties
- 重要提示:必须包含默认配置文件(
application.properties
或application-default.properties
)
4.6 自定义配置源
当现有方案无法满足需求时,可通过实现 EnvironmentRepository
接口创建自定义配置源。
实现示例:
public class CustomConfigurationRepository implements EnvironmentRepository, Ordered {
@Override
public Environment findOne(String application, String profile, String label) {
// 从自定义数据源(如NoSQL、API等)加载配置
Environment env = new Environment(application, profile);
// 添加配置属性...
return env;
}
@Override
public int getOrder() {
// 设置加载优先级(数值越小优先级越高)
return 0;
}
}
注册为 Spring Bean:
@Bean
public CustomConfigurationRepository customConfigurationRepository() {
return new CustomConfigurationRepository();
}
5. 多配置源组合
实际生产中常需组合多个配置源(如 JDBC + Redis),通过以下步骤实现:
在
bootstrap.yml
中定义优先级:spring: cloud: config: server: redis: order: 2 # 优先级较低 jdbc: order: 1 # 优先级较高
优先级规则:数值越小优先级越高
激活多个配置源:
-Dspring.profiles.active=jdbc,redis
支持任意数量的配置源组合
6. 总结
Spring Cloud Config 虽然默认推荐 Git,但提供了丰富的替代方案:
- 文件系统:适合简单场景,注意分布式部署问题
- JDBC:完美集成现有数据库基础设施
- Redis:高性能配置读取,支持环境隔离
- 密钥管理:安全存储敏感配置
- S3:利用云存储实现版本控制
- 自定义实现:满足特殊业务需求
核心优势:无论使用哪种配置源,客户端集成方式完全一致,真正实现配置存储与业务逻辑解耦。根据团队技术栈和运维需求选择最合适的方案,避免盲目跟风。