Nacos configuration file management, microservices to obtain Nacos configuration files, hot update, configuration sharing, configuration priority, etc.

In traditional projects, when we need to modify the configuration file, we need to modify the configuration file, stop the service, and restart the service, which is not conducive to improving the user experience; but
using Nacos for project configuration file management can realize hot update configuration files. After modification in Nacos, Nacos will notify the project of the new configuration file.
insert image description here

add configuration file

Step 1, click to add configuration:
insert image description here
Step 2, configuration information
insert image description here
Note: not all configurations are written into Nacos, only those configurations that may change are recommended to be written into Nacos, for example: whether an activity is enabled (true/ false), date format template (yyyymmdd/yyyy-mm-dd), etc.,
without writing the configuration of Nacos management, such as: database address, own service name, etc.

Microservice gets the configuration in Nacos

We have configured the configuration file in Nacos, so how to get it when our project is running?
insert image description here
When we did not use Nacos for configuration management before, we used such a process to read configuration files; then after we use Nacos, when and how should we read the files in Nacos?
insert image description here
Our project will read the configuration in Nacos before reading the local configuration file application.yml, then read the local configuration, and then merge;

Then there is a problem at this time: my Nacos address is written in application.yml, how do we access the Nacos address and read the configuration in Nacos before reading the local configuration file?
The answer is to use bootstrap.yml. In springboot, bootstrap.yml has a higher priority than application.yml. After loading the configuration in bootstrap.yml, all systems will read the address of Nacos, access Nacos, obtain the configuration file, and then read application.yml The configuration information in is merged.

We configure the date format string in Nacos for verification:
insert image description here
insert image description here
the first step is to introduce the Nacos client (config) to rely
on the second step, write the service name, development environment, Nacos address, and file suffix name in bootstrap.yml, The system will combine these information to obtain the configuration file in Nacos.
insert image description here
The third step is to test whether the date format configured in Nacos can be obtained.
insert image description here
Start the service, access the control layer, obtain the date format successfully and return the result according to the format, which proves that the configuration file in Nacos has been obtained Success
Note: If the namespace is configured, the namespace also needs to be configured
insert image description here

Nacos configuration file hot update

One of the benefits of Nacos is that it can implement hot update of configuration files, so how can we implement hot update of configuration files after the project starts?
There are two ways:
the first one is to add the @RefreshScope annotation to the place where the configuration file in Nacos is used to achieve hot update.
For example: after Nacos has modified the configuration file, you only need to refresh the front-end page to get the latest configuration.
insert image description here
The second method: add a configuration class, add the @configrationproperties annotation to the configuration class, and inject it into the spring container. Realize hot update

insert image description here
Example:
before update:
insert image description here
modify the date format to yyyy mm month dd day, click publish:
insert image description here
refresh the page directly without restarting to get the latest configuration values:
insert image description here

configuration sharing

In actual development, there are many configuration items in Dev (Develpment) development environment, sit (System Integration Test) system integration test, uat (User Acceptance Test) user acceptance test, and other environmentsconfiguration items used;
If this kind of configuration item is written once in the configuration of each environment, it will be very troublesome. If it needs to be modified, it must be changed one by one.
So is there a way to achieve such repeated configuration and put it in one place, in one configuration file? When modifying, you only need to modify this one place, and all the places where this configuration is called in all environments can get this value?
This is our configuration sharing:
insert image description here
userservice-dev.yml is an environment configuration file, corresponding to different development environments,
while userservice.yml is a shared configuration file, which can be read in various environments, so there is no need to write it again in each configuration file

Note: When the configuration file (local configuration) in our project, the shared configuration in Nacos, and the environment configuration of Nacos (such as dev, uat, etc.) are all configured with an attribute, then after the project starts, it will read where What about the configuration?
Answer: Nacos environment configuration (dev, sit, uat, etc.) > remote shared configuration > local configuration
insert image description here
That is to say, if the Nacos environment configuration file, Nacos shared configuration file, and the local configuration file of the project all have the same configuration properties, then the project When accessing, use the Nacos environment configuration file first, followed by the shared configuration, and finally the local configuration

Guess you like

Origin blog.csdn.net/dayuiicghaid/article/details/126131768