Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式

Posted 小小工匠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式相关的知识,希望对你有一定的参考价值。

文章目录


Pre

Spring Cloud Alibaba - 18 Nacos Config配置中心加载相同微服务的不同环境下的通用配置 这里说了相同的服务,不同环境下的配置。

那如果是 不同的微服务下相同的配置呢?

举个例子,大家的注册中心地址都是同一个,要把这个公共的配置项抽取到一个单独的文件去管理。


实现

方式一 通过 shared-dataids 方式


方式二 通过 ext-config方式


配置文件优先级

我们看日志Nacos加载的顺序


Located property source: CompositePropertySource name='NACOS', propertySources=[
NacosPropertySource name='artisan-config-center-dev.yml',     优先级最高 
NacosPropertySource name='artisan-config-center.yml',     次之
NacosPropertySource name='artisan-config-center',     再次之
NacosPropertySource name='common4.yml',           再次之
NacosPropertySource name='common3.yml',       再次之
NacosPropertySource name='common2.yml',    再次之
NacosPropertySource name='common.yml'] 级别最低

artisan-config-center-dev.yml 优先级最高 ,依次排开。

举个例子 假设 artisan-config-center-dev.yml 中存在配置项A, 则以artisan-config-center-dev.yml中的配置项A的值为准,如果artisan-config-center-dev.yml不存在,则互补。

总结一下 : 精准配置>不同环境的通用配置>不同工程的(ext-config)>不同工程(shared- dataids)

上述配置 加载的优先级

1)artisan-config-center-dev.yml  精准配置
2)artisan-config-center.yml     同工程不同环境的通用配置
3)ext-config:  不同工程 通用配置
    3.1):common4.yml
    3.2):   common3.yml
4) shared-dataids  不同工程 通用配置
    4.1)common2.yml
    4.2)common1.yml

我们来看下完整的启动日志

2022-02-04 18:00:46.500  INFO 16452 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7254c629] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
 \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2022-02-04 18:00:48.303  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'common.yml', group: 'DEFAULT_GROUP', data: spring:
  cloud:
    nacos:
      discovery: # 故意配置错一个地址,用来验证优先级 
        server-addr: 2.117.97.88:8848
2022-02-04 18:00:48.380  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'common2.yml', group: 'DEFAULT_GROUP', data: spring:
  cloud:
    nacos:
      discovery: 
        server-addr: 1.117.97.88:8848
2022-02-04 18:00:48.483  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'common3.yml', group: 'DEFAULT_GROUP', data: server:
    port: 9999
2022-02-04 18:00:48.665  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'common4.yml', group: 'DEFAULT_GROUP', data: server:
    port: 10000
2022-02-04 18:00:48.761  WARN 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[artisan-config-center] & group[DEFAULT_GROUP]
2022-02-04 18:00:48.857  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'artisan-config-center.yml', group: 'DEFAULT_GROUP', data: server: 
  servlet:
    context-path: /configApp
2022-02-04 18:00:48.922  INFO 16452 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'artisan-config-center-dev.yml', group: 'DEFAULT_GROUP', data: server:
    port: 5678
# 是否走新业务逻辑的开关
isNewPath: false   
2022-02-04 18:00:48.922  INFO 16452 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource name='NACOS', propertySources=[NacosPropertySource name='artisan-config-center-dev.yml', NacosPropertySource name='artisan-config-center.yml', NacosPropertySource name='artisan-config-center', NacosPropertySource name='common4.yml', NacosPropertySource name='common3.yml', NacosPropertySource name='common2.yml', NacosPropertySource name='common.yml']
2022-02-04 18:00:48.925  INFO 16452 --- [           main] com.artisan.ConfigCenterApplication      : The following profiles are active: dev
2022-02-04 18:00:49.382  INFO 16452 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=c392b682-9e0b-366f-8375-9d3b5444358b
2022-02-04 18:00:49.404  INFO 16452 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7254c629] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-02-04 18:00:49.625  INFO 16452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 5678 (http)
2022-02-04 18:00:49.648  INFO 16452 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-04 18:00:49.649  INFO 16452 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2022-02-04 18:00:49.654  INFO 16452 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\\Program Files (x86)\\Java\\jdk1.8.0_261\\bin;C:\\Windows\\Sun\\Java\\bin;C:\\Windows\\system32;C:\\Windows;D:\\Programs\\Python\\Python38\\Scripts\\;D:\\Programs\\Python\\Python38\\;D:\\Program Files (x86)\\Java\\jdk1.8.0_261\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;D:\\Program Files (x86)\\Git\\cmd;D:\\Program Files (x86)\\apache-maven-3.6.3\\\\bin;C:\\Program Files\\mysql\\MySQL Server 5.7\\bin;C:\\Program Files\\python;C:\\Program Files\\python\\Scripts;D:\\BtSoft\\panel\\script;D:\\Program Files\\nodejs\\;D:\\Program Files (x86)\\Python27;D:\\Program Files (x86)\\Java\\jdk1.8.0_261\\jre\\bin;C:\\Users\\artisan\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\artisan\\AppData\\Roaming\\npm;D:\\Program Files (x86)\\JetBrains\\PyCharm 2020.1\\bin;;D:\\Program Files\\VS Code\\Microsoft VS Code\\bin;.]
2022-02-04 18:00:49.742  INFO 16452 --- [           main] o.a.c.c.C.[.[localhost].[/configApp]     : Initializing Spring embedded WebApplicationContext
2022-02-04 18:00:49.742  INFO 16452 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 805 ms
2022-02-04 18:00:49.818  WARN 16452 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-02-04 18:00:49.819  INFO 16452 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-02-04 18:00:49.821  WARN 16452 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-02-04 18:00:49.822  INFO 16452 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-02-04 18:00:49.983  INFO 16452 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-02-04 18:00:51.657  INFO 16452 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2022-02-04 18:00:51.683  INFO 16452 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2022-02-04 18:00:53.265  INFO 16452 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2022-02-04 18:00:53.404  INFO 16452 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 5678 (http) with context path '/configApp'
2022-02-04 18:00:54.529  INFO 16452 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP artisan-config-center 192.168.5.1:5678 register finished
2022-02-04 18:00:56.061  INFO 16452 --- [           main] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2022-02-04 18:00:56.064  INFO 16452 --- [           main] com.artisan.ConfigCenterApplication      : Started ConfigCenterApplication in 12.183 seconds (JVM running for 13.171)


源码

https://github.com/yangshangwei/SpringCloudAlibabMaster

以上是关于Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Alibaba环境搭建

Spring Cloud Alibaba全家桶——Spring Cloud Alibaba介绍

Spring Cloud Alibaba系列教程——Spring Cloud Alibaba开篇

spring boot 整合spring cloud alibaba

spring boot 整合spring cloud alibaba

深度剖析Spring Cloud Alibaba系列——如何兼容Spring Cloud