Spring cloud Alibaba: multi file loading and sharing configuration of Nacos configuration
1, Load multiple configurations
We already know that the correspondence between the Spring application and the 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, Data ID=${spring.application.name}.properties, group = default will be loaded_ Group configuration.
Suppose there is such a requirement: we want to do unified configuration management for the Actuator module and log output of all applications. Therefore, we hope that the configuration of the Actuator module can be placed in a separate configuration file actuator.properties, and the configuration of log output can be placed in a separate configuration file log.properties. By splitting these two types of configuration content, we hope to achieve shared loading and unified management of configuration.
1. Solutions
Step 1: create data id = actor.properties, group = Default in Nacos_ Group and Data ID=log.properties, Group=DEFAULT_GROUP configuration content.
We will load these three configuration files here
Step 2: in the Spring Cloud application, configure the two configuration contents to be loaded by using the spring.cloud.nacos.config.ext-config parameter, such as:
- properties format
server.port=9004 spring.application.name=config spring.cloud.nacos.discovery.server-addr=192.168.31.100:8848 spring.cloud.nacos.config.server-addr=192.168.31.100:8848 spring.cloud.nacos.config.file-extension=yaml 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
- yml format
server: port: 9004 spring: application: name: config cloud: nacos: config: ext-config: - data-id: actuator.properties group: DEFAULT_GROUP refresh: true - data-id: log.properties group: DEFAULT_GROUP refresh: true file-extension: yaml server-addr: 192.168.31.100:8848 discovery: server-addr: 192.168.31.100:8848
As you can see, the spring.cloud.nacos.config.ext-config configuration is an array List type. Each configuration contains three parameters: data ID, group and refresh; The first two do not need to be repeated. They correspond to the configurations created in Nacos. The refresh parameter controls that the contents in this configuration file support automatic refresh. By default, only the default loaded configurations will be refreshed automatically. For these extended configurations, the automatic refresh will be realized when the settings need to be configured.
Note: this configuration must be loaded in the bootstrap.properties file
Step 3: start the test
Request address: 127.0.0.1:9004/config/info Response result: config-test.yml:there is test version=1.0 actuator.properties:"hello actuator" log.properties:"hello log"
2, Shared configuration
By loading multiple configurations above, we can actually share configurations for different applications. However, Nacos also provides another convenient configuration method. For example, the following settings are equivalent to the configuration content used above:
spring.cloud.nacos.config.shared-dataids=actuator.properties,log.properties spring.cloud.nacos.config.refreshable-dataids=actuator.properties,log.properties
- The spring.cloud.nacos.config.shared-dataids parameter is used to configure the data IDs of multiple shared configurations, which are separated by commas
- The spring.cloud.nacos.config.refreshable-dataids parameter is used to define which shared configuration data IDS can be refreshed dynamically in the application when the configuration changes, and multiple data IDs are separated by commas. If there is no explicit configuration, all shared configurations do not support dynamic refresh by default
3, Configure load priority
When we load multiple configurations, if the same key exists, we need to deeply understand the priority relationship of configuration loading.
When using Nacos configuration, there are three main types of configuration:
- A: Share configuration defined through spring.cloud.nacos.config.shared-dataids
- B: Load configuration defined through spring. Cloud. Nacos. Config. Ext config [n]
- C: Configurations spliced by internal rules (spring.cloud.nacos.config.prefix, spring.cloud.nacos.config.file-extension, spring.cloud.nacos.config.group)
Modify the bootstrap.properties configuration file
server.port=9004 spring.application.name=config spring.cloud.nacos.discovery.server-addr=192.168.31.100:8848 spring.cloud.nacos.config.server-addr=192.168.31.100:8848 spring.cloud.nacos.config.file-extension=yaml 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 spring.cloud.nacos.config.shared-dataids=log.properties spring.cloud.nacos.config.refreshable-dataids=log.properties
According to the above configuration, the application will load three different configuration files. When starting the application, you will see the following output in the log:
2021-10-25 16:31:20.897 INFO 14456 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'log.properties', group: 'DEFAULT_GROUP' 2021-10-25 16:31:20.902 INFO 14456 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'actuator.properties', group: 'DEFAULT_GROUP' 2021-10-25 16:31:20.910 INFO 14456 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'config-test.yaml', group: 'DEFAULT_GROUP'
Conclusion: the configuration loaded later will overwrite the configuration loaded earlier, so the priority relationship is: a < B < C