This article introduces how to read the shared configuration when Nacos is used as the configuration center
my environment
- Windows10
- JDK8
- SpringCloud:Finchley.RELEASE
- SpringBoot:2.0.4.RELEASE
- spring-cloud-alibaba-dependencies:0.2.2.RELEASE
- Nacos-server:1.0.1
The project Demo in this article continues to use the aggregation project in the previous article Nacos
. If you don’t have the previous environment, you can download it from the source code address
scene description
After the number of services in a project increases, the configuration files increase accordingly. There will be the same configuration in multiple configuration files, then we can separate the same configuration as a shared configuration file for each service in the project. Each service can Read shared configuration through Nacos
Let's use a demo to demonstrate whether it is feasible
- demo project: nacos-config-share
- Configuration file: nacos-config-share.yml
- Share configuration files: shareconfig1.yml, shareconfig2.yml
create project
As usual, create a subproject named under the aggregation project Nacos nacos-config-share
, and its pom.xml file depends on the same as the previous project. If you do not have the previous project, you can refer to the source code address
1. Modify the springboot startup classNacosConfigShareApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class NacosConfigShareApplication {
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class NacosConfigShareApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigShareApplication.class, args);
}
@Value("${nacos.share}")
private String share;
@Value("${share.config1}")
private String shareConfig1;
@Value("${share.config2}")
private String shareConfig2;
@RequestMapping("/getValue")
public String getValue() {
return share;
}
@RequestMapping("/getShare1")
public String getShare1() {
return shareConfig1;
}
@RequestMapping("/getShare2")
public String getShare2() {
return shareConfig2;
}
}
2. Modify the configuration file of the projectbootstrap.yml
spring:
application:
name: nacos-config-share
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
shared-dataids: shareconfig1.yml,shareconfig2.yml
refreshable-dataids: shareconfig1.yml,shareconfig2.yml
It can be seen from the configuration file that
shared-dataids
the shared configuration file to be read is specified through attributesDataID
, and multiple files are,
separated
by usingrefreshable-dataids
the specified shared configuration file to support automatic refresh
New configuration file
Here we use it as a demonstration, without joining the Namespace, and create and test directly in the public space
Create a configuration file nacos-config-share.yml
, as detailed below:
- DataId:
nacos-config-share.yml
- Configuration format:
YAML
- Configuration content:
server: port: 9984 nacos: share: nacos-config-share
Create a shared configuration file 1 shareconfig1.yml
, the details are as follows:
- DataId:
shareconfig1.yml
- Configuration format:
YAML
- Configuration content:
share: config1: 这里是共享配置文件1
Create a shared configuration file 1 shareconfig2.yml
, the details are as follows:
- DataId:
shareconfig2.yml
- Configuration format:
YAML
- Configuration content:
share: config2: 这里是共享配置文件2
After the creation is successful, the configuration list is as follows:
start test
Start the project directly, if the startup is successful. You can see the following information in the log:
Access the interface provided in the startup class, and test whether the value in the shared configuration file can be obtained
访问127.0.0.1:9984/getValue,返回:nacos-config-share
访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1
访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2
Then test refreshable-dataids
whether the configured automatic refresh takes effect
Modify the value of the shared configuration file in the Nacos console shareconfig2.yml
:这里是共享配置文件2这里是共享配置文件2
After editing and saving, request 127.0.0.1:9984/getShare2 again, and observe the returned results as follows:
Shared profile 2 here Shared profile 2 here
The above returned results show that the shared configuration file can be read and automatically refreshed by specifying shared-dataids
and in the configuration file .refreshable-dataids
change of requirements
shareconfig3.yml
Suppose you want to read and file nowshareconfig4.yml
but its Group isSHARE3_GROUP
andSHARE4_GROUP
, that is, the shared configuration file is not in the same group as the project’s own configuration file (上边的例子是全都在DEFAULT_GROUP分组
), then if you continue to use the above method, you will not be able to read the shared configuration file
At this time, another configuration can be used ext-config
, which can be customized by the user to specify the configuration to be loadedDataID、Group以及是否自动刷新
And ext-config
it is a collection ( List
) that supports the specification of multiple configuration files.
Create a new shared profile
First create configuration configuration files shareconfig3.yml
and shareconfig4.yml
pay attention to their Group attributes
- DataId:
shareconfig3.yml
- Group:
SHARE3_GROUP
- Configuration format:
YAML
- Configuration content:
share: config3: 这里是共享配置文件3,Group:SHARE3_GROUP
- DataId:
shareconfig4.yml
- Group:
SHARE4_GROUP
- Configuration format:
YAML
- Configuration content:
share: config4: 这里是共享配置文件4,Group:SHARE4_GROUP
Create a successful page as follows:
Modify project code
1. NacosConfigShareApplication.java
Add the following code in the startup class
@Value("${share.config3}")
private String shareConfig3;
@Value("${share.config4}")
private String shareConfig4;
@RequestMapping("/getShare3")
public String getShare3() {
return shareConfig3;
}
@RequestMapping("/getShare4")
public String getShare4() {
return shareConfig4;
}
2. Modify the project configuration file bootstrap.yml
and add ext-config
configuration
spring:
application:
name: nacos-config-share
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
shared-dataids: shareconfig1.yml,shareconfig2.yml
refreshable-dataids: shareconfig1.yml,shareconfig2.yml
ext-config:
- data-id: shareconfig3.yml
group: SHARE3_GROUP
refresh: true
- data-id: shareconfig4.yml
group: SHARE4_GROUP
refresh: true
Start for testing
After the project has been modified, you can see
- The nacos configuration file of the project itself belongs to DEFAULT_GROUP, which is read by default
- shareconfig1.yml, shareconfig2.yml belong to DEFAULT_GROUP,
shared-dataids
read by specifying - shareconfig3.yml and shareconfig4.yml belong to
非DEFAULT_GROUP
the following, andext-config
can be customized and read through configuration properties
Start the project and test whether all configuration files can be read normally
访问127.0.0.1:9984/getValue,返回:nacos-config-share
访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1
访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2这里是共享配置文件2
访问127.0.0.1:9984/getShare3,返回:这里是共享配置文件3,Group:SHARE3_GROUP
访问127.0.0.1:9984/getShare4,返回:这里是共享配置文件4,Group:SHARE4_GROUP
The modified shareconfig4.yml
configuration content is: 这里是共享配置文件4,Group:SHARE4_GROUP,支持自动刷新
, after saving, call 127.0.0.1:9984/getShare4 again, and the return is as follows:
Here is shared configuration file 4, Group: SHARE4_GROUP, supports automatic refresh
After calling the interface, it is found that the loading methods of the two shared configurations can be read normally and can be used together. ext-config
The way to achieve user-defined configuration shared configuration files.
Summarize
The above demo has demonstrated two implementations of Nacos shared configuration. The two methods are aimed at different scenarios and are summarized as follows:
shared-dataids
Way:- It is suitable for when the shared configuration file and the project default configuration file are in the same group, and it can be done directly with two commands
- Advantages: easy configuration
- Disadvantage: only in the same group
ext-config
Way:- It allows the developer to customize the DataId, Group, and refresh attributes of the shared configuration file to be read, which just solves the
shared-dataids
existing limitations. - Advantages: Can
shared-dataids
be used in conjunction with the program, user-defined configuration. Strong flexibility - Disadvantages: configuration is error-prone, familiar with YAML syntax
- It allows the developer to customize the DataId, Group, and refresh attributes of the shared configuration file to be read, which just solves the
It can be seen that the two methods have their own advantages, so if you need to use shared configuration during development, you can choose the most suitable solution according to your specific situation.
Source code of this article : https://github.com/larscheng/larscheng-learning-demo/tree/master/Nacos