In-depth Microservices - Core Concepts and Practices of Nacos Configuration Center

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Microservices Series Article Directory


foreword

This series takes you deep into the basic use and underlying principles of each framework of the microservice Spring system. The previous article introduced the core concepts of Nacos and the function of the client to realize service discovery. This section will take you to learn the core concepts and actual combat of Nacos Config.


The role of the distributed configuration center

The distributed configuration center can centrally manage the program configuration and realize the dynamic modification of the configuration content of the program without restarting the service container. The distributed configuration centers on the market include Ctrip's Apollo, SpringCloud Config, and Nacos Config configuration center.

What is Nacos Configuration Center

  • Nacos Config provides a key/value store for storing configuration and other metadata, providing server-side and client-side support for externalized configuration in distributed systems
  • Using Nacos Config, you can centrally manage the external property configuration of Spring Cloud applications in the Nacos Server configuration interface
  • As the application goes from development to test to production through a deployment pipeline (eg: Jenkins), it is possible to manage the configuration between these environments and ensure that the application has everything it needs to run when migrated

Nacos Config.png

Core Concepts of Nacos Configuration Center

  • Configuration

In the software development process, some parameters and variables that need to be changed are separated from the code and managed independently, and exist in the form of independent configuration files. Different configurations can be switched according to different deployment environments. Configuration changes are one of the most effective means of tuning system runtimes.

  • 配置管理 (Configuration Management)

在数据中心中,系统中所有配置的编辑、存储、分发、变更管理、历史版本管理、变更审计等所有与配置相关的活动统称为配置管理

Nacos 配置中心的配置一致性模型

Nacos 配置管理一致性分为两部分,第一部分是Server间一致性协议(Nacos Server集群环境),一个是SDK(Nacos Config Client)与Server的一致性协议,配置作为分布式系统中飞强一致性数据,在出现脑裂的时候高可用性高于一致性,因此配置中心采用的是AP一致性协议

Nacos Config 实战

需实现效果:通过Nacos实现配置的动态变更

1、启动Nacos Server 2、nacos server中新建serviceA-dev.yaml

Nacos Config configuration interface.png

3、搭建Nacos Config 服务 3.1) 引入Nacos Client依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
复制代码

3.2) 添加bootstrap.yaml

spring:
  profiles:
    active: dev #指定环境
  application:
    name: serviceA # 服务名称
  cloud:
    nacos:
      config:
        server-addr: nacos:8848
        file-extension: yaml
        username: nacos
        password: nacos
复制代码

3.3) 添加启动类,测试功能

@SpringBootApplication
public class NacosConfigApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class, args);
        String userName = applicationContext.getEnvironment().getProperty("user.name");
        String userAge = applicationContext.getEnvironment().getProperty("user.age");
        System.out.println("my name is :"+userName+"; age: "+userAge);
    }
}
复制代码

启动服务之后,控制台输出

Nacos Config output interface.png

Nacos Config功能

支持profile粒度的配置

spring-cloud-starter-alibaba-nacos-config 在加载配置的时候,不仅仅加载了以 dataid 为 ${spring.application.name}.${file-extension:properties} 为前缀的基础配置,还加载了dataid为 ${spring.application.name}-${profile}.${file-extension:properties} 的基础配置, 可以通过Spring 提供的 ${spring.profiles.active} 这个配置项来配置,实现读取不同的环境

spring:
  profiles:
    active: dev #指定环境
复制代码

支持自定义namespace配置

Namespace 的常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等 在没有明确指定 ${spring.cloud.nacos.config.namespace} 配置的情况下, 默认使用的是 Nacos 上 public 这个namespae。如果需要使用自定义的命名空间,可以通过以下配置来实现: namespace 对应的是命名空间的id

spring:
  cloud:
    nacos:
      config:
        namespace: xxx # 命名空间ID
复制代码

The namespace ID is generated by default and can be specified

Nacos Namespace ID.png

Support custom Group configuration

In the absence of an explicit ${spring.cloud.nacos.config.group}configuration , it is used by default DEFAULT_GROUP. If you need to customize your own Group, you can do it through the following configuration:

spring:
  cloud:
    nacos:
      config:
        group: DEV_GROUP
复制代码

Support shared configuration

Shared configuration solves the problem of configuration sharing among multiple applications

spring:
  cloud:
    nacos:
      config:
        shared-configs:
          - data-id: shared-configs.yml
            refresh: true
复制代码
  • Multiple shared Data Id configurations are supported through spring.cloud.nacos.config.shared-configs[n].data-id.
  • Use spring.cloud.nacos.config.shared-configs[n].group to configure the group where the custom Data Id is located. If it is not explicitly configured, the default is DEFAULT_GROUP.
  • Use spring.cloud.nacos.config.shared-configs[n].refresh to control whether the Data Id supports dynamic refresh in the application when the configuration is changed, the default is false

Support custom extended DataId configuration

Support configuration of custom Data Id

spring:
  cloud:
    nacos:
      config:
        extension-configs:
          - data-id: shared-configs.yml
            refresh: true
复制代码
  • The configuration of multiple Data Ids is supported through the configuration method of spring.cloud.nacos.config.extension-configs[n].data-id.
  • Customize the group where the Data Id is located through the configuration method of spring.cloud.nacos.config.extension-configs[n].group. If the configuration is not clear, the default is DEFAULT_GROUP.
  • The configuration method of spring.cloud.nacos.config.extension-configs[n].refresh is used to control whether the Data Id supports dynamic refresh in the application when the configuration changes, and perceives the latest configuration value. Default is not supported

Summarize

The basics of Nacos are almost finished, and I will bring you the Nacos source code analysis part later, thank you for your support

Guess you like

Origin juejin.im/post/7085926274484404232