Foreword
This article Reference article:
SpringCloud Alibaba - Nacos Config custom shared configuration
Prospects Review:
- Nacos (f): Multiple Environments how to "manage" and "isolation" configuration and service
- Nacos (V): in multiple environments how to "read" the environment of the corresponding configuration Nacos
- Nacos (four): SpringCloud project as a configuration access center Nacos
Previous chapters have introduced the most basic usage springcloud project combines Nacos, this paper introduce Nacos as a distribution center, how to read the shared configuration
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
Demo of this project continue to be used before the polymerization engineering article Nacos
, before the junior partner if not the environment, to be the source address download
Scene Description
After a project to increase the number of service profiles corresponding increase, there will be multiple configuration files in the same configuration, then we can apply the same configuration independent, as the project shared configuration files for each service, each service can sharing configuration by reading Nacos
Here with the next demo demo, if feasible
- demo project: nacos-config-share
- Profile: nacos-config-share.yml
- Share profile: shareconfig1.yml, shareconfig2.yml
Create a project
As always, created or named in a polymerization engineering Nacos nacos-config-share
sub-project, before it was pom.xml file dependencies are consistent with the project, if you do not have previous project can refer to the source address
1, modify the startup class springbootNacosConfigShareApplication.java
@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 project's profilebootstrap.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
As can be seen from the configuration file, the
shared-dataids
specified configuration file to read the shared attributesDataID
, the plurality of file,
partition
usingrefreshable-dataids
the specified shared profile support auto-refresh
New Profile
Here we demonstrate, temporarily join Namespace, created directly in the public space and test
Create profiles nacos-config-share.yml
, detailed as follows:
- DataId:
nacos-config-share.yml
- Configuration format:
YAML
- Configuring Content:
server: port: 9984 nacos: share: nacos-config-share
Create a shared profile 1 shareconfig1.yml
, detailed as follows:
- DataId:
shareconfig1.yml
- Configuration format:
YAML
- Configuring Content:
share: config1: 这里是共享配置文件1
Create a shared profile 1 shareconfig2.yml
, detailed as follows:
- DataId:
shareconfig2.yml
- Configuration format:
YAML
- Configuring Content:
share: config2: 这里是共享配置文件2
Once created, the configuration list as shown below:
Start the test
Directly start the project, if successful start. You can see the log information as follows:
Start class interface to access provided, the test can get to values shared configuration file
访问127.0.0.1:9984/getValue,返回:nacos-config-share
访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1
访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2
Under re-test refreshable-dataids
automatically refresh configuration is in effect
In Nacos console to modify the configuration file shared shareconfig2.yml
values of:这里是共享配置文件2这里是共享配置文件2
After editing and saving, re-request 127.0.0.1:9984/getShare2, which returns the following results:
Here is a shared configuration file is shared here 2 Profile 2
By the above described return results specified in the configuration file shared-dataids
and refreshable-dataids
can read the configuration file sharing and auto-refresh.
The demand for change
Now suppose you want to read
shareconfig3.yml
andshareconfig4.yml
file it as a GroupSHARE3_GROUP
andSHARE4_GROUP
that share the profile with the project itself not in the same Group Profiles in (上边的例子是全都在DEFAULT_GROUP分组
) If you continue to use that method on top, you can not read the shared configuration file
Then you can use another configuration ext-config
, it can be loaded custom configuration specified by the userDataID、Group以及是否自动刷新
And ext-config
is a collection of ( List
), to specify support for multiple profiles.
New Shared Profile
Create configuration profiles shareconfig3.yml
and shareconfig4.yml
pay attention to their Group Properties
- DataId:
shareconfig3.yml
- Group:
SHARE3_GROUP
- Configuration format:
YAML
- Configuring Content:
share: config3: 这里是共享配置文件3,Group:SHARE3_GROUP
- DataId:
shareconfig4.yml
- Group:
SHARE4_GROUP
- Configuration format:
YAML
- Configuring Content:
share: config4: 这里是共享配置文件4,Group:SHARE4_GROUP
Creating success page as follows:
Modify the project code
1, the starting class NacosConfigShareApplication.java
following new tag
@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
, increase 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 testing
The project has been modified to be seen
- Under the project itself nacos profiles belong DEFAULT_GROUP, the default read
- Under shareconfig1.yml, shareconfig2.yml belong DEFAULT_GROUP, by
shared-dataids
reading specified - shareconfig3.yml, shareconfig4.yml fall
非DEFAULT_GROUP
under, byext-config
customizing read configuration properties
Start the project, test all of the configuration file can be read correctly
访问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
Modify shareconfig4.yml
the configuration of content: 这里是共享配置文件4,Group:SHARE4_GROUP,支持自动刷新
After saving, called again 127.0.0.1:9984/getShare4, returns the following:
Here is a shared configuration file 4, Group: SHARE4_GROUP, support auto-refresh
After the call interface found two ways to load sharing configuration can be read properly, and can be used together. ext-config
The way to achieve the sharing of user-defined configuration profiles.
to sum up
The above demo has been demonstrated Nacos sharing arrangement implemented in two ways, two ways for different scenarios, summarized as follows:
shared-dataids
the way:- Suitable for a shared profile with the project default configuration file in the same Group, two direct command can handle
- Advantages: easy to configure
- Disadvantages: only in the same Group
ext-config
the way:- It can be customized shared configuration file to be read by the developer DataId, Group, refresh properties, so just to solve the
shared-dataids
limitations of existence. - Advantages: can be
shared-dataids
used in conjunction with the program, the user-defined configuration. Strong flexibility - Disadvantages: Configuration error-prone, be familiar with the YAML syntax
- It can be customized shared configuration file to be read by the developer DataId, Group, refresh properties, so just to solve the
They have their own strengths can be seen in two ways, so if you need to use a shared configuration in development, we can be on the specific circumstances choose their most appropriate solution.
This article Source : https: //github.com/larscheng/larscheng-learning-demo/tree/master/Nacos
- Author of the article: LarsCheng
- Article link: This article first appeared on his blog: https: //www.larscheng.com/nacos-namespace/
- Publishing: OpenWrite most understand your technology from media management platform
- Disclaimer: This blog All articles unless otherwise specified, are used CC BY-NC-SA 4.0 license. Reproduced please specify from LarsCheng's Blog!