應(yīng)用的配置源通常都是遠端的Config Server服務(wù)器,默認情況下,本地的配置優(yōu)先級低于遠端配置倉庫。如果想實現(xiàn)本地應(yīng)用的系統(tǒng)變量和config文件覆蓋遠端倉庫中的屬性值,可以通過如下設(shè)置:
spring:cloud:config:allowOverride: trueoverrideNone: trueoverrideSystemProperties: false
客戶端通過如上配置,可以實現(xiàn)本地配置優(yōu)先級更高,且不能被覆蓋。由于我們基于的Spring Cloud
當前版本是 Edgware.RELEASE
,上面的設(shè)置并不能起作用,而是使用了 PropertySourceBootstrapProperties
中的默認值。具體情況見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) 。首先用當前運行應(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ù)默認的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 方法中進行了哪些操作。
返回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 對象的是被直接初始化,使用的是默認的屬性值而并未注入我們在配置文件中設(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 覆寫遠端的配置屬性實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答
圖片精選