【微服务学习手册】Spring Cloud Config 配置中心入门教程(基于 Gitee)
·
一、整体架构说明
┌─────────┐ ┌──────────────┐ ┌──────────────┐
│ Gitee │ ──→ │ Config Server│ ──→ │ Config Client│
│ (仓库) │ │ (3344) │ │ (3355) │
└─────────┘ └──────────────┘ └──────────────┘
- Gitee:存放不同环境的配置文件(如
config-dev.yml) - Config Server:暴露配置服务,从 Gitee 拉取配置
- Config Client:从 Config Server 获取配置并启动应用
客户端默认只在启动时加载一次配置,修改仓库后需重启或手动刷新。
二、准备工作
2.1 登录 Gitee 并新建仓库
- 登录 Gitee
- 新建仓库:
springcloud-config-server - 保持仓库为空(后续会添加配置文件)
三、搭建 Config Server(模块:cloud-config-center-3344)
3.1 创建模块
在根目录下新建子模块:cloud-config-center-3344
3.2 添加 pom 依赖
<dependencies>
<!-- Spring Cloud Config Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- Eureka Client(可选,用于服务注册) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3.3 编写 application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/你的用户名/springcloud-config-server
# 如果配置文件在仓库根目录,无需 search-paths
# search-paths: config-repo
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
3.4 编写启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class, args);
}
}
四、搭建 Config Client(模块:cloud-config-client-3355)
4.1 创建模块
在根目录下新建子模块:cloud-config-client-3355
4.2 添加 pom 依赖
<dependencies>
<!-- Spring Cloud Config Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Web 支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka Client(可选) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
4.3 编写 bootstrap.yml(重要)
⚠️ 必须使用
bootstrap.yml,它比application.yml优先加载,用于连接 Config Server。
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名
uri: http://localhost:3344 # Config Server 地址
profile: dev # 环境标识(对应 config-dev.yml)
4.4 编写 Controller
@RestController
public class ConfigClientController {
@Value("${info}") // 从配置文件中读取 info 属性
private String info;
@GetMapping("/configInfo")
public String getConfigInfo() {
return info;
}
}
4.5 编写启动类
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
五、准备 Gitee 仓库中的配置文件
在 Gitee 仓库 springcloud-config-server 中新建两个文件:
5.1 config-dev.yml
info: Hello from dev environment
5.2 config-prod.yml
info: Hello from production environment
提交并推送到 Gitee。
六、测试基本功能
6.1 启动顺序
Eureka 7001 → Config Server 3344 → Config Client 3355
6.2 访问测试
| 模块 | URL | 预期结果 |
|---|---|---|
| Config Server | http://localhost:3344/master/config-dev.yml |
显示 Gitee 中的配置文件内容 |
| Config Client | http://localhost:3355/configInfo |
显示 Hello from dev environment |
6.3 修改配置(发现问题)
- 修改 Gitee 中
config-dev.yml的info值 - 刷新
3344/master/config-dev.yml→ 立即更新 ✅ - 刷新
3355/configInfo→ 仍然是旧值 ❌
说明:客户端只在启动时加载一次配置,不会自动更新。
6.4 重启客户端后生效
- 重启
3355服务 - 再次访问
localhost:3355/configInfo→ 新值生效 ✅
七、进阶:实现动态刷新(不重启)
使用 Spring Boot Actuator 的
/actuator/refresh端点。
7.1 添加依赖(client 3355)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
7.2 暴露 refresh 端点(bootstrap.yml)
management:
endpoints:
web:
exposure:
include: refresh
7.3 在 Controller 上加 @RefreshScope
@RestController
@RefreshScope // 必须加,支持动态刷新
public class ConfigClientController {
// ...
}
7.4 手动触发刷新
修改 Gitee 配置后,执行:
curl -X POST http://localhost:3355/actuator/refresh
再次访问 localhost:3355/configInfo → 无需重启即可看到新值 ✅
八、常见问题 FAQ
Q1:客户端必须用 bootstrap.yml 吗?
是的。bootstrap.yml 比 application.yml 先加载,用于连接 Config Server 获取外部配置。
Q2:如何区分 dev / prod 环境?
通过客户端 bootstrap.yml 中的 spring.cloud.config.profile 指定:
dev→config-dev.ymlprod→config-prod.yml
Q3:为什么修改 Git 配置后客户端不自动刷新?
Spring Cloud Config 默认只在启动时加载一次配置。如需动态刷新,必须使用 Actuator 的 /refresh 端点。
Q4:能否不逐个手动刷新所有客户端?
可以。集成 Spring Cloud Bus 可实现一次刷新、广播通知所有客户端。
九、总结
| 组件 | 核心作用 | 关键配置 |
|---|---|---|
| Config Server | 从 Git 仓库暴露配置服务 | @EnableConfigServer + spring.cloud.config.server.git.uri |
| Config Client | 从 Config Server 拉取配置 | bootstrap.yml + @RefreshScope |
| Actuator | 实现动态刷新 | /actuator/refresh + management.endpoints.web.exposure.include |
本教程完成了 Config 配置中心的基本功能:
Gitee → Config Server(3344) → Config Client(3355)
如果需要实现所有客户端自动刷新,可继续学习 Spring Cloud Bus 集成。
更多推荐
所有评论(0)