當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式
生活随笔
收集整理的這篇文章主要介紹了
Spring Cloud Alibaba - 19 Nacos Config配置中心加载不同微服务的通用配置的两种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- Pre
- 實現
- 方式一 通過 shared-dataids 方式
- 方式二 通過 ext-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.yml3.2): common3.yml 4) shared-dataids 不同工程 通用配置4.1)common2.yml4.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