做者其余技术文章html
1) Spring Boot 简介git
2)SpringCloud入门之YAML格式文件规范学习spring
3)SpringCloud入门之Spring Boot多环境配置切换指南数据库
4) Elasticsearch从入门到精通bootstrap
5) Kibana从入门到精通性能优化
6) logstash快速入门实战指南app
7)Oracle性能优化之查询语句通用原则 ide
8)Redis经常使用命令性能
SpringBoot默认支持properties和YAML两种格式的配置文件。前者格式简单,可是只支持键值对。若是须要表达列表,最好使用YAML格式。SpringBoot支持自动加载约定名称的配置文件,例如application.yml
。若是是自定义名称的配置文件,就要另找方法了。惋惜的是,不像前者有@PropertySource
这样方便的加载方式,后者的加载必须借助编码逻辑来实现。
1、bootstrap.yml(bootstrap.properties)与application.yml(application.properties)执行顺序
bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,能够用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 先于 application.yml 加载
2、典型的应用场景以下:
技术上,bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的。这个父级的 Spring ApplicationContext是先加载的,在加载application.yml 的 ApplicationContext以前。
为什么须要把 config server 的信息放在 bootstrap.yml 里?
当使用 Spring Cloud 的时候,配置信息通常是从 config server 加载的,为了取得配置信息(好比密码等),你须要一些提前的引导配置。所以,把 config server 信息放在 bootstrap.yml,用来加载在这个时期真正须要的配置信息。
3、高级使用场景
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 Context
扫描到PropertySourceLocator
而且有属性,则会添加到CompositePropertySource。Spirng Cloud Config就是经过这种方式来添加的属性的,详细看源码
ConfigServicePropertySourceLocator`。下面也也有一个例子自定义的例子。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
。
源码位置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启动前加入到它的上下文里去。
默认的`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
将会被包含到应用。