Nacos自定义扩展的 Data Id 配置
Spring Cloud Alibaba Nacos Config可支持自定义 Data Id 的配置。 一个完整的配置案例如下所示:下边我们在service2微服务下配置扩展。spring:application:name: service2cloud:nacos:config:server‐addr: 127.0.0.1:8848# config external configuration#
·
Spring Cloud Alibaba Nacos Config可支持自定义 Data Id 的配置。 一个完整的配置案例如下所示:
下边我们在service2微服务下配置扩展。
spring:
application:
name: service2
cloud:
nacos:
config:
server‐addr: 127.0.0.1:8848
# config external configuration
# 1、Data Id 在默认的组 DEFAULT_GROUP,不支持配置的动态刷新
ext‐config[0]:
data‐id: ext‐config‐common01.properties
# 2、Data Id 不在默认的组,不支持动态刷新
ext‐config[1]:
data‐id: ext‐config‐common02.properties
group: GLOBALE_GROUP
# 3、Data Id 既不在默认的组,也支持动态刷新
ext‐config[2]:
data‐id: ext‐config‐common03.properties
group: REFRESH_GROUP
refresh: true
refresh: true
可以看到:
- 通过
spring.cloud.nacos.config.ext-config[n].data-id
的配置方式来支持多个 Data Id 的配置。 - 通过
spring.cloud.nacos.config.ext-config[n].group
的配置方式自定义 Data Id 所在的组,不明确配置的话,默认是 DEFAULT_GROUP。 - 通过
spring.cloud.nacos.config.ext-config[n].refresh
的配置方式来控制该 Data Id 在配置变更时,是否支持应用中可动态刷新, 感知到最新的配置值。默认是不支持的。
Note : spring.cloud.nacos.config.ext-config[n].data-id 的值必须带文件扩展名,文件扩展名既可支持properties,又可以支持 yaml/yml。 此时spring.cloud.nacos.config.file-extension 的配置对自定义扩展配置的 Data Id 文件扩展名没有影响。
通过自定义扩展的 Data Id 配置,既可以解决多个应用间配置共享的问题,又可以支持一个应用有多个配置文件。
测试:
配置ext-config-common01.properties:
配置ext-config-common02.properties
配置ext-config-common03.properties
编写测试代码:
@GetMapping(value = "/configs2")
public String getConfigs2(){
String name = applicationContext.getEnvironment().getProperty("common.name");
String age = applicationContext.getEnvironment().getProperty("common.age");
String address = applicationContext.getEnvironment().getProperty("common.address");
String birthday= applicationContext.getEnvironment().getProperty("common.birthday");
String fullname = applicationContext.getEnvironment().getProperty("common.fullname");
return name+"+"+ age+"+"+address+"+"+ birthday+"+"+ fullname;
}
重启应用,访问http://localhost:56011/configs2,观察配置是否成功获取。
输出:
service2 config+12+beijing+1990‐1‐1+zhangsansanff
更多推荐
已为社区贡献10条内容
所有评论(0)