前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式

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

作者头像
小小工匠
发布2022-02-05 16:00:07
1.6K0
发布2022-02-05 16:00:07
举报
文章被收录于专栏:小工匠聊架构

文章目录

Pre

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

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

实现

方式一 通过 shared-dataids 方式

方式二 通过 ext-config方式

配置文件优先级

我们看日志Nacos加载的顺序

代码语言:javascript
复制
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)

上述配置 加载的优先级

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/02/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • Pre
  • 实现
    • 方式一 通过 shared-dataids 方式
      • 方式二 通过 ext-config方式
      • 配置文件优先级
      • 源码
      相关产品与服务
      对象存储
      对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档