這篇文章主要介紹Spring Cloud覆寫遠端配置屬性的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司是專業(yè)的天壇街道網(wǎng)站建設(shè)公司,天壇街道接單;提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行天壇街道網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!應(yīng)用的配置源通常都是遠端的Config Server服務(wù)器,默認(rèn)情況下,本地的配置優(yōu)先級低于遠端配置倉庫。如果想實現(xiàn)本地應(yīng)用的系統(tǒng)變量和config文件覆蓋遠端倉庫中的屬性值,可以通過如下設(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
覆寫遠端的配置屬性歸根結(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ù)是否快速失敗進行重試。主要流程如下:
locate(Environment environment) 調(diào)用 getRemoteEnvironment(restTemplate, properties, label, state) 方法通過http的方式獲取遠程服務(wù)器上的配置數(shù)據(jù)。實現(xiàn)也很簡單,顯示替換請求路徑path中占位符,然后進行頭部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ù)默認(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的可變形式,可進行的操作如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ù)默認(rèn)的 AnnotationAwareOrderComparator 排序規(guī)則對propertySourceLocators數(shù)組進行排序
獲取運行的環(huán)境上下文ConfigurableEnvironment
遍歷propertySourceLocators時
調(diào)用 locate 方法,傳入獲取的上下文environment
將source添加到PropertySource的鏈表中
設(shè)置source是否為空的標(biāo)識標(biāo)量empty
source不為空的情況,才會設(shè)置到environment中
返回Environment的可變形式,可進行的操作如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中提供了覆寫遠端屬性的 PropertySourceBootstrapProperties ,利用該配置類進行判斷屬性源的優(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覆寫遠端配置屬性的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:SpringCloud覆寫遠端配置屬性的示例分析-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://aaarwkj.com/article20/cdhdjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、企業(yè)建站、定制網(wǎng)站、品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容