Spring Cloud Context模块
SpringCloud这个框架本身是建立在SpringBoot基础之上的,所以使用SpringCloud的方式与SpringBoot相仿。也是通过类似如下代码进行启动。
SpringApplication.run(XxxApplication.class, args);
其中 XxxApplication.class 类上也需要添加 @SpringBootApplication注解。
要使用SpringCloud框架,在pom文件中要确保引入 spring-cloud-starter 依赖包, spring-cloud-starter 依赖如下的 jar :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
</dependencies>
其中 spring-cloud-context-x.y.z.RELEASE.jar 和 spring-cloud-commons-x.y.z.RELEASE.jar 下的 META-INF 目录下都包含 spring.factories 文件,所以可以把这两个jar看作是springCloud程序的入口。
SpringCloud在构建上下文 (即ApplicationContext实例)时,采用了Spring父子容器的设计,会在 SpringBoot构建的容器(后面称之为应用容器)之上创建一父容器 Bootstrap Application Context .
那么SpringCloud设计出Bootstrap Application Context ,并把它作为 应用容器的父容器的目的是什么呢:
因为SpringCloud 作为一个微服务框架,需要使用全局的配置中心,而配置中心的配置是可以提供给应用容器的,所以在应用容器初始化和实例化Bean之前需要先完成配置中心的实例化,这个任务就由Bootstrap Application Context 来完成,而配置中心的相关配置属性就从bootstrap.properties或bootstrap.yml文件中读取。
但要注意的是,在Bootstrap Application Context 启动工作完成之后,其从bootstrap.properties或bootstrap.yml文件中读取的配置,是会被应用容器对应的application.properties或yml文件中的同名属性覆盖的。
下面从源码角度来分析上面的论述:
1. 代码运行时还是从SpringApplication实例的run方法开始 ,此处会触发 BootstrapApplicationListener 类中的代码 , Bootstrap Application Context 的创建就是通过这个监听器触发的
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//此处会加载spring-cloud-context提供的监听器org.springframework.cloud.bootstrap.BootstrapApplicationListener.class
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//此处会发布ApplicationEnvironmentPreparedEvent事件,触发BootstrapApplicationListener中的代码
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
2. Bootstrap Application Context 的实例化 ,由BootstrapApplicationListener类的 onApplicationEvent方法触发
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
//可以通过环境变量 spring.cloud.bootstrap.enabled来禁止使用Bootstrap容器
if (!environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class,
true)) {
return;
}
// 由于Bootstrap容器在创建时还是会再次调用上面步骤1的代码,还会再次触发BootstrapApplicationListener类这个方法,所以此处作个判断,
如果当前是Bootstrap容器的处理,则直接返回
if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
return;
}
ConfigurableApplicationContext context = null;
//获取配置文件的名字,默认为bootstrap.properties或.yml ,并且这个名字可以通过 spring.cloud.bootstrap.name在环境中配置
String configName = environment
.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
for (ApplicationContextInitializer<?> initializer : event.getSpringApplication()
.getInitializers()) {
//从相应jar中的spring.factories文件中读取初始化器的配置类实例,如果这个实例类是ParentContextApplicationContextInitializer类型
则直接从该类中获取到父容器,默认情况下,没有提供这样的类,下面这段代码会跳过
if (initializer instanceof ParentContextApplicationContextInitializer) {
context = findBootstrapContext(
(ParentContextApplicationContextInitializer) initializer,
configName);
}
}
if (context == null) {
//此处分析见步骤3
context = bootstrapServiceContext(environment, event.getSpringApplication(),
configName);
event.getSpringApplication().addListeners(new CloseContextOnFailureApplicationListener(context));
}
apply(context, event.getSpringApplication(), environment);
}
3. bootstrap容器的创建
private ConfigurableApplicationContext bootstrapServiceContext(
ConfigurableEnvironment environment, final SpringApplication application,
String configName) {
/**
此处代码主要是从各处获取属性配置,此处忽略
**/
// TODO: is it possible or sensible to share a ResourceLoader?
SpringApplicationBuilder builder = new SpringApplicationBuilder()
.profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
.environment(bootstrapEnvironment)
// Don't use the default properties in this builder
.registerShutdownHook(false).logStartupInfo(false)
.web(WebApplicationType.NONE); //SpringApplicationBuilder的作用:1.构建SpringApplication 2.构建ApplicationContext
//这里需要思考一下,springboot在启动时已经构建了一个SpringApplication实例,为何此处又构建了一个
//这是因为这个SpringApplication实例的构建环境和SringBoot原生构建的那个不同,看一下上一行代码就能明白
final SpringApplication builderApplication = builder.application();
if(builderApplication.getMainApplicationClass() == null){
builder.main(application.getMainApplicationClass());
}
if (environment.getPropertySources().contains("refreshArgs")) {
builderApplication
.setListeners(filterListeners(builderApplication.getListeners()));
}
//从springFactories文件中查找BootstrapConfiguration的配置类
builder.sources(BootstrapImportSelectorConfiguration.class); //构建出BootstrapContext
final ConfigurableApplicationContext context = builder.run();
context.setId("bootstrap");
// 设置BootstrapContext成为应用Context的父容器,此处分析见步骤4
addAncestorInitializer(application, context);
// It only has properties in it now that we don't want in the parent so remove
// it (and it will be added back later)
bootstrapProperties.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
mergeDefaultProperties(environment.getPropertySources(), bootstrapProperties);
return context;
}
4. 设置BootstrapContext成为应用Context的父容器 (在SpringApplication实例中动态添加了一个初始化器,相当于给应用Context埋了个雷)
private void addAncestorInitializer(SpringApplication application,
ConfigurableApplicationContext context) {
boolean installed = false;
//从spring.factories文件中获取初始化器的配置类且类型为AncestorInitializer
for (ApplicationContextInitializer<?> initializer : application
.getInitializers()) {
if (initializer instanceof AncestorInitializer) {
installed = true;
// New parent
((AncestorInitializer) initializer).setParent(context);
}
}
//默认情况下是没有配围置AncestorInitializer这样的类,此处是则执行由BootstrapListener提供的内部类
if (!installed) {
//将BootstrapContext作为父容器传到AncestorInitializer实例中,并将其放入SpringApplication实例的初始器列表中
application.addInitializers(new AncestorInitializer(context));
} }
5. 初始化器AncestorInitializer被触发,是由应用Context的处理触发的
public void initialize(ConfigurableApplicationContext context) { //这个context是应用Context
while (context.getParent() != null && context.getParent() != context) {
context = (ConfigurableApplicationContext) context.getParent();
}
reorderSources(context.getEnvironment());
//完成应用容器的父容器的设置
new ParentContextApplicationContextInitializer(this.parent)
.initialize(context);
}
6. ParentContextApplicationContextInitializer代码
private static class ParentContextApplicationContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> { private final ApplicationContext parent; ParentContextApplicationContextInitializer(ApplicationContext parent) {
this.parent = parent;
} @Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.setParent(this.parent); //设置应用Context的父容器
} }
总结:
1. SpringCloud Context模块的功能 :主要是构建了一个Bootstrap容器,并让其成为原有的springboot程序构建的容器的父容器。
2. Bootstrap容器的作用:是为了预先完成一些bean的实例化工作,可以把Bootstrap容器看作是先头部队。
3. Bootstrap容器的构建:是利用了Springboot的事件机制,当 springboot 的初始化 Environment 准备好之后会发布一个事件,这个事件的监听器将负责完成Bootstrap容器的创建。构建时是使用 SpringApplicationBuilder 类来完成的。
4. 如何让Bootstrap容器与应用Context 建立父子关系 :由于Bootstrap容器与应用Context都是关联着同一个SpringApplication实例,Bootstrap容器自己完成初始化器的调用之后,会动态添加了一个初始化器 AncestorInitializer,相当于给应用Context 埋了个雷 ,这个初始化器在应用容器进行初始化器调用执行时,完成父子关系的设置。
Spring Cloud Context模块的更多相关文章
- Spring Cloud Commons模块
上一篇介绍了 Spring Cloud Context模块 ,本文介绍SpringCloud的另一个基础模块 SpringCloud Commons模块 .只要在项目的pom文件中引入了spring- ...
- 【spring cloud】【IDEA】【Maven】spring cloud多模块打包,打包的jar包只有几k,jar包无法运行,运行报错:no main manifest attribute, in /ms-eureka.jar
======================================================================================== 引申:maven打包多 ...
- 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在
>>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...
- SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
构建分布式系统非常复杂且容易出错.Spring Cloud为最常见的分布式系统模式提供了简单易用的编程模型,帮助开发人员构建弹性,可靠和协调的应用程序.Spring Cloud构建于Spring Bo ...
- Spring cloud基础模块学习
1.微服务架构构成 单体架构(通过应用集群和数据库集群来提高性能,多余模块存在浪费) 垂直架构(新的功能模块通过新项目来实现,数据库之间存在交叉关联.存在数据冗余,和单体架构一样通过扩展集群结点,成本 ...
- Spring cloud公共模块
1.0公共的模块是公共的工具包以及实体等 2.添加架包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- spring cloud各个模块作用
Eureka Client:负责将这个服务的信息注册到Eureka Server中.Eureka Server:注册中心,里面有一个注册表,保存了各个服务所在的机器和端口号.ribbon:负载均衡,获 ...
- spring cloud连载第一篇之bootstrap context
1. Spring Cloud Context: Application Context Services(应用上下文服务) 1.1 The Bootstrap Application Context ...
- 利用 Maven 构造 Spring Cloud 微服务架构 模块使用 spring Boot构建
采用Maven 聚合工程搭建 Spring Cloud 使用工具: IntelliJ IDEA 版本: 2019.2.2 maven 版本: 3.6.0 JDK ...
随机推荐
- CentOS7.5修改字符集
乱码产生的原因: 计算机中储存的信息都是用二进制数表示的:而我们在屏幕上看到的英文.汉字等字符是二进制数转换之后的结果.通俗的说,按照何种规则将字符存储在计算机中,如'a'用什么表示,称为" ...
- React-propsType和defaultProps
TodoItem.propTypes={ content:PropTypes.string, text:PropTypes.string.isRequired, handleDeleteItem:Pr ...
- 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件
目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...
- 对于for循环中使用let或var时,i的作用域范围的记录
在for循环中使用let时,结果如下 for内部定义的i在循环结束后不会覆盖外部的i 在for循环中使用var,且不控制i的作用域时,结果如下 第一个for循环内部定义的i并不会创建,而是直接使用外部 ...
- js中的枚举
在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. js中基本包装类型的原型属性是不可枚举的 ...
- webpack+vue 组件间传参(单一事件中心管理组件通信--$root),如果有路由的话会失效
先给一个例子: <body> <div id="box"> <com-a></com-a> <com-b></co ...
- 其它综合-有关service、systemctl、chkconfig、init
有关service.systemctl.chkconfig.init CentOS 7.x 开始,CentOS 开始使用 systemd 服务来代替 daemon,原来管理系统启动和管理系统服务的相关 ...
- 前端获取checkbox复选框的值 通过数组形式传递
html代码: <form role="form" class="select_people"> <div style="displ ...
- Linux 学习 (五) 压缩与解压缩命令
Linux达人养成计划 I 学习笔记 常用压缩格式:.zip | .gz | .bz2 | .tar.gz | .tar.bz2 .zip zip 压缩文件名 源文件:压缩文件 zip -r 压缩文件 ...
- [HackerRank]New Year Chaos[UNDONE]
Input (stdin)Download 2 8 5 1 2 3 7 8 6 4 8 1 2 5 3 7 8 6 4 Your Output (stdout) Too chaotic Too cha ...