Nacos enables environmental isolation

Posted by phpBuddy on Tue, 08 Feb 2022 18:24:35 +0100

1, Construction of project basic configuration

1. Create a configuration center client project

Introducing nacos dependency

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

2. Create two profiles

bootstrap.yml and application yml

Bootstrap. In SpringBoot project YML file loading priority is higher than application yml . nacos also uses bootstrap YML obtains the configuration from the configuration center and then combines it with application Start the project with the configuration item in YML.

1)bootstrap.yml

server:
  port: 3377
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #nacos service registry address
      config:
        server-addr: 127.0.0.1:8848 #nacos service configuration center address
        file-extension: yaml #Specifies that the read is configured in yaml format
  1. application.yml
spring:
      profiles:
        active: dev # Represents the development environment

Server configuration Data Id:
{spring.application.name}-{spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

That is, the value of the Data Id configured by the Nacos server is: nacos-config-client-dev.yaml

3. Write the main startup class

Add the annotation @ EnableDiscoveryClient to start the service registration discovery function.

package com.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientMain.class, args);
    }
}

4. Write business class to obtain configuration

Create the controller class, get the information in the configuration list, add the annotation @ RefreshScope, and realize the function of dynamically refreshing the configuration.

package com.nacos.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //Support the dynamic refresh function of Nacos.
public class ConfigClientController {

    @Value("${config.info}")
    public String info;

    @GetMapping("/get/configinfo")
    public String getPayment() {
        return "name:" + info;
    }
}

5.nacos server configuration

Add a new configuration under the public namespace under the nacos configuration list menu

New configuration (+)

6. Start the client and test it

The results are as follows: our configuration has been successfully read

7. Modify the configuration information to realize dynamic refresh

Visit again http://localhost:3377/get/configinfo , you can see that the latest configuration information has been read!

2, Read the configuration of different environments according to the suffix

1. Modify the configuration file

Modify application YML file spring profiles. The active value is test

2. Create new configuration information

Create configuration information of test environment in nacos

3. Restart the client

After configuration, restart the client and access http://localhost:3377/get/configinfo

Note: for example, the name of a service is order service, the registered test environment is order service, and the name of the locally started service is order service
Local order service. In this way, the exposed service is called order service, and the service of the test environment can be called locally
Try, but you can't adjust the local service because the service name is different

3, Read the configuration of different environments according to groups

1. Modify bootstrap Specify group, YML

Add spring cloud. nacos. config. Group configuration item, which specifies that it is grouped as PROD_GROUP

2. New server configuration

3. Restart the client

visit http://localhost:3377/get/configinfo

4, Read the configuration of different environments according to the namespace

1.nacos server creates a new namespace


You can see the namespace just created in configuration management.

2. Create a new configuration in the test environment and group it into TEST_GROUP

3. Modify bootstrap YML, add namespace

In bootstrap Add spring.com to the YML file cloud. nacos. config. Namespace key, whose value is the namespace id automatically generated when creating a new configuration

4. Restart the client

5, Explain

For example, the name of a service is order service, the registered test environment is order service, and the name of the locally started service is local order service. The service of the test environment can be called locally, but the test cannot call the local service because the service names are different. This enables isolation between services in different environments.

Topics: Spring Cloud