作者其他技術文章
3)SpringCloud入門之Spring Boot多環境配置切換指南
5) Kibana從入門到精通
SpringBoot默認支持properties和YAML兩種格式的配置文件。前者格式簡單,但是只支持鍵值對。如果需要表達列表,最好使用YAML格式。SpringBoot支持自動加載約定名稱的配置文件,例如application.yml
。如果是自定義名稱的配置文件,就要另找方法了。可惜的是,不像前者有@PropertySource
這樣方便的加載方式,后者的加載必須借助編碼邏輯來實現。
一、bootstrap.yml(bootstrap.properties)與application.yml(application.properties)執行順序
bootstrap.yml(bootstrap.properties)用來在程序引導時執行,應用於更加早期配置信息讀取,如可以使用來配置application.yml中使用到參數等
application.yml(application.properties) 應用程序特有配置信息,可以用來配置后續各個模塊中需使用的公共參數等。
bootstrap.yml 先於 application.yml 加載
二、典型的應用場景如下:
- 當使用 Spring Cloud Config Server 的時候,你應該在 bootstrap.yml 里面指定 spring.application.name 和 spring.cloud.config.server.git.uri
- 和一些加密/解密的信息
技術上,bootstrap.yml 是被一個父級的 Spring ApplicationContext 加載的。這個父級的 Spring ApplicationContext是先加載的,在加載application.yml 的 ApplicationContext之前。
為何需要把 config server 的信息放在 bootstrap.yml 里?
當使用 Spring Cloud 的時候,配置信息一般是從 config server 加載的,為了取得配置信息(比如密碼等),你需要一些提早的引導配置。因此,把 config server 信息放在 bootstrap.yml,用來加載在這個時期真正需要的配置信息。
三、高級使用場景
啟動上下文
Spring Cloud會創建一個`Bootstrap Context`,作為Spring應用的`Application Context`的父上下文。初始化的時候,`Bootstrap Context`負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的`Environment`。`Bootstrap`屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。 `Bootstrap context`和`Application Context`有着不同的約定,所以新增了一個`bootstrap.yml`文件,而不是使用`application.yml` (或者`application.properties`)。保證`Bootstrap Context`和`Application Context`配置的分離。下面是一個例子: **bootstrap.yml**
spring: application: name: foo cloud: config: uri: ${SPRING_CONFIG_URI:http://localhost:8888}
推薦在`bootstrap.yml` or `application.yml`里面配置`spring.application.name`. 你可以通過設置`spring.cloud.bootstrap.enabled=false`來禁用`bootstrap`。
應用上下文層次結構
如果你通過`SpringApplication`或者`SpringApplicationBuilder`創建一個`Application Context`,那么會為spring應用的`Application Context`創建父上下文`Bootstrap Context`。在Spring里有個特性,子上下文會繼承父類的`property sources` and `profiles` ,所以`main application context` 相對於沒有使用Spring Cloud Config,會新增額外的`property sources`。額外的`property sources`有:
- “bootstrap” : 如果在
Bootstrap Context
掃描到PropertySourceLocator
並且有屬性,則會添加到CompositePropertySource。Spirng Cloud Config就是通過這種方式來添加的屬性的,詳細看源碼
ConfigServicePropertySourceLocator`。下面也也有一個例子自定義的例子。 - “applicationConfig: [classpath:bootstrap.yml]” ,(如果有
spring.profiles.active=production
則例如 applicationConfig: [classpath:/bootstrap.yml]#production): 如果你使用bootstrap.yml
來配置Bootstrap Context
,他比application.yml
優先級要低。它將添加到子上下文,作為Spring Boot應用程序的一部分。下文有介紹。
由於優先級規則,Bootstrap Context
不包含從bootstrap.yml
來的數據,但是可以用它作為默認設置。
你可以很容易的擴展任何你建立的上下文層次,可以使用它提供的接口,或者使用SpringApplicationBuilder
包含的方法(parent()
,child()
,sibling()
)。Bootstrap Context
將是最高級別的父類。擴展的每一個Context
都有有自己的bootstrap property source
(有可能是空的)。擴展的每一個Context
都有不同spring.application.name
。同一層層次的父子上下文原則上也有一有不同的名稱,因此,也會有不同的Config Server配置。子上下文的屬性在相同名字的情況下將覆蓋父上下文的屬性。
注意SpringApplicationBuilder
允許共享Environment
到所有層次,但是不是默認的。因此,同級的兄弟上下文不在和父類共享一些東西的時候不一定有相同的profiles
或者property sources
。
修改Bootstrap屬性配置
源碼位置BootstrapApplicationListener
。
String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}"); String configLocation = environment.resolvePlaceholders("${spring.cloud.bootstrap.location:}"); Map<String, Object> bootstrapMap = new HashMap<>();bootstrapMap.put("spring.config.name",configName); if(StringUtils.hasText(configLocation)){ bootstrapMap.put("spring.config.location", configLocation); }
bootstrap.yml
是由spring.cloud.bootstrap.name
(默認:”bootstrap”)或者spring.cloud.bootstrap.location
(默認空)。這些屬性行為與spring.config.*
類似,通過它的Environment
來配置引導ApplicationContext
。如果有一個激活的profile
(來源於spring.profiles.active
或者Environment
的Api構建),例如bootstrap-development.properties
就是配置了profile
為development
的配置文件.
覆蓋遠程屬性
property sources
被bootstrap context
添加到應用通常通過遠程的方式,比如”Config Server”。默認情況下,本地的配置文件不能覆蓋遠程配置,但是可以通過啟動命令行參數來覆蓋遠程配置。如果需要本地文件覆蓋遠程文件,需要在遠程配置文件里設置授權 spring.cloud.config.allowOverride=true
(這個配置不能在本地被設置)。一旦設置了這個權限,你可以配置更加細粒度的配置來配置覆蓋的方式,
比如:
- spring.cloud.config.overrideNone=true
覆蓋任何本地屬性
- spring.cloud.config.overrideSystemProperties=false
僅僅系統屬性和環境變量
源文件見PropertySourceBootstrapProperties
自定義啟動配置
bootstrap context
是依賴/META-INF/spring.factories
文件里面的org.springframework.cloud.bootstrap.BootstrapConfiguration
條目下面,通過逗號分隔的Spring @Configuration
類來建立的配置。任何main application context
需要的自動注入的Bean可以在這里通過這種方式來獲取。這也是ApplicationContextInitializer
建立@Bean
的方式。可以通過@Order
來更改初始化序列,默認是”last”。
# spring-cloud-context-1.1.1.RELEASE.jar # spring.factories # AutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshAutoConfiguration,\ org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration,\ org.springframework.cloud.autoconfigure.LifecycleMvcEndpointAutoConfiguration # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.cloud.bootstrap.BootstrapApplicationListener,\ org.springframework.cloud.context.restart.RestartListener # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\ org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
- 警告
小心,你添加的自定義
BootstrapConfiguration
類沒有錯誤的@ComponentScanned
到你的主應用上下文,他們可能是不需要的。使用一個另外的包不被@ComponentScan
或者@SpringBootApplication
注解覆蓋到。
bootstrap context
通過spring.factories
配置的類初始化的所有的Bean都會在SpingApplicatin啟動前加入到它的上下文里去。
自定義引導配置來源:Bootstrap Property Sources
默認的`property source`添加額外的配置是通過配置服務(Config Server),你也可以自定義添加`property source`通過實現`PropertySourceLocator`接口來添加。你可以使用它加配置屬性從不同的服務、數據庫、或者其他。
- 下面是一個自定義的例子:
@Configuration public class CustomPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource<?> locate(Environment environment) { return new MapPropertySource("customProperty", Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended")); } }
Environment
被ApplicationContext
建立,並傳入property sources
(可能不同個profile
有不同的屬性),所以,你可以從Environment
尋找找一些特別的屬性。比如spring.application.name
,它是默認的Config Server property source
。
如果你建立了一個jar包,里面添加了一個META-INF/spring.factories
文件:
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那么,”customProperty“的PropertySource
將會被包含到應用。