springboot2.X integrates nacos to realize registration center and configuration center

nacos official manual https://nacos.io/zh-cn/docs/what-is-nacos.html

1. Environmental preparation

This article uses spring boot 2 1.9. RELEASE,nacos1.3.2 ,JDK 1.8+ ,mysql5.6.5 + this paper adopts Windows deployment environment

2. Download the source code or installation package

You can get Nacos through source code and distribution package.

1. Download the source code from Github 2. Download the compressed package after compilation

For the convenience of this article, download the zip package and unzip it. Address: Latest stable version Download Nacos server - $version Zip package.

3. Start the server

Nacos supports three deployment modes

  • Stand alone mode - used for testing and stand-alone trial.
  • Cluster mode - used in production environment to ensure high availability.
  • Multi cluster mode - used in multi data center scenarios.
  1. Stand alone deployment:

Unzip nacos-server-1.3.2 Zip package, go to nacos\conf, and you can see the mysql initialization script. You can refer to the selection of Nacos data storage mode https://blog.csdn.net/thinkingcao/article/details/105444718 , because this paper adopts single machine deployment, for the convenience of data viewing, mysql database is used to store data.

Copy and execute Nacos mysql SQL script creates and initializes the database. Modify application The properties configuration file configures database connection information

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:

### Count of DB:

### Connect URL of DB:

nacos is started by cluster by default. You can modify startup CMD configuration file is in stand-alone startup mode: stand-alone stands for stand-alone mode and non cluster mode

Double click Startup CMD start the nacos server. visit http://localhost:8848/nacos/index.html , login default account: nacos, password: nacos

Nacos configuration


nacos uses namespace for environment isolation, which can specify different environments to better manage the configuration file management of development, testing and production

Resource allocation

Data ID

The format of Data ID is as follows:

reference resources: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html


  • prefix defaults to spring application. The value of name can also be set through the configuration item spring cloud. nacos. config. prefix to configure.

  • spring.profile.active is the profile corresponding to the current environment. Note: when spring profile. When active is empty, the corresponding connector - will not exist, and the splicing format of dataId will become ${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.


    The default is DEFAULT_GROUP, you can group and manage different types of micro service configuration files. Configuration file through spring cloud. nacos. config. Group = AAA.

Configuration content

The configuration file format supports several types of TEXT, JSON, XML, YAML, HTML and Properties

Historical version

Each modification of the resource file will record a historical version. The default saving time of the historical record is 30 days. You can see the content of each update according to the historical record. You can also roll back the specified log file to the previous version.

Listening query

You can listen to which ip addresses access each specific resource file

2. Cluster deployment:

Modify the configuration file cluster Conf.example , rename to cluster Conf and modify the content

Note: ip is required for Windows deployment.   

Just start.

4. Spring boot integrates nacos as the registration center and configuration center

  • Add dependency to pom file

    <!--Spring cloud alibaba 2.1.0.RELEASE-->

    <!--web + actuator-->
  • Create a new bootstrp. In the resources directory Properties file

Note: the bootstrap is loaded by the parent Spring ApplicationContext. The parent ApplicationContext is loaded before using the application. When using Spring Cloud, the configuration information is usually loaded from the config server. In order to obtain the configuration information (such as password), you need some early or boot configuration. Therefore, put the config server information in the bootstrap to load the really needed configuration information.

  port: 8080
# nacos configuration
    #The reason why you need to configure spring application. Name, because it is part of the Nacos configuration management dataId field.
    name: nacos-config
        server-addr: #Nacos service registry address
        namespace: e5b58bd9-88b8-4acd-a81f-55c0e53426b4  # Default public namespace when namespace is not specified
        server-addr: #Nacos as configuration center address
        file-extension: yaml #Specifies the configuration of yaml format
        group: DEFAULT_GROUP
        namespace: e5b58bd9-88b8-4acd-a81f-55c0e53426b4 # Default public namespace when namespace is not specified

# Naming rules for configuration files in Nacos
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml this file name is the file name of the new configuration file in Nacos

* application found during use YML and bootstrap YML has the problem of attribute coverage, that is, when the same attribute is configured in both configuration files, the application configuration will overwrite the bootstrap attribute value. During configuration, pay more attention to the bifurcation and avoid repeated configuration.

  • Add annotation information to startup class @ EnableDiscoveryClient
public class Application {
    public static void main(String [] args){
        SpringApplication.run(Application.class, args);

To make the configuration file effective in real time, you need to add comments: @ RefreshScope

*When the annotation @ RefreshScope is not added during use, the dynamic loading of configuration can also be realized, and the specific situation is treated on a case by case basis.

public class TestNacosControlle {



Tags: Nacos

Posted by gotDNS on Sat, 07 May 2022 12:36:33 +0300