01 Nacos unified configuration management

Nacos implements configuration management

① Add profile in Nacos

② Introducing the config dependency of nacos into microservices

③ Add a bootstrap. In the microservice YML, configure the nacos address, current environment, service name, file suffix and namespace. These determine which file to read from nacos when the program starts

Add configuration information to Nacos

Process of configuration acquisition

Configuration file reading process without Nacos

Reading process after being handed over to Nacos for unified configuration management

Operation steps

Introducing the dependency of Nacos configuration management client

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

Create a new bootstrap in the resource directory of the service yml

bootstrap. The YML file is a boot file with priority over application YML, in bootstrap The content configured in YML is in application There is no need to configure again in YML, bootstrap The contents of YML are as follows:

spring:
  application:
    name: userservice # Service name
  profiles:
    active: dev # development environment 
  cloud:
    nacos:
      config:
        file-extension: yaml # file extension
        server-addr: localhost:8848
        namespace: ad04ea8d-50ee-4dd6-bf38-a71f8337c117 #If you want to fill in the id of the namespace under the configuration, otherwise you cannot read the bootstrap Configuration of YML

Verify that the configuration is pulled from Nacos

Set pattern The dateformat attribute is injected into the UserController for testing:

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${pattern.dateformat}")
    private String dateformat;

    @GetMapping("/now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat, Locale.CHINA));
    }
}

Nacos configuration hot update

After the Nacos configuration is changed, the micro service can realize hot update in the following ways:

① Refresh through @ Value annotation injection and @ RefreshScope

② Automatically refresh through @ ConfigurationProperties injection

matters needing attention:

  • Not all configurations are suitable to be placed in the configuration center, which is cumbersome to maintain
  • It is recommended to put some key parameters and parameters that need to be adjusted at runtime into the nacos configuration center, which are generally user-defined configurations

After the configuration file in Nacos is changed, the micro service can be perceived without restarting. This can be achieved through the following two configurations:

Add the annotation @ RefreshScope on the class of the variable injected by @ Value

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    //Configuration properties managed by nacos
    @Value("${pattern.dateformat}")
    private String dateformat;

    @GetMapping("/now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat, Locale.CHINA));
    }
}

Use @ ConfigurationProperties annotation (recommended)

Create a new configuration class PatternProperties

@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    private String dateformat;
}
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private PatternProperties properties;

    @GetMapping("/now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat(), Locale.CHINA));
    }
}

Nacos multi environment configuration sharing

The micro service will read the following configuration files from nacos:

  • [service name] - [spring. Profile. Active] Yaml, environment configuration
  • [service name] yaml, default configuration, multi environment sharing

Priority:

  1. [service name] - [environment] yaml
  2. [service name] yaml
  3. Local configuration

Nacos new multi environment sharing configuration

Modify configuration class

@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    private String dateformat;	
    private String envSharedValue;	//Multi environment sharing configuration
}

Add test interface

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private PatternProperties properties;

    @GetMapping("/prop")
    public PatternProperties properties(){
        return properties;
    }
}

Verification results

Keywords: Java Spring Boot Spring Cloud

Added by sglane on Fri, 04 Feb 2022 07:49:09 +0200