https://www.jianshu.com/p/8eeffa9fdb59
今天我們一起學習一下 Bootstrap 配置的相關知識,在學習目標中我已經列出了今天需要學習的知識點,第一個知識點為復習知識點,屬於 Spring Boot 中的知識,這里我們既然講到了配置文件,就順便把 Spring Boot 支持的兩種配置文件類型的相關知識復習一下,有助於我們對 Bootstrap 配置的學習。
首先我們先創建項目:打開 https://start.spring.io/,填寫相關信息,添加 Web、Actuator 以及 Cloud Bootstrap 依賴,點擊 “Generate Project” 按鈕生成項目,並導入到 idea 中。(注:此處使用的 Spring Boot 版本為 1.X 系列)

一、(復習)為什么 Spring Boot 可以支持兩種配置文件類型
熟悉 Spring Boot 的小伙伴一定知道 Spring Boot 是支持兩種配置文件類型的:
application.yaml / application.yml
application.properties
這里我們通過源代碼查找的方式,看一下為什么 Spring Boot 會支持這兩種配置文件類型。
首先,我們要找到加載上下文環境的類 **ConfigFileApplicationListener**
,這個類是通過加載本地已知的配置文件中的屬性來配置上下文環境。
然后在這個類中找到 **Loader#load()**
** **方法,查看 **PropertySourcesLoader **
源碼,找到
private final List<PropertySourceLoader> loaders;
這里我們查看一下 **PropertySourceLoader **
接口的實現類,可以發現,有兩個類實現了該接口:
**PropertiesPropertySourceLoader**
**YamlPropertySourceLoader**
具體代碼就不再展開講解了,已經十分明了了。
二、Bootstrap 配置文件
我們都知道,Spring Boot 中使用 application.properties
配置文件,通過上篇文章對 Bootstrap 上下文的學習,我們應該也能猜到,在 Spring Cloud 中使用 bootstrap.properties
配置文件。也就是說,在 Spring Cloud 項目中,application.properties
和 bootstrap.properties
配置文件是同時存在的。
在 **BootstrapApplicationListener#onApplicationEvent()**
** **方法中,可以看出當 spring.cloud.bootstrap.name:bootstrap
存在時,使用該配置項,否則,使用 "bootstrap" 默認值。
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
所以我們在項目的 resources
目錄下新建名為 bootstrap.properties
的配置文件,這個就是我們 Spring Cloud 中的配置文件。
三、調整 Bootstrap 配置文件名稱
由於 Spring Cloud 的默認配置文件為 bootstrap.properties
,那如果我們想要修改配置文件的名稱應該如何做呢?這里我們使用調整程序啟動參數的方式來進行修改。
首先我們在 Idea 中添加如下程序啟動參數:
--spring.cloud.bootstrap.name=spring-cloud

bootstrap 配置文件名稱發生了改變,改變成了 "spring-cloud",現有三個文件:( 標黑的為我們的期望值)
- application.properties
- spring.application.name=spring-cloud-client
- bootstrap.properties
- spring.application.name = spring-cloud-config-client-demo
- spring-cloud.properties
- spring.application.name = spring-cloud
其中 application.properties
(ConfigFileApplicationListener
被加載了)和 spring-cloud.properties
讀取了,這是我們所期望的。
重啟項目,在 http://localhost:8080/env 中查看結果,結果也是和我們所期望的是相同的:

四、調整 Bootstrap 配置文件路徑
既然配置文件名稱是可以修改的,那么接下來我們嘗試使用類似的方式修改一下 Bootstrap 配置文件路徑,這里我們在 resources
目錄下新建 config/spring-cloud2.properties
文件,添加如下程序啟動參數:
--spring.cloud.bootstrap.name=spring-cloud2
--spring.cloud.bootstrap.location=config

