Nacos supports three configuration loader schemes
Nacos supports the configuration solution of Namespace + group + data ID
Namespace scheme
Environment differentiation through namespaces
The following is a configuration example:
1. Create a namespace:
Namespace -> create a namespace:
Create three namespaces, namely dev, test and pre
2. Back in the configuration list, you can see the three namespaces created
Next, we need to create gulimall-coupon.properties configuration rules under the dev namespace:
3. Test:
The rules configured under the dev namespace are not used, but the rules configured under the public namespace are used.
What's going on here?
"gulimall-coupon.properties" , the default is the rules configured in the content of the public namespace.
4. Specify the namespace
If you want to make our custom namespace take effect, you need to specify which command to use in the "bootstrap.properties" file
namespace:
spring.cloud.nacos.config.namespace=00bf4f79-4103-4752-af8b-abff134b1d46
This namespace ID comes from the namespace we created in the first step
5. Restart "gulimall-coupon" and test ok again
But the granularity of this namespace is not fine enough, so we can create a name for each microservice module of the project
space.
6. Create namespaces for all microservices
7. Go back to the configuration list tab and clone the pulic configuration rules to the coupon namespace
Open the configuration:
8. Modify the bootstrap.properties file under gulimall-coupon and add the following configuration information
spring.cloud.nacos.config.namespace=b2041d1f-b8f1-45d4-9373-b0ce4027a28d
What is specified here is that the configuration under the coupon namespace is used when reading.
9. Restart "gulimall-coupon", test ok
DataID scheme
By specifying spring.profile.active and the DataID of the configuration file, different configurations can be read in different environments, and the configuration can be read
When using the default namespace public, the DataID under the default group (default_group) is used.
By default, Namespace=public, Group=DEFAULT GROUP, the default Cluster is DEFAULT
A configuration file that spring.profiles.active=dev
can be specified by specifyingxxx-dev.properties
Group plan
Realize environment distinction through Group
Example: Read different configurations by using different groups, or take the gulimall-coupon microservice above as an example
1. Create a new "gulimall-coupon.properties" under coupon and place it under the "tmp" group
2. Modify the "bootstrap.properties" configuration and add the following configuration
spring.cloud.nacos.config.group=tmp
It is wrong to comment out, you want to keep the namespace
3. Restart "gulimall-coupon", test ok
In actual development, the group names are dev, test, pre