Nacos installation
The official website has very detailed steps, so I won’t go into details here:/zh-cn/docs/
Basic steps for configuring nacos service
- Download the jar package or synchronize the code and compile the jar package
- Generate MySql database table (MySql script is in the config directory)
- Reset the password of the default user of Nacos, obtain the encryption string of the password in the following way, and update the password in the data table
String password = new BCryptPasswordEncoder().encode("Your password");
- Deploy to the server and modify the conf/file to support mysql. For detailed reference:/zh-cn/docs/
- Start the Nacos service
- Log in with a new password, and you can create various new configurations
SpringBoot client reads the configuration of nacos
- pom quote:
<dependency> <groupId></groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.1</version> </dependency>
- Configure the Nacos service address
Configure the address of the nacos service in the configuration file
nacos: config: server-addr: 10.10.1.111:8848
- Get a property value of a dataId in the default group under public space
nacos creates a "public" namespace by default, and the created dataId is under a group named "DEFAULT_GROUP".
The method of obtaining the newly created dataId configuration called "myfirstconfig" under the default environment is as follows:
@NacosPropertySource(dataId = "myfirstconfig", autoRefreshed = true) public class NacosMyFirstConfig { @NacosValue(value = "${ExpireSeconds:30}", autoRefreshed = true) private int expireSeconds;
Suppose there is an attribute named ExpireSeconds in the dataId "myfirstconfig". The method to obtain the value of this attribute name is as above. The value after the ExpireSeconds colon is the default value, and the default value used when the attribute name corresponding to nacos cannot be obtained.
autoRefreshed = true means that once the value of this property on the nacos server changes, the client can obtain the new changed value.
- You can create a new namespace namespace and a new group name. Next, different namespaces will be used for deployment in multiple environments.
SpringBoot client uses nacos in multiple environments
Our programs generally have a development environment, a test environment, a production environment, or a bastion environment between testing and generation. A set of java code controls different environments and their configurations through specification and corresponding ones.
So how can we let different environments read different nacos configuration values?
Create different namespaces on nacos
- You can create multiple namespaces on nacos. In addition to the default public namespace, you can create namespaces such as dev, test, etc. A new namespace will appear next to the name, such as 6e9c8f46-3e40-41f2-9bf3-50f78c860971, which is
- This namepac's unique ID is useful next.
Create a set of dataIds with the same name in each namespace. The attribute names in it are the same, only if the attribute values are different, they are placed under the default "DEFAULT_GROUP" group
- For example, the dataId of myfirstconfig just now was created under the namespace of dev and test, and it also has the property name ExpireSeconds, and the property value can be different.
The client specifies different namespaces
- Specify different namespaces in different files
nacos: config: server-addr: 10.10.1.111:8848 namespace: 6e9c8f46-3e40-41f2-9bf3-50f78c860971
Note that namespace does not fill in the names dev and test, but needs to fill in the unique ID string. Each newly created namespace has a unique ID, as shown above.
If the default publicnamespace is called, you do not need to specify a namespace
In this way, you can find that programs started by different environments get attribute values under different namespaces, and the code in other places does not need to be modified.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.