# 05.微服务-Nacos-config

# 1.Nacos 配置中心介绍

# 1.1 什么是配置中心?

在微服务架构中,每个服务都有各自的配置文件,且不同环境(开发、测试、生产)配置存在差异,导致配置管理复杂度急剧上升。配置中心作为微服务架构的核心组件,提供集中化的配置存储、版本管理和动态更新能力,解决配置分散、变更难以追溯、动态调整困难等痛点

# 1.2 Nacos 配置管理的特点

  • 维护性(统一管理方便维护)

  • 时效性(不用再重启系统,动态刷新)

  • 安全性(基于用户-角色-资源的精细权限管理)

# 1.3 核心概念

概念 说明
Data ID 配置的唯一标识,如 order-service.yaml
Group 配置分组,默认为 DEFAULT_GROUP,用于归类管理
Namespace 命名空间,实现多环境/多租户配置隔离,彼此相互独立

# 2.快速入门

# 2.1 导入依赖

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 2.2 Nacos管理界面配置

在 Nacos Spring Cloud 中,dataId 的完整格式如下:

如果不指定环境的话,默认文件不用加环境,制定环境后,后台会根据以下格式去Nacos中找对应的配置

${prefix}-${spring.profile.active}.${file-extension}
1

# 2.3 配置文件配置

bootstrap.yml

spring:
  application:
    name: com.xy.order
  cloud:
    nacos:
      server-addr: 47.94.9.59:8848
      config:
        username: nacos
        password: 2eb4e10f
        file-extension: yml
1
2
3
4
5
6
7
8
9
10

注:file-extension为文件扩展名

# 3.其他扩展配置

# 3.1 shared-configs(共享配置)

多个服务共享的通用配置(如数据库连接、公共组件配置)

spring:
  application:
    name: com.xy.order
  cloud:
    nacos:
      server-addr: 47.94.9.59:8848
      config:
        username: nacos
        password: 2eb4e10f
        file-extension: yml
        shared-configs:
            - data-id: com.xy.common.yml
              refresh: true
              group: xy
1
2
3
4
5
6
7
8
9
10
11
12
13
14

注:shared-configs的自动刷新(refresh)默认是关的,需要手动打开

# 3.2 extension-configs(扩展配置)

为特定服务扩展额外配置:

spring:
  application:
    name: com.xy.order
  cloud:
    nacos:
      server-addr: 47.94.9.59:8848
      config:
        username: nacos
        password: 2eb4e10f
        file-extension: yml
        shared-configs:
            - data-id: com.xy.common.yml
              refresh: true
              group: xy
        extension-configs:
            - data-id: com.xy.extension.yml
              refresh: true
              group: xy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 3.3 配置优先级

  1. 当前服务自己的配置

  2. 如果第一个是带环境的配置情况,加载默认配置${spring.application.name}.yaml

  3. extension-configs

  4. shared-configs

  5. 本地 application.yml

越靠前优先级越高,后定义的会覆盖前面的同名配置。

# 4.@RefreshScope 详解

# 4.1 作用

@RefreshScope 是 Spring Cloud 提供的特殊作用域,标记的 Bean 在配置刷新时会销毁旧实例,下次调用时重建并使用最新配置,从而实现配置(在nacos中的配置)热更新而不重启应用

# 4.2 使用方式

// 方式一:配合 @Value
@RestController
@RefreshScope
public class ConfigController {
    @Value("${app.timeout}")
    private int timeout;
}

// 方式二:配合 @ConfigurationProperties(推荐)
@Component
@RefreshScope
@ConfigurationProperties(prefix = "app.settings")
public class AppSettings {
    private String name;
    private int timeout;
    // getter/setter
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
最近更新: 9/19/2026, 1:27:08 PM
编程NOTE   |