一,加载REACTIVE相关自动配置

spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REACTIVE,然后加载上下文类org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext,看下面代码
private WebApplicationType deduceWebApplicationType() {
if (ClassUtils.isPresent("org.springframework.web.reactive.DispatcherHandler", (ClassLoader)null) && !ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", (ClassLoader)null) && !ClassUtils.isPresent("org.glassfish.jersey.server.ResourceConfig", (ClassLoader)null)) {
return WebApplicationType.REACTIVE;
} else {
String[] var1 = WEB_ENVIRONMENT_CLASSES;
int var2 = var1.length; for(int var3 = 0; var3 < var2; ++var3) {
String className = var1[var3];
if (!ClassUtils.isPresent(className, (ClassLoader)null)) {
return WebApplicationType.NONE;
}
} return WebApplicationType.SERVLET;
}
}

当程序类型是REACTIVE,spring boot首先就会加载自身的自动配置类org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration。如果找到HttpServer.clas而且又没有自己实现的ReactiveWebServerFactory.class就应用spring boot自带的nettyReactiveWebServerFactory对象。如果会用jetty或tomcat就引用有Server.class或Tomcat.class第三方包。

@AutoConfigureOrder(-2147483648)
@Configuration
@ConditionalOnClass({ReactiveHttpInputMessage.class})
@ConditionalOnWebApplication(
type = Type.REACTIVE
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ReactiveWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class, EmbeddedNetty.class})
public class ReactiveWebServerFactoryAutoConfiguration {
public ReactiveWebServerFactoryAutoConfiguration() {
} ...................................... abstract class ReactiveWebServerFactoryConfiguration {
ReactiveWebServerFactoryConfiguration() {
} @Configuration
@ConditionalOnMissingBean({ReactiveWebServerFactory.class})
@ConditionalOnClass({HttpServer.class})
static class EmbeddedNetty {
EmbeddedNetty() {
} @Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
return new NettyReactiveWebServerFactory();
}
}

应用程序没有提供WebFluxConfigurationSupport的实例,spring boot就会加载自身的自动配置类org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration。加载对象有webHandler(org.springframework.web.reactive.DispatcherHandler)等

@Configuration
@ConditionalOnWebApplication(
type = Type.REACTIVE
)
@ConditionalOnClass({WebFluxConfigurer.class})
@ConditionalOnMissingBean({WebFluxConfigurationSupport.class})
@AutoConfigureAfter({ReactiveWebServerFactoryAutoConfiguration.class, CodecsAutoConfiguration.class, ValidationAutoConfiguration.class})
@AutoConfigureOrder(-2147483638)
public class WebFluxAutoConfiguration {

应用程序没有提供HttpHandler的实例,spring boot就会加载自身的自动配置类org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,加载对象httpHandler(org.springframework.web.server.adapter.HttpWebHandlerAdapter)。

@Configuration
@ConditionalOnClass({DispatcherHandler.class, HttpHandler.class})
@ConditionalOnWebApplication(
type = Type.REACTIVE
)
@ConditionalOnMissingBean({HttpHandler.class})
@AutoConfigureAfter({WebFluxAutoConfiguration.class})
@AutoConfigureOrder(-2147483638)
public class HttpHandlerAutoConfiguration {
public HttpHandlerAutoConfiguration() {
} @Configuration
public static class AnnotationConfig {
private ApplicationContext applicationContext; public AnnotationConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
} @Bean
public HttpHandler httpHandler() {
return WebHttpHandlerBuilder.applicationContext(this.applicationContext).build();
}
}
}

二,创建REACTIVE内嵌服务器

WebServer在org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext创建。依赖的HttpHandler实例上面已说明如果创建的。

org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext

   protected void onRefresh() {
super.onRefresh(); try {
this.createWebServer();
} catch (Throwable var2) {
throw new ApplicationContextException("Unable to start reactive web server", var2);
}
} private void createWebServer() {
  WebServer localServer = this.webServer;
  if (localServer == null) {
  this.webServer = this.getWebServerFactory().getWebServer(this.getHttpHandler());
  }   this.initPropertySources();
}
org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory

    public WebServer getWebServer(HttpHandler httpHandler) {
HttpServer httpServer = this.createHttpServer();
ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);
return new NettyWebServer(httpServer, handlerAdapter, this.lifecycleTimeout);
}

三,Annotated Controllers与Functional Endpoints

