Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

An overview of

Alibaba launched Nacos in July 2018, a dynamic service discovery, configuration management and service management platform that makes it easier to build cloud-native applications. It also said that it will complete the production of available version 0.8 in 6-8 months. The current version is version 0.9.

Nacos offers four main features

  • Service discovery and service health check

    Nacos makes it easier for services to register themselves and discover other services through DNS or HTTP interfaces. Nacos also provides real-time health checks of services to prevent requests from being sent to unhealthy hosts or service instances.

  • Dynamic configuration Management

    Dynamically configuring services allows you to manage the configuration of all services in a centralized and dynamic manner in all environments. Nacos eliminates the need to redeploy applications and services when the configuration is updated, making configuration changes more efficient and flexible.

  • Dynamic DNS Service

    The dynamic DNS service supports weighted routing, enabling you to implement load balancing at the middle layer, flexible routing policies, traffic control, and simple DNS resolution services on the data center Intranet. Dynamic DNS services also make it easier for you to implement DNS protocol-based service discovery to help eliminate the risk of coupling to vendor-proprietary service discovery apis.

  • Service and metadata management

    Nacos provides easy-to-use service dashboards that help you manage service metadata, configuration, Kubernetes DNS, service health and metrics statistics.

The installation

Nacos can be installed in either of the following ways:

  • 1. Download the stable version from the official website and decompress it for use.
  • 2. Download the source code to compile and use, the latest version is 0.9.0.

This article uses the first method by downloading the stable version of Nacos at github.com/alibaba/nac…

Unzip nacos-server-0.9.0.zip or tar -xvf nacos-server-0.9.0.tar.gzCopy the code

Go to the bin directory and run the following command to start Nacos.

Sh -m standalone #Windows CMD startupCopy the code

Start after the success, visit Nacos service, http://localhost:8848/nacos/#/login, the default user name password is Nacos, login page as shown.

The following figure is displayed after login.

SpringBoot is managed using Nacos configuration

Create a SpringBoot project with the following main code.

pom.xml

<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> The < version > 0.2.1 < / version > < / dependency >Copy the code

application.yml

Spring: Application: name: SpringCloud-nacos-hello nacos: config: server-addr: 127.0.0.1:8848Copy the code

Configuration description:

  • Spring.application. name: indicates the configuration application name.
  • Nacos.config. server-addr: indicates the IP address of the nacos server.

Start the class

In the startup class, add the @nacosPropertysource annotation which contains two properties as follows:

  • DataId: This attribute is the Data Id to be configured in Nacos.
  • AutoRefreshed: Enable automatic updates for true.

After using Nacos as the configuration center, you need to use the @nacosValue annotation to get the configuration in the same way as @Value. The complete startup class code is shown below.

@SpringBootApplication @NacosPropertySource(dataId = "springcloud-nacos-hello", autoRefreshed = true) @RestController public class SpringcloudNacosHelloApplication { public static void main(String[] args) { SpringApplication.run( SpringcloudNacosHelloApplication.class, args ); } @NacosValue(value = "${test.properties.useLocalCache:false}", autoRefreshed = true) private boolean useLocalCache; @GetMapping("/get") public boolean get(){ return useLocalCache; }}Copy the code

Start the application, visit http://localhost:8080/get, return to the configuration of the default value is “false”

Use Nacos to modify the configuration

Add the service with the data ID you just created and change the configuration from false to true, as shown in the figure.

After publishing, the configuration list is returned and the newly added configuration appears, as shown in the figure.

Visit http://localhost:8080/get again, the return value is “true”.

The data source

After some simple operations above, we can now use the Nacos configuration center normally.

But have you ever wondered: where is the configuration data stored?

We didn’t do any configuration on Nacos Server, so there are only two places to store data:

  • memory
  • Local database

When you restart Nacos Server, you will find that the previously created configuration remains, indicating that it is not stored in memory.

When you open NACOS_PATH/data, you’ll find a derby-data directory. Derby is a database written in Java that is an open source project of Apache. Our configuration data is now stored in this library.

We are not familiar with Derby, but the data source can be changed to MySQL, which we are familiar with. The detailed procedure is as follows.

    1. Create a database named nacOS_config.
    1. Import the table structure from NACOS_PATH/conf/ nacOS-mysql.sql into the library you just created.
    1. Modify NACOS_PATH/conf/application. The properties files, increase support for mysql data source configuration (currently only supports mysql), add a mysql data source url, user name and password.
Spring. The datasource. Platform = mysql db. Num = 1 db. Url. 0 = JDBC: mysql: / / 127.0.0.1:3306 / nacos_config? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=rootCopy the code

When nacOS is started in standalone mode, all data written to the embedded database in NACOS is written to mysql.

Here SpringBoot is done using the Nacos configuration center. For more information about Nacos and how to use it, see the official documentation.