1. Nacos and project integration
1.1 Contents of the outermost POM file
<! <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.6. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> < spring - the boot version > 2.2.6. RELEASE < / spring - the boot. Version > < spring - cloud. Version > Hoxton. SR7 < / spring - cloud. Version > < spring - cloud - alibaba. Version > 2.2.0. RELEASE < / spring - cloud - alibaba. Version > < / properties > < dependencyManagement > <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <! --spring cloud alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code
1.2. Module POM
<dependencies> <! -- nacos --> <! -- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <! -- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies>Copy the code
** bootstrap.yml **
spring: application: name: order cloud: nacos: discovery: server-addr: The ${NACOS - HOST: 127.0.0.1} : ${8848} NACOS - PORT: config: server - addr: ${spring.cloud.nacos.discovery.server-addr} file-extension: yml profiles: active: devCopy the code
Why use bootstrap.yml instead of application.yml?
The first reason is that bootstrap takes precedence over application loading. The second is to keep the Bootstrap Context and Application Context configurations separate. Conclusion and specific can see https://www.baeldung.com/spring-cloud-bootstrap-properties https://www.cnblogs.com/blognetspace/p/8469033.html
Launch will default to load a prefix + ‘-‘ + ${spring. Active. Profile} +. + ${spring. Cloud. Nacos. Config. File – the extension} the configuration file, The prefix default is spring. The application. The name, of course, can also be reset: spring. Cloud. Nacos. Config. The prefix
The sample project address: https://gitee.com/rookiesJ/nacos-test.git
2. Configure read
2.1. Locate other configuration sets
If you look at the previous section, we’ve created the namespace, we’ve set the group, so how do we use that?
. We only need to configure the spring cloud. Nacos. Config. The namespace can specify namespaces, configure spring. Cloud. Nacos. Config. The group can specify grouping. If namespace is not specified, the default value is public. If group is not specified, the default value is DEFAULT_GROUP.
What about namespace and group?
Namespace is filled with “namespace ID”, which can be seen in the namespace menu, or see this location.
The group field is in the configuration set of the configuration list.
Example:
spring:
cloud:
nacos:
config:
namespace:
group:
Copy the code
2.2 Read environment configuration in the project
Now that we have some configuration items configured in Nacos, how do we read the Nacos configuration in the project?
In fact, we normally read the configuration operation is exactly the same.
@Value("${haha}")
private String test;
Copy the code
That’s all.
2.3. The configuration file is automatically refreshed
We change the configuration item in the Nacos configuration set, and the console also prompts that the attribute has been updated to something, but why is it still the same value after refreshing? Spring. Cloud. Nacos. Config. Refresh – enabled is true (the default is true), automatic refresh has launched the ah, why or started to read?
This is related to the @value annotation read configuration, the specific relationship, I will fill in later. After all, it reads the configuration it originally loaded. To refresh the configuration it read, you must annotate the class that needs to be refreshed with the @refreshScope annotation (provided by Spring Cloud), like this:
@RestController @RefreshScope public class TestController { @Value("${you}") private String test; @RequestMapping("test") public String test(){ return test; }}Copy the code
This allows the environment configuration to be dynamically read. There are other methods, which I won’t describe.
2.3. A module corresponds to multiple configuration files
Since the project can read the Nacos configuration set, can it read more than one? Ok, the developer also thought of this problem, let’s see how to operate.
The first configuration mode is extension-configs.
spring:
cloud:
nacos:
config:
extension-configs:
-
data-id: extends-base-dev.yml
refresh: true
Copy the code
Refresh indicates that the function of automatically refreshing configurations is enabled. This can also specify a configuration like group, just like the one above. Format if it is. The properties, is written in the spring. Cloud. Nacos. Config. The extension – configs [0]. The data – id = this.
The second option is to use shared-configs:
spring:
cloud:
nacos:
config:
shared-configs:
-
data-id: extends-base-dev.yml
refresh: true
Copy the code
The difference between the two is the load priority issue. However, in the previous version, shared-configs could not specify other properties such as group.
2.4. Configure file loading priority
bootstrap > application > extension-configs > shared-configs
The larger the subscript of an array configuration file in extension-configs is, the higher the priority is, and subsequent properties will overwrite the previous properties. The later the property is loaded, the property loaded after the property conflict overrides the previous property.
See the live update here for more tutorials. You can subscribe to receive updates.