Spring cloud Alibaba: introduction to nacos configuration center

Keywords: Java Back-end Spring Cloud

Spring cloud Alibaba: introduction to nacos configuration center

1, Introduction to configuration center

1. Introduction

Nacos provides key/value storage for storing configuration and other metadata, and provides server-side and client-side support for externalized configuration in distributed systems. Using Spring Cloud Alibaba Nacos Config, you can centrally manage the external attribute configuration of your Spring Cloud application in the Nacos Server.
Spring Cloud Alibaba Nacos Config is an alternative to Config Server and Client. The concepts on the Client and server are consistent with the Spring Environment and PropertySource. In the special bootstrap stage, the configuration is loaded into the Spring Environment. When an application is deployed through a pipeline from development to test to production, you can manage the configuration between these environments and ensure that the application has everything it needs to run when migrating

2. dataid

In the Nacos Spring Cloud, the complete format of dataId is as follows:

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

Prefix defaults to the value of spring.application.name, which can also be configured through the configuration item spring.cloud.nacos.config.prefix.

Spring.profile.active is the profile corresponding to the current environment. For details, please refer to the Spring Boot document. Note: when spring.profile.active is empty, the corresponding connector - will not exist, and the splicing format of dataId becomes prefix. {file extension}.

File exception is the data format of the configuration content, which can be configured through the configuration item spring.cloud.nacos.config.file extension. Currently, only properties and yaml types are supported.

3. Create dataid

  • Click new

  • fill in

  • complete

2, Code

1.pom file

<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.application.yml

spring:
  profiles:
    active: dev

Priority loading order of configuration files in spring boot: bootstrap.yml > xxx.yml > xxx.properties

bootstrap.yml is loaded by the parent Spring ApplicationContext.

bootstrap.yml can be understood as some parameter configurations at the system level. These parameters are generally unchanged

Application.yml can be used to define the application level. If the file defined in application.yml is used with spring cloud config, dynamic replacement can be realized

3.bootstrap.yml

server:
  port: 9004
spring:
  application:
    name: config
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848
      config:
        server-addr: 192.168.31.100:8848
        file-extension: yaml

For example, the dataId is config-dev.yaml

The config here is spring.application.name=config

Dev here is spring.profiles.active=dev

Here, yaml is spring. Cloud. Config. File extension = yaml

4.controller

@RestController
@RefreshScope
public class ConfigController {
    @Value("${config.info}")
    private String info;
    @GetMapping("/config/info")
    public String info(){
        return info;
    }
}

5. Startup class

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }

}

6. Test

3, Configuration center classification management

  1. Default

    Namespace= public, Group= DEFAULT_GROUP, the default Cluster is default

  2. Namespace mainly implements isolation

    For example, we now have three environments: development, testing and production. We can create three namespaces. Different namespaces are isolated

  3. Group

    Group can divide different micro service services into the same group

  4. Service

    Service is a micro service; A Servicer can contain multiple clusters. The default Cluster of Nacos is default, and Cluster = is a virtual partition of the specified micro service

  5. Instance

    Examples of microservices

1.DataID

Specify spring.profile.active and the DataId of the configuration file to read different data in different environments

  • DataId of new test

  • Modify application.yml
spring:
  profiles:
    active: test
  • restart

2.Group scheme

  • Create two new group environments

  • Modify bootstrap.yml to the group of the test environment
server:
  port: 9004
spring:
  application:
    name: config
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848
      config:
        server-addr: 192.168.31.100:8848
        file-extension: yaml
        group: DEV_GROUP
  • restart

3.namespace

  • Create command space

  • View service list

  • dev create configuration

  • Modify bootstrap.yml
server:
  port: 9004
spring:
  application:
    name: config
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.100:8848
      config:
        server-addr: 192.168.31.100:8848
        file-extension: yaml
        group: TEST_GROUP
        namespace: 88f92e71-04fa-4c13-8892-736adb162f47
  • restart

Posted by Raider on Sun, 24 Oct 2021 20:00:04 -0700