本文参考文章:html
SpringCloud Alibaba - Nacos Config 自定义共享配置java
前景回顾:git
前几章已经基本介绍了springcloud项目结合Nacos的大部分用法,本文介绍一下Nacos做为配置中心时,如何读取共享配置github
本文的项目Demo继续沿用以前文章中的聚合工程Nacos
,若小伙伴尚未以前的环境,可至源码地址中下载spring
一个项目中服务数量增长后,配置文件相应增长,多个配置文件中会存在相同的配置,那么咱们能够将相同的配置独立出来,做为该项目中各个服务的共享配置文件,每一个服务均可以经过Nacos进行共享配置的读取bootstrap
下面用一个demo演示下,是否可行springboot
一如往常,仍是在聚合工程Nacos下建立名为nacos-config-share
的子工程,其pom.xml文件依赖与以前的项目都一致,若是您没有以前的项目可参考源码地址app
一、修改springboot启动类NacosConfigShareApplication.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; } }
二、修改该项目的配置文件bootstrap.yml
spa
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
从配置文件能够看出,经过
shared-dataids
属性来指定要读取共享配置文件的DataID
,多个文件用,
分隔
使用refreshable-dataids
指定共享配置文件支持自动刷新
这里咱们做为演示,暂不加入Namespace,直接在公共空间中建立及测试
建立配置文件nacos-config-share.yml
,详细以下:
nacos-config-share.yml
YAML
server: port: 9984 nacos: share: nacos-config-share
建立共享配置文件1shareconfig1.yml
,详细以下:
shareconfig1.yml
YAML
share: config1: 这里是共享配置文件1
建立共享配置文件1shareconfig2.yml
,详细以下:
shareconfig2.yml
YAML
share: config2: 这里是共享配置文件2
建立成功后,配置列表以下图:
直接启动项目,若是启动成功。能够看到日志中以下信息:
访问启动类中提供的接口,测试下可否获取到共享配置文件中的值
访问127.0.0.1:9984/getValue,返回:nacos-config-share 访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1 访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2
再测试下refreshable-dataids
配置的自动刷新是否生效
在Nacos控制台中修改共享配置文件shareconfig2.yml
的值为:这里是共享配置文件2这里是共享配置文件2
编辑保存后,从新请求 127.0.0.1:9984/getShare2 ,观察返回结果以下:
这里是共享配置文件2这里是共享配置文件2
以上返回结果说明经过在配置文件中指定shared-dataids
和refreshable-dataids
是能够实现共享配置文件的读取和自动刷新的。
假设如今要读取
shareconfig3.yml
和shareconfig4.yml
文件可是它的Group为SHARE3_GROUP
和SHARE4_GROUP
, 即共享配置文件与项目自身配置文件不在同一Group中(上边的例子是全都在DEFAULT_GROUP分组
) 那若是继续用上边的方法,就没法读取共享配置文件
这时可使用另外一个配置ext-config
,它能够由用户自定义指定须要加载的配置DataID、Group以及是否自动刷新
而且ext-config
是一个集合(List
),支持多个配置文件的指定。
先建立配置配置文件shareconfig3.yml
和shareconfig4.yml
,注意他们的Group属性
shareconfig3.yml
SHARE3_GROUP
YAML
share: config3: 这里是共享配置文件3,Group:SHARE3_GROUP
shareconfig4.yml
SHARE4_GROUP
YAML
share: config4: 这里是共享配置文件4,Group:SHARE4_GROUP
建立成功页面以下:
一、在启动类NacosConfigShareApplication.java
中新增以下代码
@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; }
二、修改项目配置文件bootstrap.yml
,增长ext-config
配置
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
项目通过修改后,能够看到
shared-dataids
指定进行读取非DEFAULT_GROUP
下,经过ext-config
配置属性进行自定义读取启动项目,测试全部的配置文件是否能够正常读取
访问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
修改shareconfig4.yml
的配置内容为:这里是共享配置文件4,Group:SHARE4_GROUP,支持自动刷新
,保存后,再次调用127.0.0.1:9984/getShare4,返回以下:
这里是共享配置文件4,Group:SHARE4_GROUP,支持自动刷新
调用接口后发现,两种共享配置的加载方式均可以正常读取,而且能够一块儿使用。ext-config
的方式实现了用户自定义配置共享配置文件。
上面的demo已经演示Nacos共享配置的两种实现方式,两种方式针对不一样的场景,总结以下:
shared-dataids
方式:
ext-config
方式:
shared-dataids
存在的局限性。shared-dataids
方案结合使用,用户自定义配置。灵活性强可见两种方式各有长处,因此若是在开发中须要使用共享配置,你们能够是具体状况而定选择本身最合适的方案。
本文源码:https://github.com/larscheng/larscheng-learning-demo/tree/master/Nacos