Nacos配置中心 #

一、Nacos配置中心概述 #

1.1 什么是Nacos配置中心 #

Nacos提供了统一的配置管理功能,支持配置的动态更新、版本管理和灰度发布。

text
┌─────────────────────────────────────────────────────────────┐
│                    Nacos配置中心架构                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐  ┌─────────────┐  ┌─────────────┐        │
│   │  服务实例A  │  │  服务实例B  │  │  服务实例C  │        │
│   └──────┬──────┘  └──────┬──────┘  └──────┬──────┘        │
│          │                │                │                │
│          └────────────────┼────────────────┘                │
│                           │                                 │
│                           ▼                                 │
│                   ┌─────────────┐                          │
│                   │    Nacos    │                          │
│                   │ Config Center│                          │
│                   └─────────────┘                          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

1.2 Nacos配置中心特性 #

特性 说明
动态配置 配置实时更新
版本管理 配置历史版本
灰度发布 配置灰度发布
命名空间 环境隔离
分组管理 配置分组

二、基本使用 #

2.1 添加依赖 #

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

2.2 bootstrap.yml #

yaml
spring:
  application:
    name: user-service
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: dev
        group: DEFAULT_GROUP
        file-extension: yaml
        refresh-enabled: true

2.3 使用配置 #

java
@RestController
@RefreshScope
public class ConfigController {

    @Value("${app.message:default}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}

三、命名空间与分组 #

3.1 命名空间 #

yaml
spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: dev

3.2 分组 #

yaml
spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        group: USER_GROUP

四、多配置文件 #

4.1 配置多个文件 #

yaml
spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        shared-configs:
          - data-id: common.yaml
            group: DEFAULT_GROUP
            refresh: true
          - data-id: database.yaml
            group: DEFAULT_GROUP
            refresh: true
        extension-configs:
          - data-id: redis.yaml
            group: DEFAULT_GROUP
            refresh: true

五、配置刷新 #

5.1 自动刷新 #

yaml
spring:
  cloud:
    nacos:
      config:
        refresh-enabled: true

5.2 手动刷新 #

java
@RestController
@RefreshScope
public class ConfigController {

    @Value("${app.message}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}

六、总结 #

要点 说明
命名空间 环境隔离
分组 配置分组
动态刷新 @RefreshScope
多配置 shared-configs

接下来让我们学习 配置加密

最后更新:2026-03-28