When developing microservices with SpringCloud, you often encounter relatively small backend parameter configurations that are not large enough to be stored in a separate table and that are read by other services. For example, IP whitelisting. In this case, it is easier to use Nacos to save and read them.
- Nacos version 2.1.xRELEASE corresponds to Spring Boot version 2.1.x.
- Nacos version 2.0.xRELEASE corresponds to Spring Boot version 2.0.x.
- Nacos version 1.5.xRELEASE corresponds to Spring Boot version 1.5.x.
Nacos server configuration
Configure the address and application name of the Nacos server in the microservice configuration.
The reason you need to configure
spring.application.nameis because it is part of the
dataIdfield that makes up the Nacos configuration management.
In Nacos Spring Cloud, the full format of
dataId is as follows.
prefix defaults to the value of
spring.application.name, and can also be configured via the configuration item
spring.profiles.activeis the corresponding
profilefor the current environment, you can refer to the Spring Boot documentationfor details.
spring.profiles.activeis empty, the corresponding linker
-will also not exist, and the splicing format of
file-exetensionis the data format of the configuration content, which can be configured via the configuration item
spring.cloud.nacos.config.file-extension. Currently only
yamltypes are supported.
Once you have configured Nacos as a configuration center as above, you can turn on automatic fetching of values from Nacos by adding SpringCloud native annotations to the configuration class, for example
@Valuedirect injection of variable values
@ConfigurationPropertiesconsolidates several variables into a single Properties class
To enable automatic synchronization of changes to Nacos variables, add the
@RefreshScopeannotation to the class into which the variables are injected.
Nacos also provides its own annotations specific to it.
|Spring Cloud Annotations||Nacos Spring Annotations||Remarks|
|@ConfigurationProperties||@NacosConfigurationProperties||auto-refreshed, @NacosProperty set for a property, @NacosIgnore Nacos ignores the value|
Generally, the variables we inject are configured in the configuration file of the microservice, e.g.
application.yaml. But sometimes, we want to save some configuration as a separate nacos configuration, i.e., with a separate
dataId, and then we need to use
For example, there is the following configuration.
We want to create a
test.properties configuration on nacos to store the values of the properties, so we need to modify the microservice configuration file.
extension-configs[n] can be added multiple times, each containing three configurations.
data-id, which must end with
yamland is not affected by
groupThe configuration of a separate
refreshwhether to enable auto-refresh, default
In this way, the nacos configuration of independent profiles and the automatic update of their values are implemented.
On top of the above, let’s add another feature: microservice modifies variable values from local and uploads them to nacos, other instances of the same microservice, get the same value when fetching, then we need to call nacos’ native API
- Suppose there are two instances of the current microservice: A and B. We update the value of
TestPropertiesto Nacos by
publishConfigon A. Then the other instance, B, will receive a notification from Nacos to get the latest value from nacos, but there will be a millisecond delay in between.
- Nacos also provides APIs such as
ConfigService, etc., but these APIs can only be used in SpringBoot and cannot be used directly in SpringCloud.