現有四個文件:( 標黑的為我們的期望值)
- application.properties
- spring.application.name=spring-cloud-client
- bootstrap.properties
- spring.application.name = spring-cloud-config-client-demo
- spring-cloud.properties
- spring.application.name = spring-cloud
- config/spring-cloud2.properties
- spring.application.name = spring-cloud2
重啟項目,在 http://localhost:8080/env 中查看結果,結果也是和我們所期望的是相同的:

五、自定義 Bootstrap 配置(實現 Spring 標准接口 )
上面說了這么多 Bootstrap 配置相關的東西,一直使用的都是別人的配置項,接下來我們來自定義配置,首先我們實現 Spring 的標准接口來完成自定義Bootstrap 配置 :
1、創建 META-INF/spring.factories
文件(類似於 Spring Boot 自定義 Starter)
2、創建自定義 Bootstrap 配置 Configuration
package top.alanshelby.springcloudchapter2.bootstrap; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import java.util.HashMap; import java.util.Map; /** * Bootstrap 配置 Bean * * @ClassName MyConfiguration * @Author AlanShelby * @Date 2019-04-22 14:29:14 14:29 * @Version 1.0 */ @Configuration public class MyConfiguration implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); // 獲取 PropertySource MutablePropertySources propertySources = environment.getPropertySources(); // 定義一個新的 PropertySource 放在首位 propertySources.addFirst(createPropertySource()); } private PropertySource createPropertySource() { Map<String, Object> source = new HashMap<>(); source.put("name", "AlanShelby"); PropertySource propertySource = new MapPropertySource("my-property-source", source); return propertySource; } }
上面的代碼是我們定義了一個新的 PropertySource
並將其放到了 MutablePropertySources
首位,這個配置項的 Key 值是 my-property-source
,里面對應的屬性信息為 source.put("name", "AlanShelby")
3、配置 META-INF/spring.factories
關聯 Key org.springframework.cloud.bootstrap.BootstrapConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration =top.alanshelby.springcloudchapter2.bootstrap.MyConfiguration
然后瀏覽器訪問 http://localhost:8080/env,可以看到以下信息,表示我們自定義的配置成功了:

六、自定義 Bootstrap 配置(實現 SpringCloud 的接口)
上面我們使用了實現 Spring 標准接口的方式自定義了 Bootstrap 配置項,接下來我們來實現 Spring Cloud 接口來實現該功能。
1、實現 PropertySourceLocator
package top.alanshelby.springcloudchapter2; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.env.*; import java.util.HashMap; import java.util.Map; /** * 自定義 Bootstrap 配置屬性源(實現 SpringCloud 的接口) * * @ClassName MyPropertySourceLocator * @Author AlanShelby * @Date 2019-04-22 14:45:15 14:45 * @Version 1.0 */ public class MyPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource<?> locate(Environment environment) { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment); MutablePropertySources propertySources = configurableEnvironment.getPropertySources(); propertySources.addFirst(createPropertySource()); } return null; } private PropertySource createPropertySource() { Map<String, Object> source = new HashMap<>(); source.put("spring.application.name", "AlanShelby-SpringCloud"); PropertySource propertySource = new MapPropertySource("alan-property-source", source); return propertySource; } }
2、配置 META-INF/spring.factories
關聯 Key org.springframework.cloud.bootstrap.BootstrapConfiguration
:
org.springframework.cloud.bootstrap.BootstrapConfiguration = top.alanshelby.springcloudchapter2.MyPropertySourceLocator
然后瀏覽器訪問 http://localhost:8080/env,可以看到以下信息,表示我們自定義的配置成功了:

至此,關於 Spring Cloud 中的 Bootstrap 配置就講解完了,這是我的理解,各位看官如果有不同見解或文章中有錯誤,請不吝指正。
所用代碼碼雲地址:https://gitee.com/AlanShelby/spring-cloud-chapter
知乎專欄地址:https://zhuanlan.zhihu.com/c_200981602
個人微信公眾號:AlanShelby(多多關注,感謝~)
作者:AlanShelby
鏈接:https://www.jianshu.com/p/8eeffa9fdb59
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。