Previously on:

  • Spring Cloud Alibaba Basics: Service Registration and Discovery using Nacos
  • Spring Cloud Alibaba Basic Tutorial: Several service Consumption Modes supported
  • Spring Cloud Alibaba Basics: Using Nacos as the Configuration Center
  • Spring Cloud Alibaba Basic Tutorial: Details of Nacos Configuration loading rules
  • Spring Cloud Alibaba Basics: Managing Multiple Environments for Nacos Configurations

In previous posts on the use of Nacos as a configuration hub, we have covered how to create configuration content in Nacos, the mapping between Nacos configuration content and Spring application configuration, and configuration management solutions in multiple environments.

However, we often encounter such problems in the actual application process: Sometimes we split the configuration of an application according to its specific function and store it in different configuration files. In addition to categorizing different configurations, the configuration can be easily shared among different applications. Nacos also supports this requirement, so let’s take a look at how to load multiple configurations and how to share configurations when using Nacos.

Loading multiple configurations

From previous studies, we have learned that the mapping between Spring applications and configuration content in Nacos is controlled by the following three parameters:

  • spring.cloud.nacos.config.prefix
  • spring.cloud.nacos.config.file-extension
  • spring.cloud.nacos.config.group

By default, the configuration of Data ID=${spring.application.name}. Properties, Group=DEFAULT_GROUP is loaded.

Assume that there is a requirement: we want to have unified configuration management for all applications’ Actuator modules and log output. Therefore, we hope that the configuration of the Actuator module can be placed in a separate configuration file, and the configuration of the log output can be placed in a separate configuration file, log.properties. By splitting the two types of configuration, you can load the configuration in a shared manner and manage the configuration in a unified manner.

At this point, we only need to do the following two steps to achieve this requirement:

Properties, Group=DEFAULT_GROUP and Data ID=log.properties, Group=DEFAULT_GROUP.

Step 2: in the Spring Cloud applications through the use of the Spring. The Cloud. Nacos. Config. Ext – config parameters to configure to load the content of the two configurations, such as:

spring.cloud.nacos.config.ext-config[0].data-id=actuator.properties
spring.cloud.nacos.config.ext-config[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[0].refresh=true
spring.cloud.nacos.config.ext-config[1].data-id=log.properties
spring.cloud.nacos.config.ext-config[1].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[1].refresh=true
Copy the code

As you can see, spring. Cloud. Nacos. Config. Ext – config configuration is an array type List. Each configuration contains three parameters: data-id, group, and refresh. The refresh parameter controls the contents of this configuration file to support automatic refresh. By default, only the default loaded configuration is automatically refreshed. For the extended configuration loaded content, the automatic refresh is implemented only when the Settings are configured.

Shared configuration

By loading multiple configurations above, we can actually implement shared configurations for different applications. However, Nacos provides another convenient configuration method, such as the following Settings are equivalent to those used above:

spring.cloud.nacos.config.shared-dataids=actuator.properties,log.properties
spring.cloud.nacos.config.refreshable-dataids=actuator.properties,log.properties
Copy the code
  • spring.cloud.nacos.config.shared-dataidsParameter is used to configure multiple shared configurationsData IdIf there are more than one, separate them with commas
  • spring.cloud.nacos.config.refreshable-dataidsParameters are used to define which share configurationsData IdWhen the configuration changes, the application can dynamically refresh, multipleData IdSeparate them with commas. If the configuration is not specified, all share configurations do not support dynamic refreshing by default

Set the loading priority

When we load multiple configurations, if the same key exists, we need to understand the configuration loading priority relationship.

When using Nacos configuration, there are three main types of configuration:

  • A: byspring.cloud.nacos.config.shared-dataidsShare configuration defined
  • B: byspring.cloud.nacos.config.ext-config[n]Load configuration defined
  • C: By internal rules (spring.cloud.nacos.config.prefix,spring.cloud.nacos.config.file-extension,spring.cloud.nacos.config.groupThe configuration of these parameters is concatenated

To find out the order in which these configurations are loaded, as can be clearly seen from the log, we can do a simple experiment:

spring.cloud.nacos.config.ext-config[0].data-id=actuator.properties
spring.cloud.nacos.config.ext-config[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[0].refresh=true

spring.cloud.nacos.config.shared-dataids=log.properties
spring.cloud.nacos.config.refreshable-dataids=log.properties
Copy the code

According to the above configuration, the application will load three different configuration files. When the application is started, the following output will be displayed in the log:

The 2019-02-08 21:23:02. 63804-665 the INFO [main] O.S.C.A.N.C.N acosPropertySourceBuilder: Loading nacos data, dataId:'log.properties', group: 'DEFAULT_GROUP'The 2019-02-08 21:23:02. 63804-671 the INFO [main] O.S.C.A.N.C.N acosPropertySourceBuilder: Loading nacos data, dataId:'actuator.properties', group: 'DEFAULT_GROUP'The 2019-02-08 21:23:02. 63804-677 the INFO [main] O.S.C.A.N.C.N acosPropertySourceBuilder: Loading nacos data, dataId:'alibaba-nacos-config-client.properties', group: 'DEFAULT_GROUP'
Copy the code

The configuration loaded later overrides the configuration loaded earlier, so the priority relationship is as follows: A < B < C

The resources

  • Nacos official documentation

Code sample

Readers of the sample article can check out the Alibaba-Nacos-config-client project in the following repository:

  • Making:Github.com/dyc87112/Sp…
  • Gitee:Gitee.com/didispace/S…

If you are interested in these, welcome to star, follow, favorites, forward to give support!

The following tutorials may be of interest to you

  • Spring Boot Basics tutorial
  • Spring Cloud Basics tutorial