Nacos-Unified Configuration Center

In addition to being a registration center, Nacos can also be used for configuration management.

Unified configuration management

When more and more instances of microservices are deployed, reaching dozens or hundreds, modifying the configuration of microservices one by one will make people crazy and error-prone. We need a unified configuration management solution that can centrally manage the configuration of all instances.
insert image description here
On the one hand, Nacos can centrally manage the configuration, and on the other hand, when the configuration changes, it can notify the microservice in time to realize hot update of the configuration.

1. Nacos write configuration file

insert image description here

insert image description here

Note: The core configuration of the project needs to be managed by nacos only when the hot update configuration is required. It is better to save some configurations that will not be changed locally in the microservice.

2. Microservice pull configuration

The microservice needs to pull the configuration managed in nacos and merge it with the local application.yml configuration to complete the project startup.

But if application.yml has not been read yet, how do you know the address of nacos?

Therefore, spring introduces a new configuration file: bootstrap.yaml file, which will be read before application.yml. The process is as follows:
insert image description here

(1) Introduce pom dependencies

<!--nacos配置管理依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

(2) Add bootstrap.yaml

spring:
  application:
    name: userservice # 服务名称
  profiles:
    active: dev #开发环境,这里是dev 
  cloud:
    nacos:
      server-addr: localhost:8848 # Nacos地址
      config:
        file-extension: yaml # 文件后缀名

Here, the nacos address will be obtained according to spring.cloud.nacos.server-addr, and then according to

${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}As a file id, to read the configuration.

In this example, it is to read userservice-dev.yaml:
insert image description here

(3) Read nacos configuration

Add business logic to UserController in user-service, read pattern.dateformat configuration:
insert image description here

3. Configure hot update

Our ultimate goal is to modify the configuration in nacos, so that the microservice can make the configuration take effect without restarting, that is, configuration hot update .
To achieve configuration hot update, two methods can be used:

(1) Method 1

Add the annotation @RefreshScope to the class where the variable injected by @Value is located:
insert image description here

(2) Method 2

Use the @ConfigurationProperties annotation instead of the @Value annotation.

In the user-service service, add a class to read the patternern.dateformat property:

package cn.itcast.user.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    
    
    private String dateformat;
}

Use this class instead of @Value in UserController:
insert image description here

4. Configure sharing

In fact, when the microservice starts, it will go to nacos to read multiple configuration files, for example:

  • [spring.application.name]-[spring.profiles.active].yaml, for example: userservice-dev.yaml

  • [spring.application.name].yaml, for example: userservice.yaml

Does not[spring.application.name].yaml contain environments, so can be shared by multiple environments.

The following test configuration sharing

(1) Add an environment sharing configuration

We add a userservice.yaml file in nacos:
insert image description here

(2) Read shared configuration in user-service

In the user-service service, modify the PatternProperties class to read the newly added properties:
insert image description here
In the user-service service, modify UserController and add a method:
insert image description here

(3) Configure the priority of sharing

When nacos and service local have the same attribute at the same time, the priority is divided into high and low:
insert image description here

Guess you like

Origin blog.csdn.net/me_1984/article/details/128749817
Recommended