在spring cloud中的config server中,对于公用的配置我们会放到application-{profiles.active}.yml中,对于共享配置片断,可以使用include来实现;而对于nacos来说,是不能直接继承了,则需要使用nacos自己的语法。 一 共享配置片断https://www.cnblogs.com/lori/p/15893020.html spring: cloud: nacos: discovery: server-addr: 192.168.60.138:80 namespace: zzl config: server-addr: 192.168.60.138:80 file-extension: yaml namespace: zzl shared-configs[0]: data-id: actuator.yaml refresh: true shared-configs[1]: data-id: const.yaml refresh: true 二 导入配置文件(继承配置)spring.config.import属性,用来导入本地或者远程的公用配置文件 spring: application: name: @artifactId@ cloud: nacos: discovery: server-addr: ${NACOS_HOST:pig-register}:${NACOS_PORT:8848} config: server-addr: ${spring.cloud.nacos.discovery.server-addr} config: import: - nacos:application-@profiles.active@.yml - nacos:${spring.application.name}-@profiles.active@.yml以上两种共享配置文件的方法,非常实用,希望对咱们有所帮助。 三 本地与远程配置优先级远程优先 其次是本地 测试 本地配置类,开启配置自动刷新 @Data @RefreshScope @ConfigurationProperties("auth") public class AuthProperties { private String name; private String date; } //启动类 @SpringBootApplication @EnableConfigurationProperties(AuthProperties.class) //开启AuthProperties配置bean @Slf4j public class LindDemoApplication {}远程nacos配置文件auth.yml auth: name: lind-remote
作者:仓储大叔,张占岭, 支付宝扫一扫,为大叔打赏!
|