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.
Configuration
Adding dependencies
- 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.name
is because it is part of thedataId
field 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 itemspring.cloud.nacos.config.prefix
. -
spring.profiles.active
is the correspondingprofile
for the current environment, you can refer to the Spring Boot documentationfor details.Note: When
spring.profiles.active
is empty, the corresponding linker-
will also not exist, and the splicing format ofdataId
will become${prefix}. ${file-extension}
-
file-exetension
is the data format of the configuration content, which can be configured via the configuration itemspring.cloud.nacos.config.file-extension
. Currently onlyproperties
andyaml
types are supported.
Injecting configuration
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
@Value
direct injection of variable values@ConfigurationProperties
consolidates several variables into a single Properties class
To enable automatic synchronization of changes to Nacos variables, add the
@RefreshScope
annotation 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 |
---|---|---|
@Value | @NacosValue | auto-refreshed |
@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 extension-configigs
.
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
The standalonedata-id
, which must end withproperties
oryaml
and is not affected byspring.cloud.nacos.config.file-extension
group
The configuration of a separategroup
.refresh
whether to enable auto-refresh, defaultfalse
In this way, the nacos configuration of independent profiles and the automatic update of their values are implemented.
Synchronous configuration
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 NacosConfigManager
.
Note
- Suppose there are two instances of the current microservice: A and B. We update the value of
TestProperties
to Nacos bypublishConfig
on 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
@NacosInject
,ConfigService
, etc., but these APIs can only be used in SpringBoot and cannot be used directly in SpringCloud.
Reference: http://edisonxu.com/2022/07/08/spring-cloud-nacos-variable.html