springcloud config 提取公共參數

  每一個微服務都有本身的配置文件application-local.yml和bootstrap.yml, 這個兩個配置文件的讀取順序是先讀取bootstrap.yml文件,在讀取application-local.yml文件,參數名相同,則後者覆蓋前者。
git

提取公共參數是由於,eurekazipkin等配置都是同樣的,每個微服務都要進行配置,很繁瑣,並且配置一多,就心煩。spring

提取公共配置參數,要藉助Spring Cloud Config,我這裏是把配置文件放在了gitlab上,固然你也能夠放在GitHub和其餘相關的代碼託管平臺上。以下圖

bootstrap

 

spring cloud config 官網裏有這麼一段話:app

With file-based (git, svn, and native) repositories, resources with file names in application* (application.propertiesapplication.ymlapplication-*.properties, and so on) are shared between all client applications. You can use resources with these file names to configure global defaults and have them be overridden by application-specific files as necessary.ide

The #_property_overrides[property overrides] feature can also be used for setting global defaults, with placeholders applications allowed to override them locally.svn

 

也就是說gitlab上的application-local.yml和application-test.yml能夠做爲全局的配置文件,後面的-test和-local是做爲佔位符同樣的存在。微服務

佔位符就是運行環境,dev、test、pro等,固然也能夠本身制定名字,記得在讀取config文件時指定好。就是下圖中profile:local的配置項。gitlab

 

 

必定要分清讀取順序:spa

首先會讀取bootstrap.yml裏面的參數,3d

而後會讀取gitlab上application.yml上的配置,

最後讀取微服務本地的application.yml文件。

參數名相同,後者覆蓋前者。

 

我本地的配置文件以下圖:

 

 這樣你就能夠把通用的公共配置參數提取到gitlab上的application文件上了,根據環境指定運行時要讀取的文件,也就是佔位符(-test、-local)

相關文章
相關標籤/搜索