1. Spring Cloud Context: Application Context Services(应用上下文服务)

1.1 The Bootstrap Application Context(引导上下文)

一个spring cloud应用会创建一个“bootstrap”context,它是主应用的parent context。它负责加载外部资源的配置属性并且解释本地外部配置文件中的属性。

这里有两个context,一个是spring boot的main context,另一个是spring cloud的bootstrap context,这两个context共享同一个环境,也就是说他们共享spring项目的外部配置属性。

默认情况下,bootstrap属性(并非是bootstrap.properties而是那些在bootstrap阶段加载的属性)是以高优先级的方式添加的,所以无法被本地配置覆盖。

bootstrap context和main context使用不同的方法定位外部配置,你可以使用bootstrap.yml来替代application.yml来为bootstrap context添加配置,这样就可以区分开bootstrap context和main context。

可以通过在系统属性中设置spring.cloud.bootstrap.enabled=false来禁用bootstrap程序。

1.2 Application Context Hierarchies(应用上下文层级)

如果你通过SpringApplication或者SpringApplicationBuilder构建了一个application context,那么bootstrap context将会作为它的parent context被添加。

spring的一个特性是child context会从它的parent context中继承属性资源和配置文件,因此main application context有一些额外的属性资源:

“bootstrap”:如果在bootstrap context中发现PropertySourceLocators并且含有非空属性,那么一个CompositePropertySource将会以高优先级出现。

“applicationConfig”:如果你有一个bootstrap.yml,并且设置了配置bootstrap context的属性,那么它们将会被添加到child context中。但是它们具有比application.yml或者其他配置更低的优先级。

由于资源属性的排序规则,“bootstrap”入口具有高优先级。注意这不包括bootstrap.yml中的数据(具有较低优先级,可以用来设置默认属性)。

1.3 Changing the Location of Bootstrap Properties

bootstrap.yml可以通过在系统属性中设置spring.cloud.bootstrap.name或者spring.cloud.bootstrap.location来指定。

如果有一个激活的配置文件(通过spring.profiles.active或者Environment API设置),那么这些文件中的属性都会被加载。

1.4 Overriding the Values of Remote Properties(覆盖远程属性的值)

通过bootstrap context添加到应用中的属性资源可能经常是“远程”的,例如从Spring Cloud Config Server读取的属性。默认情况下,他们不能被本地覆盖。

如果你希望让你的应用通过系统属性或者本地配置来重写那些远程配置,可以通过设置远程属性资源spring.cloud.config.allowOverride=true(在本地设置无效)。

一旦设置了上面的标志,就可以通过下面两个远程属性来控制远程属性和系统属性跟本地配置的关系:

spring.cloud.config.overrideNone=true:远程属性可以被本地任意属性资源覆盖

spring.cloud.config.overrideSystemProperties=false:仅仅系统属性,命令行参数和环境变量(不包括配置文件)可以覆盖远程设置。

1.5 Customizing the Bootstrap Configuration(自定义bootstrap配置)

bootstrap context可以被设置来做任何你想要做的事,只要在/META-INF/spring.factories文件中配置org.springframework.cloud.bootstrap.BootstrapConfiguration的值即可。

它的值是以逗号分隔的@Configuration类的全限定名。所以任何你想要在main application context中注入的bean都可以在这里配置。如果你希望控制启动顺序,在类上添加@Order注解(默认顺序为最后)。

注意:不要bootstrap配置被main context加载到,即不能被@ComponentScan和@SpringBootApplication注解的配置类覆盖到。

bootstrap程序最后将初始化器注入到main SpringApplication实例中。首先,通过spring.factories中配置的类来创建bootstrap context,然后所有ApplicationContextInitializer的bean将会被添加到main SpringApplication中,在其启动前。

1.6 Customizing the Bootstrap Property Sources(定制化bootstrap属性资源)

通过bootstrap程序添加的外部配置的默认属性资源是Spring Cloud Config Server。但是可以通过添加PropertySourceLocator类型的bean到bootstrap context中(通过spring.factories)来添加额外的资源。

举个

spring cloud连载第一篇之bootstrap context的更多相关文章

  1. spring cloud连载第二篇之Spring Cloud Config

    Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持.通过Spring Cloud Config你可以有一个统一的地方来管理所有 ...

  2. Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案

    一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...

  3. Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

    本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...

  4. Spring Cloud第十篇 | 分布式配置中心Config

    ​ 本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  5. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  6. Spring Cloud第十三篇 | Spring Boot Admin服务监控

    本文是Spring Cloud专栏的第十三篇文章,了解前十二篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  7. spring boot实战(第一篇)第一个案例

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...

  8. Spring Cloud第四篇 | 客户端负载均衡Ribbon

    ​ 本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: ​Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  9. Spring Cloud第五篇 | 服务熔断Hystrix

    ​ 本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

随机推荐

  1. Linux带有时间控制的多进程bash脚本

    目标 以可控制的多进程执行,达到最大执行时长后停止脚本. 思路 1.产生fifo管道,并预填充n个值(与并发数相等) 2.记录脚本本身PID并启动计时器进程(计时终止后杀脚本本身PID) 3.并发执行 ...

  2. airport 抓包

    链接airport命令: ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources ...

  3. web api 请求结果中页面显示的json字符串与json对象结果不一致

    我在前端调用这个api的时候也是百思不得其解,明明看到页面上的结果ID是不一样的,但是在js中使用的时候,却一直有重复ID的情况 后来才发现原来是long这个类型的原因,JavaScript中Numb ...

  4. linux下使用supervisor启动.net core mvc website的配置

    发布好的asp.net core mvc项目, 如果想在window或linux下的以控制台程序启动的话,可以用下面的命令 dotnet MyProject.dll --urls="http ...

  5. brew安装指定版本boost

    brew 如何安装指定版本的boost brew uninstall boost brew install boost@1.57 brew link boost@1.57 --force --over ...

  6. iOS错误 - too many open files (error = 24)

    碰到这个错误是在用 UIImageView 显示图片的时候.UIImage 用的是 imageNamed 方法.错误原因是打开了太多的文件.应该是太多文件的打开导致了 UIImage 的 cache ...

  7. leetcode 72 编辑距离 JAVA

    题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例  ...

  8. django 结合 OPTIONS方法 处理跨域请求(单个视图方法中)

    OPTIONS 方法比较少见,该方法用于请求服务器告知其支持哪些其他的功能和方法.通过 OPTIONS 方法,可以询问服务器具体支持哪些方法,或者服务器会使用什么样的方法来处理一些特殊资源.可以说这是 ...

  9. day 09 课后作业

    # -*- coding: utf-8 -*-# @Time : 2018/12/28 14:25# @Author : Endless-cloud# @Site : # @File : 08 课后作 ...

  10. java集合中的HashMap源码分析

    1.hashMap中的成员分析 transient Node<K,V>[] table; //为hash桶的数量 /** * The number of key-value mapping ...