Skip to content

Config 配置中心

一、概述

┌─────────────────────────────────────────────────────────────────┐
│                      配置中心架构                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│                    ┌─────────────┐                              │
│                    │   Config    │                              │
│                    │   Server    │                              │
│                    │  (配置仓库)  │                              │
│                    └──────┬──────┘                              │
│                           │                                      │
│         ┌─────────────────┼─────────────────┐                   │
│         ▼                 ▼                 ▼                   │
│  ┌────────────┐     ┌────────────┐     ┌────────────┐          │
│  │ User       │     │  Order     │     │  Product   │          │
│  │ Service    │     │  Service   │     │  Service   │          │
│  └────────────┘     └────────────┘     └────────────┘          │
│                                                                  │
│  集中管理配置,支持配置变更推送                                    │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

二、Spring Cloud Config

Config Server

xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
yaml
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-org/config-repo
          username: your-username
          password: your-password
          default-label: main
          search-paths: '{application}'
java
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

Config Client

xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
yaml
# bootstrap.yml(注意是 bootstrap)
spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev
      label: main

三、Nacos 配置中心

引入依赖

xml
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

配置

yaml
spring:
  application:
    name: user-service
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: dev
        group: DEFAULT_GROUP
        file-extension: yaml
        refresh-enabled: true
        shared-configs:
          - data-id: common.yaml
            group: COMMON_GROUP
            refresh: true

配置 data-id

${spring.application.name}-${spring.profiles.active}.${file-extension}
= user-service-dev.yaml

四、配置刷新

@RefreshScope

java
@RestController
@RefreshScope  // 支持动态刷新
public class UserController {
    
    @Value("${app.feature.enabled:false}")
    private boolean featureEnabled;
    
    @GetMapping("/feature")
    public String feature() {
        return featureEnabled ? "功能已开启" : "功能未开启";
    }
}

配置变更监听

java
@Component
public class NacosConfigListener {
    
    @NacosConfigListener
    public void onChange(String config) {
        System.out.println("配置变更: " + config);
        // 重新加载配置
    }
}

五、面试高频问题

Q1: 为什么用 bootstrap.yml?

Config Client 需要在应用启动前获取配置,所以用 bootstrap 优先加载

Q2: @RefreshScope 原理?

使用代理,每次刷新时销毁旧 Bean,创建新 Bean

六、下一章预告

下一章我们将学习 Bean 生命周期

  • 实例化 → 属性填充 → 初始化 → 销毁
  • Spring 后置处理器
  • 初始化回调顺序

基于 MIT 许可发布