spring webflux处理http请求有两种编程模式,分别是annotated controllers和functional endpoints,annotated controllers与spring mvc类似。它们都调用HttpHandler实现org.springframework.web.server.adapter.HttpWebHandlerAdapter的handle方法。

handle:183, HttpWebHandlerAdapter (org.springframework.web.server.adapter)
apply:75, ReactorHttpHandlerAdapter (org.springframework.http.server.reactive)
apply:40, ReactorHttpHandlerAdapter (org.springframework.http.server.reactive)
applyHandler:380, ChannelOperations (reactor.ipc.netty.channel)
onHandlerStart:407, HttpServerOperations (reactor.ipc.netty.http.server)
run:-1, 1927250759 (reactor.ipc.netty.channel.ContextHandler$$Lambda$524)
safeExecute$$$capture:163, AbstractEventExecutor (io.netty.util.concurrent)
safeExecute:-1, AbstractEventExecutor (io.netty.util.concurrent)
- Async stack trace
addTask:-1, SingleThreadEventExecutor (io.netty.util.concurrent)
execute:765, SingleThreadEventExecutor (io.netty.util.concurrent)
createOperations:249, ContextHandler (reactor.ipc.netty.channel)
channelRead:136, HttpServerHandler (reactor.ipc.netty.http.server)
invokeChannelRead:362, AbstractChannelHandlerContext (io.netty.channel)
invokeChannelRead:348, AbstractChannelHandlerContext (io.netty.channel)
fireChannelRead:340, AbstractChannelHandlerContext (io.netty.channel)
fireChannelRead:438, CombinedChannelDuplexHandler$DelegatingChannelHandlerContext (io.netty.channel)
fireChannelRead:310, ByteToMessageDecoder (io.netty.handler.codec)
channelRead:284, ByteToMessageDecoder (io.netty.handler.codec)
channelRead:253, CombinedChannelDuplexHandler (io.netty.channel)
invokeChannelRead:362, AbstractChannelHandlerContext (io.netty.channel)
invokeChannelRead:348, AbstractChannelHandlerContext (io.netty.channel)
fireChannelRead:340, AbstractChannelHandlerContext (io.netty.channel)
channelRead:1434, DefaultChannelPipeline$HeadContext (io.netty.channel)
invokeChannelRead:362, AbstractChannelHandlerContext (io.netty.channel)
invokeChannelRead:348, AbstractChannelHandlerContext (io.netty.channel)
fireChannelRead:965, DefaultChannelPipeline (io.netty.channel)
read:163, AbstractNioByteChannel$NioByteUnsafe (io.netty.channel.nio)
processSelectedKey:628, NioEventLoop (io.netty.channel.nio)
processSelectedKeysOptimized:563, NioEventLoop (io.netty.channel.nio)
processSelectedKeys:480, NioEventLoop (io.netty.channel.nio)
run:442, NioEventLoop (io.netty.channel.nio)
run:884, SingleThreadEventExecutor$5 (io.netty.util.concurrent)
run:745, Thread (java.lang)

他们也调用handle:149, DispatcherHandler (org.springframework.web.reactive),但最后他们分别调用BookController或BookHandler,看下面不同调用栈:

annotated controllers
getBookById:19, BookController (com.javaworld.webflux.bookservice.web)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
doInvoke:243, InvocableHandlerMethod (org.springframework.web.reactive.result.method)
lambda$invoke$0:138, InvocableHandlerMethod (org.springframework.web.reactive.result.method)
apply:-1, 595198211 (org.springframework.web.reactive.result.method.InvocableHandlerMethod$$Lambda$652)
onNext:118, MonoFlatMap$FlatMapMain (reactor.core.publisher)
complete:1083, Operators$MonoSubscriber (reactor.core.publisher)
signal:247, MonoZip$ZipCoordinator (reactor.core.publisher) .................................................... functional endpoints
findById:24, BookHandler (com.javaworld.webflux.bookservice.web)
handle:-1, 953080701 (com.javaworld.webflux.bookservice.web.BookRouter$$Lambda$437)
handle:61, HandlerFunctionAdapter (org.springframework.web.reactive.function.server.support)
invokeHandler:168, DispatcherHandler (org.springframework.web.reactive)
lambda$handle$1:160, DispatcherHandler (org.springframework.web.reactive)
apply:-1, 1795086753 (org.springframework.web.reactive.DispatcherHandler$$Lambda$550)
onNext:118, MonoFlatMap$FlatMapMain (reactor.core.publisher)
onNext:67, FluxSwitchIfEmpty$SwitchIfEmptySubscriber (reactor.core.publisher)
onNext:76, MonoNext$NextSubscriber (reactor.core.publisher)

