Spring Cloud 覆寫遠(yuǎn)端的配置屬性實例詳解
應(yīng)用的配置源通常都是遠(yuǎn)端的Config Server服務(wù)器,默認(rèn)情況下,本地的配置優(yōu)先級低于遠(yuǎn)端配置倉庫。如果想實現(xiàn)本地應(yīng)用的系統(tǒng)變量和config文件覆蓋遠(yuǎn)端倉庫中的屬性值,可以通過如下設(shè)置:
spring: cloud: config: allowOverride: true overrideNone: true overrideSystemProperties: false
- overrideNone:當(dāng)allowOverride為true時,overrideNone設(shè)置為true,外部的配置優(yōu)先級更低,而且不能覆蓋任何存在的屬性源。默認(rèn)為false
- allowOverride:標(biāo)識overrideSystemProperties屬性是否啟用。默認(rèn)為true,設(shè)置為false意為禁止用戶的設(shè)置
- overrideSystemProperties:用來標(biāo)識外部配置是否能夠覆蓋系統(tǒng)屬性,默認(rèn)為true
客戶端通過如上配置,可以實現(xiàn)本地配置優(yōu)先級更高,且不能被覆蓋。由于我們基于的Spring Cloud
當(dāng)前版本是 Edgware.RELEASE
,上面的設(shè)置并不能起作用,而是使用了 PropertySourceBootstrapProperties
中的默認(rèn)值。具體情況見issue: https://github.com/spring-cloud/spring-cloud-commons/pull/250 ,我們在下面分析時會講到具體的bug源。
源碼分析
ConfigServicePropertySourceLocator
覆寫遠(yuǎn)端的配置屬性歸根結(jié)底與客戶端的啟動時獲取配置有關(guān),在獲取到配置之后如何處理?我們看一下spring cloud config中的資源獲取類 ConfigServicePropertySourceLocator 的類圖。
ConfigServicePropertySourceLocator 實質(zhì)是一個屬性資源定位器,其主要方法是 locate(Environment environment) 。首先用當(dāng)前運行應(yīng)用的環(huán)境的application、profile和label替換configClientProperties中的占位符并初始化RestTemplate,然后遍歷labels數(shù)組直到獲取到有效的配置信息,最后還會根據(jù)是否快速失敗進(jìn)行重試。主要流程如下:
locate(Environment environment) 調(diào)用 getRemoteEnvironment(restTemplate, properties, label, state) 方法通過http的方式獲取遠(yuǎn)程服務(wù)器上的配置數(shù)據(jù)。實現(xiàn)也很簡單,顯示替換請求路徑path中占位符,然后進(jìn)行頭部headers組裝,組裝好了就可以發(fā)送請求,最后返回結(jié)果。
在上面的實現(xiàn)中,我們看到獲取到的配置信息存放在 CompositePropertySource ,那是如何使用它的呢?這邊補充另一個重要的類是PropertySourceBootstrapConfiguration,它實現(xiàn)了ApplicationContextInitializer接口,該接口會在應(yīng)用上下文刷新之前 refresh() 被回調(diào),從而執(zhí)行初始化操作,應(yīng)用啟動后的調(diào)用棧如下:
SpringApplicationBuilder.run() -> SpringApplication.run() -> SpringApplication.createAndRefreshContext() -> SpringApplication.applyInitializers() -> PropertySourceBootstrapConfiguration.initialize() PropertySourceBootstrapConfiguration
而上述 ConfigServicePropertySourceLocator 的locate方法會在initialize中被調(diào)用,從而保證上下文在刷新之前能夠拿到必要的配置信息。具體看一下initialize方法:
public class PropertySourceBootstrapConfigurationimplements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered { private int order = Ordered.HIGHEST_PRECEDENCE + 10; @Autowired(required = false) private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>(); @Override public void initialize(ConfigurableApplicationContext applicationContext){ CompositePropertySource composite = new CompositePropertySource( BOOTSTRAP_PROPERTY_SOURCE_NAME); //對propertySourceLocators數(shù)組進(jìn)行排序,根據(jù)默認(rèn)的AnnotationAwareOrderComparator AnnotationAwareOrderComparator.sort(this.propertySourceLocators); boolean empty = true; //獲取運行的環(huán)境上下文 ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySourceLocator locator : this.propertySourceLocators) { //遍歷this.propertySourceLocators PropertySource<?> source = null; source = locator.locate(environment); if (source == null) { continue; } logger.info("Located property source: " + source); //將source添加到PropertySource的鏈表中 composite.addPropertySource(source); empty = false; } //只有source不為空的情況,才會設(shè)置到environment中 if (!empty) { //返回Environment的可變形式,可進(jìn)行的操作如addFirst、addLast MutablePropertySources propertySources = environment.getPropertySources(); String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { //移除bootstrapProperties propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } //根據(jù)config server覆寫的規(guī)則,設(shè)置propertySources insertPropertySources(propertySources, composite); reinitializeLoggingSystem(environment, logConfig, logFile); setLogLevels(environment); //處理多個active profiles的配置信息 handleIncludedProfiles(environment); } } //... }
下面我們看一下,在 initialize 方法中進(jìn)行了哪些操作。
- 根據(jù)默認(rèn)的 AnnotationAwareOrderComparator 排序規(guī)則對propertySourceLocators數(shù)組進(jìn)行排序
- 獲取運行的環(huán)境上下文ConfigurableEnvironment
- 遍歷propertySourceLocators時
- 調(diào)用 locate 方法,傳入獲取的上下文environment
- 將source添加到PropertySource的鏈表中
- 設(shè)置source是否為空的標(biāo)識標(biāo)量empty
- source不為空的情況,才會設(shè)置到environment中
返回Environment的可變形式,可進(jìn)行的操作如addFirst、addLast
移除propertySources中的bootstrapProperties
根據(jù)config server覆寫的規(guī)則,設(shè)置propertySources
處理多個active profiles的配置信息
初始化方法 initialize 處理時,先將所有PropertySourceLocator類型的對象的 locate 方法遍歷,然后將各種方式得到的屬性值放到CompositePropertySource中,最后調(diào)用 insertPropertySources(propertySources, composite) 方法設(shè)置到Environment中。Spring Cloud Context中提供了覆寫遠(yuǎn)端屬性的 PropertySourceBootstrapProperties ,利用該配置類進(jìn)行判斷屬性源的優(yōu)先級。
private void insertPropertySources(MutablePropertySources propertySources, CompositePropertySource composite) { MutablePropertySources incoming = new MutablePropertySources(); incoming.addFirst(composite); PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties(); new RelaxedDataBinder(remoteProperties, "spring.cloud.config") .bind(new PropertySourcesPropertyValues(incoming)); //如果不允許本地覆寫 if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone() && remoteProperties.isOverrideSystemProperties())) { propertySources.addFirst(composite); return; } //overrideNone為true,外部配置優(yōu)先級最低 if (remoteProperties.isOverrideNone()) { propertySources.addLast(composite); return; } if (propertySources .contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { //根據(jù)overrideSystemProperties,設(shè)置外部配置的優(yōu)先級 if (!remoteProperties.isOverrideSystemProperties()) { propertySources.addAfter( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite); } else { propertySources.addBefore( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite); } } else { propertySources.addLast(composite); } }
上述實現(xiàn)主要是根據(jù) PropertySourceBootstrapProperties 中的屬性,調(diào)整多個配置源的優(yōu)先級。從其實現(xiàn)可以看到 PropertySourceBootstrapProperties 對象的是被直接初始化,使用的是默認(rèn)的屬性值而并未注入我們在配置文件中設(shè)置的。
修復(fù)后的實現(xiàn):
@Autowired(required = false) private PropertySourceBootstrapProperties remotePropertiesForOverriding; @Override public int getOrder(){ return this.order; private void insertPropertySources(MutablePropertySources propertySources, CompositePropertySource composite) { MutablePropertySources incoming = new MutablePropertySources(); incoming.addFirst(composite); PropertySourceBootstrapProperties remoteProperties = remotePropertiesForOverriding == null ? new PropertySourceBootstrapProperties() : remotePropertiesForOverriding;
總結(jié)
以上所述是小編給大家介紹的Spring Cloud 覆寫遠(yuǎn)端的配置屬性實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Mybatis?selectKey 如何返回新增用戶的id值
這篇文章主要介紹了Mybatis?selectKey 如何返回新增用戶的id值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01詳解Spring Data操作Redis數(shù)據(jù)庫
Redis是一種NOSQL數(shù)據(jù)庫,Key-Value形式對數(shù)據(jù)進(jìn)行存儲,其中數(shù)據(jù)可以以內(nèi)存形式存在,也可以持久化到文件系統(tǒng)。Spring data對Redis進(jìn)行了很好的封裝,用起來也是十分的得心應(yīng)手,接下來通過本文給大家分享Spring Data操作Redis數(shù)據(jù)庫,需要的朋友參考下2017-03-03springboot為異步任務(wù)規(guī)劃自定義線程池的實現(xiàn)
本文主要介紹了springboot為異步任務(wù)規(guī)劃自定義線程池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例
本文主要介紹了Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringMVC4+MyBatis+SQL Server2014實現(xiàn)數(shù)據(jù)庫讀寫分離
這篇文章主要介紹了SpringMVC4+MyBatis+SQL Server2014實現(xiàn)讀寫分離,需要的朋友可以參考下2017-04-04Java貪心算法之Prime算法原理與實現(xiàn)方法詳解
這篇文章主要介紹了Java貪心算法之Prime算法原理與實現(xiàn)方法,簡單描述了Prime算法的概念、原理、實現(xiàn)與使用技巧,需要的朋友可以參考下2017-09-09詳解多云架構(gòu)下的JAVA微服務(wù)技術(shù)解析
本文介紹了基于開源自建和適配云廠商開發(fā)框架兩種構(gòu)建多云架構(gòu)的思路,以及這些思路的優(yōu)缺點2021-05-05Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊列的實現(xiàn)
循環(huán)隊列 (Circular Queue) 是一種特殊的隊列。循環(huán)隊列解決了隊列出隊時需要將所有數(shù)據(jù)前移一位的問題。本文將帶大家詳細(xì)了解循環(huán)隊列如何實現(xiàn),需要的朋友可以參考一下2021-12-12Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析
今天給大家?guī)淼氖顷P(guān)于Java源碼的相關(guān)知識,文章圍繞著Java ImmutableMap展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下,希望能給你帶來幫助2021-06-06