Spring Cloud Config #
一、Config概述 #
1.1 什么是配置中心 #
配置中心是集中管理微服务配置的组件,支持配置的集中存储、版本管理和动态刷新。
text
┌─────────────────────────────────────────────────────────────┐
│ 配置中心架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 服务实例A │ │ 服务实例B │ │ 服务实例C │ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │
│ └────────────────┼────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Config Server│ │
│ │ 配置服务器 │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Git Repository│ │
│ │ 配置仓库 │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
1.2 Config特性 #
| 特性 | 说明 |
|---|---|
| 集中管理 | 统一管理所有服务配置 |
| 版本控制 | 支持Git版本管理 |
| 环境隔离 | 支持多环境配置 |
| 动态刷新 | 支持配置热更新 |
| 加密解密 | 支持配置加密 |
二、服务端配置 #
2.1 添加依赖 #
xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.2 启动类 #
java
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2.3 配置文件 #
yaml
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/example/config-repo
search-paths: '{application}'
username: ${GIT_USERNAME}
password: ${GIT_PASSWORD}
default-label: main
2.4 访问配置 #
text
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
三、客户端配置 #
3.1 添加依赖 #
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2 bootstrap.yml #
yaml
spring:
application:
name: user-service
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
label: main
fail-fast: true
3.3 使用配置 #
java
@RestController
@RefreshScope
public class ConfigController {
@Value("${app.message:default}")
private String message;
@GetMapping("/message")
public String getMessage() {
return message;
}
}
四、Git仓库配置 #
4.1 目录结构 #
text
config-repo/
├── user-service-dev.yml
├── user-service-test.yml
├── user-service-prod.yml
├── order-service-dev.yml
├── order-service-test.yml
└── order-service-prod.yml
4.2 配置文件示例 #
yaml
server:
port: 8001
spring:
datasource:
url: jdbc:mysql://localhost:3306/user_db
username: root
password: password
app:
message: Hello from Config Server
feature:
enabled: true
五、配置刷新 #
5.1 添加依赖 #
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5.2 配置端点 #
yaml
management:
endpoints:
web:
exposure:
include: refresh,health,info
5.3 刷新配置 #
bash
curl -X POST http://localhost:8001/actuator/refresh
六、加密解密 #
6.1 配置密钥 #
yaml
encrypt:
key: my-secret-key
6.2 加密配置 #
bash
curl http://localhost:8888/encrypt -d mypassword
6.3 使用加密值 #
yaml
spring:
datasource:
password: '{cipher}加密后的值'
七、总结 #
| 要点 | 说明 |
|---|---|
| Config Server | 配置服务器 |
| Config Client | 配置客户端 |
| Git仓库 | 配置存储 |
| 动态刷新 | @RefreshScope |
接下来让我们学习 Nacos配置中心!
最后更新:2026-03-28