参考资料

spring webflux reference

Mastering Spring framework 5, Part 2: Spring WebFlux

Spring boot加载REACTIVE源码分析的更多相关文章

  1. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析

    文章目录 1. EnableAutoConfiguration 帮助我们做了什么 2. 配置参数类 – FreeMarkerProperties 3. 自动配置类 – FreeMarkerAutoCo ...

  2. Springboot 加载配置文件源码分析

    Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...

  3. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  4. ElasticSearch 启动时加载 Analyzer 源码分析

    ElasticSearch 启动时加载 Analyzer 源码分析 本文介绍 ElasticSearch启动时如何创建.加载Analyzer,主要的参考资料是Lucene中关于Analyzer官方文档 ...

  5. Spring Boot 揭秘与实战 源码分析 - 开箱即用,内藏玄机

    文章目录 1. 开箱即用,内藏玄机 2. 总结 3. 源代码 Spring Boot提供了很多”开箱即用“的依赖模块,那么,Spring Boot 如何巧妙的做到开箱即用,自动配置的呢? 开箱即用,内 ...

  6. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

    目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...

  7. Spring Cloud Nacos实现动态配置加载的源码分析

    理解了上述Environment的基本原理后,如何从远程服务器上加载配置到Spring的Environment中. NacosPropertySourceLocator 顺着前面的分析思路,我们很自然 ...

  8. springboot Properties加载顺序源码分析

    关于properties: 在spring框架中properties为Environment对象重要组成部分, springboot有如下几种种方式注入(优先级从高到低): 1.命令行 java -j ...

  9. jQuery实现DOM加载方法源码分析

    传统的判断dom加载的方法 使用 dom0级 onload事件来进行触发所有浏览器都支持在最初是很流行的写法 我们都熟悉这种写法: window.onload=function(){ ... }  但 ...

随机推荐

  1. FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)

    题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...

  2. 运维shell脚本函数语法

    在fun.sh 文件里,使用函数来封装脚本内容 usege() { echo "hello world" echo "脚本怎么使用函数......"}usege ...

  3. mysql5.5.28在Linux下的安装

    1.       下载mysql 在http://dev.mysql.com/downloads/mysql/ 官网上下载mysql-5.5.28-linux2.6-i686.tar.gz. 2.   ...

  4. 高性能JavaScript(1)

    ---------------------------------------------------------------------------------------------------- ...

  5. CentOS Linux 升级内核步骤和方法(转)

    当前系统为CentOS Linux release 6.0 (Final),内核版本为2.6.32-71.el6.i686.由于最近内核出现最新的漏洞(linux kernel 又爆内存提权漏洞,2. ...

  6. Linux中访问Apache报403错误处理方法

    1.先说本地环境,脱离基础环境的解决策略都是耍流氓 (1)Linux:CentOS7.5 (2)Apache: Apache/2.4.6 (CentOS) (3)Apache的安装路径在 /usr/l ...

  7. javamail发邮件

    使用JavaMail发送一封简单邮件的步骤:(1)创建代表邮件服务器的网络连接信息的Session对象.(2)创建代表邮件内容的Message对象(3)创建Transport对象.连接服务器.发送Me ...

  8. java后台常见问题

    Java后台面试 常见问题 Nginx负载均衡 轮询.轮询是默认的,每一个请求按顺序逐一分配到不同的后端服务器,如果后端服务器down掉了,则能自动剔除 ip_hash.个请求按访问IP的hash结果 ...

  9. 2018-2019-2 网络对抗技术 20165228 Exp6 信息搜集与漏洞扫描

    2018-2019-2 网络对抗技术 20165228 Exp6 信息搜集与漏洞扫描 回答问题 哪些组织负责DNS,IP的管理. 全球根服务器均由美国政府授权的ICANN统一管理,负责全球的域名根服务 ...

  10. linux中的baff/cache占用大量内存,如何清理?

    执行命令echo 3 > /proc/sys/vm/drop_caches,即可清空buff/cache 参考http://blog.csdn.net/kevins_lee/article/de ...