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

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

6735aa4777de402592fbe82e8b40ee3d.png

Pre


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

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


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


实现


7d9fd58af4cc443d96255b515d5d9bb2.png

方式一 通过 shared-dataids 方式


8a57018978a2489ea148597d6965738c.png

方式二 通过 ext-config方式


655dc77998ab4776ae9ef36c50ffaacb.png


配置文件优先级


65211d2755a44513b461c5d9b25b75c7.png


我们看日志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

相关文章
|
7天前
|
消息中间件 负载均衡 Java
Java一分钟之-Spring Cloud:微服务架构工具集
【6月更文挑战第8天】本文介绍了Spring Cloud的核心组件,包括Eureka(服务注册与发现)、Ribbon(客户端负载均衡)、Zuul(API网关)、Hystrix(断路器)、Spring Cloud Config(配置中心)和Spring Cloud Bus(事件总线)。文中强调了各组件的易错点,如Eureka的服务注册失败、Ribbon的配置、Zuul的路由错误、Hystrix的启用及配置、Config Server的加载失败和Bus的通讯问题,并给出了相应的代码示例和解决建议。在实际开发中,关注日志和使用调试工具是保证微服务系统稳定运行的关键。
89 6
|
9天前
|
运维 负载均衡 Java
认识微服务,认识Spring Cloud
认识微服务,认识Spring Cloud
62 2
|
9天前
|
Java Nacos Maven
Sentinel1.8.6更改配置同步到nacos(项目是Gateway)
Sentinel1.8.6 nacos springcloud springcloud-alibaba gateway
|
19天前
|
负载均衡 Nacos 数据库
【Nacos】配置管理、微服务配置拉取、实现配置热更新、多环境配置
【Nacos】配置管理、微服务配置拉取、实现配置热更新、多环境配置
36 1
|
19天前
|
监控 Dubbo Java
【SpringCloud】认识微服务
【SpringCloud】认识微服务
24 1
|
22天前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
28 3
Spring的加载配置文件、容器和获取bean的方式
|
1月前
|
Nacos
nacos 配置页面的模糊查询
nacos 配置页面的模糊查询
|
1月前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
33 1
|
Java 测试技术 开发工具
|
1月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
78 0