The default is a Spring Cloud project to describe usage

Version used

  • Nacos 1.4.2
  • Spring – the boot: 2.1 x
  • spring-cloud:Greenwich.RELEASE
  • Spring – the cloud – alibaba: 2.1 x
  • Java: 1.8 +

Nacos server

The server is equivalent to a collection of Euraka and Spring-Cloud-Config. The Nacos Web page, service registration, and configuration are all on the server

The registry

Registries are similar to Eureka, except that Nacos provides a different page, but it is more convenient to use the page in service details to control the service up and down. Like the configuration center, you can also use Group+Namespace to manage the service. Nacos’s service calls support Feign and Dubbo

Configuration center

Nacos supports configuration management of Spring, spring-Boot, and spring-Cloud.

The Nacos service configuration must be written in bootstrap.properties/ yML. By default, Nacos will read the configuration starting with the service name, where the unique ID is dataId.

In Nacos Spring Cloud, the full format of dataId is as follows:

${prefix}-${spring.profiles.active}.${file-extension}
Copy the code
  • prefixThe default isspring.application.nameIs also available through the configuration itemspring.cloud.nacos.config.prefixTo configure.
  • spring.profiles.activeIs the profile corresponding to the current environment. For details, seeSpring Boot document.Note: whenspring.profiles.activeIf null, the corresponding concatenate-Also will not exist, the dataId splicing format becomes${prefix}.${file-extension}
  • file-exetensionFor the data format of the configuration content, you can pass the configuration itemspring.cloud.nacos.config.file-extensionTo configure. Currently only supportedpropertiesyamlType.

In addition to the configuration file starting with the service name, Nacos also supports by spring. Cloud. Nacos. Config. The Shared – configs [n]. The data – id and spring. Cloud. Nacos. Config. The extension – configs [n]. Add data – id With additional configuration, they are loaded in the following order

Configure the read order

Spring Cloud Alibaba Nacos Config currently offers three configuration capabilities to pull relevant configurations from Nacos.

  • A: byspring.cloud.nacos.config.shared-configs[n].data-idMultiple shared Data ids can be configured
  • B: byspring.cloud.nacos.config.extension-configs[n].data-idSupports multiple extended Data Id configurations
  • C: Data Id configurations are automatically generated based on internal rules (application name, application name + Profile)

When the three approaches are used together, one of their priorities is :A < B < C

See: github.com/alibaba/spr…

For spring-boot Configuration, see Externalized Configuration. For bootstrap.properties/ yML, see application-Context

Configuration management

Nacos uses groups and namespaces to manage configuration files. Each configuration file has a Group and a Namespace attribute, including a Group under a Namespace and a configuration file under a Group. You can use these two attributes to distinguish between different environments and scenarios

Build Nacos project

Introducing spring-cloud-Alibaba version dependencies

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

The version depends on the spring-Boot version. You can view the release version depending on the version, as shown in the following figure

You can also see this at github.com/alibaba/spr…

Server usage

See Nacos official

The current project is a direct download of Nacos stable version of the compressed package

And use (Linux)

sh bin/startup.sh -m standalone
Copy the code

Standalone starts on a standalone device, and if you do not add -m standalone, the standalone mode is clustered by default

Start the conf/application configuration file. The properties, can be in the database, the configuration file stored in the database, using database need to initialize the database, database scripts conf/nacos – mysql. SQL

The Nacos login page is displayed when you visit http://ip:8848/nacos. The default login account name and password Nacos Nacos

Registry Use

Introduction of depend on

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code

Add registry Settings to bootstrap.yml or bootstrap.properties

# Nacos address
spring.cloud.nacos.discovery.server-addr=Nacos - server address
# namespace, optional, default public
spring.cloud.nacos.discovery.namespace=NACOS namespace
Copy the code

To use Feign in the same way as euraka used before, Dubbo can see nacos. IO /zh-cn/docs/…

Configuration center use

Introduction of depend on

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

Add configuration center Settings to bootstrap.yml or bootstrap.properties

spring.cloud.nacos.config.server-addr=Nacos - server address
# This is a must
spring.application.name=example
Copy the code

Use this if you don’t need extra configuration

If you need to add additional configurations, here are just a few of the configurations that are used most frequently in projects

You can refer to the sequence of reading configurations

This configuration only affects the default configuration (i.e., service name + environment + file suffix)
spring.cloud.nacos.config.file-extension=properties
This configuration only affects the default configuration (i.e., service name + environment + file suffix)
spring.cloud.nacos.config.group=APPLICATION_GROUP
# share configuration (DEFAULT_GROUP)
spring.cloud.nacos.config.shared-configs=application.properties
# NACOS namespace
spring.cloud.nacos.config.namespace=Namespace of NACOS
# Extra configuration
spring.cloud.nacos.config.extension-configs[0].data-id=xxxxx.properties
The default value is false
spring.cloud.nacos.config.extension-configs[0].refresh=true
# group (default is DEFAULT_GROUP, can not write
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[1].data-id=xxx.properties
spring.cloud.nacos.config.extension-configs[1].refresh=true
Copy the code

Dynamic refresh

Dynamic refreshes can be achieved by using the @RefeshScope annotation in a project (Spring Cloud project)

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

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get(a) {
        returnuseLocalCache; }}Copy the code

After changing useLocalCache to true on nacOS and calling the interface, you can see that the interface returns true

Use this annotation to introduce dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-context</artifactId>
</dependency>
Copy the code

Importing Configuration in Batches

Nacos supports bulk configuration import in two ways that I know of so far

  • Export the Nacos database directly to SQL, and then import another database
  • Use the import configuration function on the page

Here only the second method is described. Choose Configuration Management > Configuration List to import the namespace

Select upload files, Nacos currently only seems to support ZIP packages

The configuration will be placed in a folder, the folder name is the imported Group name, so the imported structure

├ ─ imp (1), ├ ─ imp (2

Then compress it into a ZIP file and import it

Project Application Description

Currently, Nacos is used as the configuration center and registry center in the project, and only one bootstrap.properties file is left locally. Other configurations are managed by Nacos, and the command space of Nacos is used to manage configurations of different environments. Groups are used to distinguish public files from service configuration files, such as APPLICATION_GROUP and DEFAULT_GROUP. Registries are currently simple registration functions, while Nacos currently uses standalone +mysql

The appendix

Nacos GitHub

Nacos document

Spring Cloud Alibaba GitHub