0%

springCloudConfig简介

Spring Cloud Config:分布式配置中心的实现与实践

Spring Cloud Config 是 Spring Cloud 生态中用于分布式配置管理的组件,旨在解决微服务架构中多服务配置的集中化、动态化管理问题。它将配置信息从服务中剥离,统一存储在版本控制系统(如 Git)中,通过配置中心提供配置访问接口,实现配置的集中管理和动态更新。

Spring Cloud Config 的核心架构

Spring Cloud Config 采用客户端 - 服务端架构,分工如下:

角色 职责
服务端 作为分布式配置中心,连接 Git 等版本控制系统,提供配置信息的访问接口。
客户端 启动时从服务端拉取配置,运行中可动态感知配置变化并更新。

核心优势

  • 集中管理多环境(开发、测试、生产)配置,避免配置分散在各服务中;
  • 基于 Git 实现配置的版本控制,支持配置回溯和审计;
  • 支持动态更新配置,服务无需重启即可应用新配置。

服务端配置(Config Server)

服务端是配置中心的核心,负责从 Git 仓库拉取配置并对外提供访问接口。

1. 引入依赖

在 Maven 项目的 pom.xml 中添加 Config Server 依赖:

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 若需注册到服务中心(如Eureka),需额外添加服务发现依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2. 启动类配置

通过 @EnableConfigServer 注解开启配置中心功能:

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableConfigServer // 标记为配置中心服务端
@EnableEurekaClient // 可选:注册到服务中心
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

3. 核心配置(application.yml)

配置 Git 仓库地址、搜索路径等信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server:
port: 7010 # 配置中心端口

spring:
application:
name: micro-service-config-server # 服务名
cloud:
config:
server:
git:
uri: https://github.com/isfox/micro_service_config # Git仓库地址(HTTPS)
# uri: git@github.com:isfox/micro_service_config.git # SSH方式(需配置密钥)
search-paths: myconfig # 仓库中存储配置的子目录(支持通配符,如"config/*")
clone-on-start: true # 启动时立即克隆仓库(默认首次请求时克隆)
force-pull: true # 强制拉取最新配置,避免本地缓存与远程不一致
basedir: /User/zhanghe/Desktop/config-repo # 本地缓存目录(可选,默认随机生成)
default-label: master # 默认分支(如不指定,客户端需显式指定label)

# 注册到Eureka(可选)
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

4. 配置访问规则

Config Server 提供 REST 接口访问配置,支持多种路径格式,核心规则如下(application 为服务名,profile 为环境,label 为 Git 分支):

接口格式 示例 说明
/{application}-{profile}.yml /user-service-dev.yml 获取 user-service 服务 dev 环境的配置
/{label}/{application}-{profile}.yml /master/user-service-prod.yml 获取 master 分支 prod 环境的配置
/{application}/{profile}/{label} /order-service/test/dev 路径参数形式访问

例如:若 Git 仓库中 myconfig 目录下有 user-service-dev.yml,则可通过 http://localhost:7010/user-service-dev.yml 访问。

客户端配置(Config Client)

客户端(各微服务)通过配置中心拉取自身配置,需使用 bootstrap.yml(系统级配置,优先级高于 application.yml)。

1. 引入依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 服务发现依赖(若配置中心已注册到服务中心) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2. 配置 bootstrap.yml

客户端通过 bootstrap.yml 指定配置中心地址和所需配置的标识:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
application:
name: user-service # 服务名(对应配置文件中的application)
cloud:
config:
# 配置中心地址(若配置中心已注册到服务中心,可替换为服务名)
uri: http://localhost:7010
# 若使用服务发现,需配置 discovery.enabled=true 和 service-id
# discovery:
# enabled: true
# service-id: micro-service-config-server
profile: dev # 环境(对应配置文件中的profile)
label: master # Git分支(对应label)
name: user-service # 配置文件名(默认与spring.application.name一致,可省略)

# 注册到Eureka(可选)
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/

为什么用 bootstrap.yml?
bootstrap.yml 属于系统级配置,在应用启动早期加载,用于初始化配置中心连接;而 application.yml 是用户级配置,加载时机较晚,依赖于 bootstrap.yml 中的配置中心信息。

3. 读取配置

客户端通过 @Value@ConfigurationProperties 注解读取配置中心的配置:

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class ConfigClientController {

// 读取配置中心的"app.version"配置
@Value("${app.version:unknown}")
private String appVersion;

@GetMapping("/version")
public String getVersion() {
return "当前版本:" + appVersion;
}
}

动态刷新配置

默认情况下,客户端启动后不会主动感知配置变化,需通过以下方式实现动态刷新:

1. 引入 actuator 依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. 配置刷新端点

application.yml 中暴露刷新端点:

1
2
3
4
5
management:
endpoints:
web:
exposure:
include: refresh # 暴露刷新端点

3. 标记需要刷新的类

在需要动态更新配置的类上添加 @RefreshScope 注解:

1
2
3
4
5
6
7
@RestController
@RefreshScope // 支持配置动态刷新
public class ConfigClientController {
@Value("${app.version:unknown}")
private String appVersion;
// ...
}

4. 触发刷新

修改 Git 仓库中的配置后,向客户端发送 POST 请求触发刷新:

1
curl -X POST http://localhost:服务端口/actuator/refresh

进阶:使用 Spring Cloud Bus 批量刷新
对于多客户端场景,可集成 Spring Cloud Bus(基于消息队列),通过一次请求触发所有客户端刷新,避免逐个调用端点。

实际应用注意事项

  1. 配置安全:生产环境需为配置中心添加认证(如 Spring Security),避免配置泄露。
  2. 加密配置:敏感配置(如数据库密码)可通过 spring-cloud-config-server 的加密功能加密存储,客户端自动解密。
  3. 高可用:配置中心作为核心组件,需集群部署(通过相同的 Git 仓库实现配置一致性)。
  4. 替代方案:Spring Cloud Config 依赖 Git 且动态刷新需手动触发,目前主流替代方案为 Nacos(集配置中心与服务发现于一体,支持自动刷新)。

总结

Spring Cloud Config 通过服务端 - 客户端架构实现了配置的集中管理,基于 Git 提供版本控制,适合对配置审计和回溯有要求的场景。其核心流程为:服务端从 Git 拉取配置 → 客户端从服务端获取配置 → 结合 Actuator 实现动态刷新

欢迎关注我的其它发布渠道

表情 | 预览
快来做第一个评论的人吧~
Powered By Valine
v1.3.10