Java's SpringCloud Alibaba [3] [Microservice Nacos-config Configuration Center]

1. Nacos-config Configuration Center

1. Official documents

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 Confg, you can centrally manage
the external property configuration of your Spring Cloud application in Nacos Server.
1. Maintainability 2. Timeliness 3. Security
insert image description here
Springcloud config compares
three major advantages

  • Most application scenarios of springcloud config are used in conjunction with git, and dynamic changes also need to rely on the Spring Cloud Bus message bus to pass all client changes
  • springcloud config is used by GIT in most scenarios, and dynamic changes also require the SpringCloud Bus message bus to pass through all client changes
  • nacos config uses long polling to update the configuration. Once the configuration changes, the process of notifying the Provider is very fast, killing the original config of springcloud by a few blocks in terms of speed.
    insert image description here

2. Quick configuration

Visit: http://192.168.180.128:8847/nacos/index.html#/login

1. Basic operation of Nacos-config

New configuration

insert image description here
insert image description here
Click to publish
insert image description here
insert image description here

edit configuration
insert image description here
insert image description here
insert image description here

View modified history information

insert image description here
check the details

insert image description here
insert image description here
operations such as details and rollback

insert image description here

listen query

insert image description here

Create new namespace

insert image description here

insert image description here
insert image description here

insert image description here

Clone configuration files into other namespaces
insert image description here
insert image description here
User management & role management
insert image description here
Add users
insert image description here
insert image description here
Add roles
insert image description here
insert image description here
insert image description here
Rights management
insert image description here
insert image description here
We set up the logout system
insert image description here

insert image description here

2. Use of Nacos Configuration Center

Nacos server initialization
1. Start Nacos Server. The startup method can be found on the Nacos official website.
2. After starting Nacos, add the following configuration to Nacos

Data ID: nacos-config.properties
Group : DEFAULT_GROUP
Configuration format: Properties
Configuration content:
user.name = Xu Shu2
user.age = 11
insert image description here
Note Note that dataid is the extension of properties (the default file extension method)

How to use the client
If you want to use Nacos in your project to realize the external configuration of the application, use the starter whose group ID is com.alibaba.cloud and artfact ID is spring-cloud-starter-alibaba-nacos-config

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

Now you can create a standard SpringBoot application
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here


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

insert image description here
insert image description here

server:
  port: 8050
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Nacos config 依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

insert image description here

@SpringBootApplication
public class ConfigApplication {
    
    
    public static void main(String[] args) {
    
    
        ConfigurableApplicationContext applicationContext = SpringApplication.run(ConfigApplication.class, args);
        String userName = applicationContext.getEnvironment().getProperty("user.name");
        String userAge = applicationContext.getEnvironment().getProperty("user.age");
        System.out.println("user name :"+userName +"; age: "+userAge);
    }
}

Before running this Example, you must use the bootstarp.properties configuration file to configure the Nacos Server address, for example
insert image description here
insert image description here

spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      server-addr: 192.168.180.128:8847

insert image description here
insert image description here
insert image description here

insert image description here
insert image description here
insert image description here

3. The Nacos client goes to the registration center every 10ms to judge according to MD5

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

4. Based on the file extension method with dataid as yaml

spring-cloud-starter-alibaba-nacos-config also perfectly supports the yaml format. At this time, you only need to complete the following two steps:

1. The declared dataid file extension displayed in the application's bootstrap.properties configuration file.
bootstrap.properties as shown below

spring.cloud.nacos.config.file-extension=yaml

insert image description here
insert image description here

user:
    name: 徐庶-yaml
    age: 11

insert image description here
We found that the configuration information could not be obtained. We
insert image description here
need to modify the corresponding configuration information and set the corresponding read file extension.
insert image description here

      # Nacos 客户端 默认是Properties 的文件扩展名  file-extension进行设置
      config:
        file-extension: yaml

insert image description here

      # Nacos 客户端 默认是Properties 的文件扩展名  file-extension进行设置
      config:
        file-extension: yaml
        #refresh-enabled: false nacos客户端将无法感知配置的变化

Re-run the project
insert image description here
and set the application.yml configuration file
application-dev.yml for multiple environments
insert image description here
insert image description here
insert image description here
insert image description here
. Among the configuration information output above, the user name is the configuration information just now, but the age is still the old configuration information.
insert image description here
Set the namespace to dev
insert image description here

        namespace: dev

insert image description here
We can see that no configuration information can be read, because we have not set the configuration information in the dev environment.
insert image description here
insert image description here
insert image description here
The clone is successful
insert image description here

5. Support custom group configuration

${spring.cloud.nacos.config.group}The default is used when no configuration is explicitly specified DEFAULT_GROUP. If you need to customize your own Group, you can do it through the following configuration:

spring.cloud.nacos.config.group=DEVELOP_GROUP

insert image description here
insert image description here

        group: itbluebox

The corresponding information cannot be read.
insert image description here
Cloning and setting the Group
insert image description here
insert image description here
can continue to read the corresponding configuration information
insert image description here

6. Support custom extended Data id configuration

Spring Cloud Alibaba Nacos Config can support custom Data ld configuration from version 0.2.1. For the detailed design of this part, please refer to here. A complete configuration example is as follows:
insert image description here
insert image description here
insert image description here
insert image description here

String userConfig = applicationContext.getEnvironment().getProperty("user.config");
            System.out.println("user name :"+userName +"; age: "+userAge + "; userConfig:"+userConfig);

insert image description here

group: itbluebox
        shared-configs:
          - data-id: com.itblueboxmall.common.properties
            refresh: true

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

            #group:默认是Default-group
          - data-id: com.itblueboxmall.common2.properties
            refresh: true

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

            #group:默认是Default-group
        extension-configs[0]:
          data-id: com.itblueboxmall.common03.properties
          refresh: true

insert image description here

7. @RefreshScope [Set dynamic perception corresponding to configuration changes]

The @Value annotation can get the value of the configuration center, but it cannot dynamically perceive the modified value. You need to use the @RefreshScope annotation

insert image description here
insert image description here

@RestController
@RequestMapping("/config")
public class ConfigController {
    
    

    @Value("${user.name}")
    public String username;

    @RequestMapping("/show")
    public String show(){
    
    
        return this.username;
    }
}

insert image description here
insert image description here

http://localhost:8050/config/show
insert image description here
insert image description here
insert image description here
The content output by the console has changed.
insert image description here
Visiting the corresponding interface again does not change
http://localhost:8050/config/show
insert image description here
setting dynamic perception corresponding configuration changes
insert image description here

@RefreshScope

Restart the running project
insert image description here
and modify the configuration information.
insert image description here
insert image description here
The console output content has changed.
insert image description here
Visit: http://localhost:8050/config/show
insert image description here

Guess you like

Origin blog.csdn.net/qq_44757034/article/details/131527250
Recommended