日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringCloud覆寫遠(yuǎn)端配置屬性的示例分析

這篇文章主要介紹Spring Cloud覆寫遠(yuǎn)端配置屬性的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計、宣城網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

應(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的類圖。

Spring Cloud覆寫遠(yuǎn)端配置屬性的示例分析 

ConfigServicePropertySourceLocator 實質(zhì)是一個屬性資源定位器,其主要方法是 locate(Environment environment) 。首先用當(dāng)前運(yùn)行應(yīng)用的環(huán)境的application、profile和label替換configClientProperties中的占位符并初始化RestTemplate,然后遍歷labels數(shù)組直到獲取到有效的配置信息,最后還會根據(jù)是否快速失敗進(jìn)行重試。主要流程如下:

Spring Cloud覆寫遠(yuǎ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 ,那是如何使用它的呢?這邊補(bǔ)充另一個重要的類是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, Ordered {
 private int order = Ordered.HIGHEST_PRECEDENCE + 10;
 @Autowired(required = false)
 private List 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;
 //獲取運(yùn)行的環(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)行排序

  • 獲取運(yù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;

以上是“Spring Cloud覆寫遠(yuǎn)端配置屬性的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


文章名稱:SpringCloud覆寫遠(yuǎn)端配置屬性的示例分析
文章URL:http://www.dlmjj.cn/article/jdsejj.html