一,加载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. MySQL80修改密码

    mysql> update user set password=password('123') where user='root' and host='localhost'; ERROR 106 ...

  2. Sonar Java 规则插件开发 (基于阿里开发手册)

    引言 最近在做Sonar静态代码扫描管理,以此顺手接了Sonar的插件开发,基于阿里开发手册进行开发,在整体开发过程中,其中还是遇到不少坑位,也以此给大家做相应借鉴官网Demo演示插件开发地址:htt ...

  3. C#获取当前日期时间

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  4. 求助,我在安装SQL sever2016时遇到了这种情况

    哪位大佬能告诉我这怎么解决?多谢!

  5. Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...

  6. 大硬盘(大于2T)分区方法

    背景 在使用fdisk建立分区时,我们最大只能建立2TB大小的分区.如需建立超过2T的分区需要采用GPT磁盘模式.下文补充一下GPT和MBR的基础知识和分超过2T分区的方法. 基本概念 MBR 1.M ...

  7. TP5对数据库操作的事物作用

    假如: 你写好了一段完整的代码,模型对数据库的操作,增删改查什么的,都没有问题,当然运行速度也是最快的,完全不用担心会出错, 前提肯定是已经写好的一整段代码, 但是,万一服务器中断了呢,执行一半,后面 ...

  8. 小程序radio样式修改

    .city-radio-group-label .city-label-radio {    //label样式   padding: 15rpx 50rpx;   position: relativ ...

  9. HTTPS和HTTP(加密知识)

    什么是HTTPS? 基于安全套接字层的超文本传输协议(HTTP over SSL),一个Netscape开发的Web协议.HTTPS在HTTP应用层的基础上使用安全套接字层(或者升级版传输层安全,Tr ...

  10. ServletContext、ServletRequest和HttpSession的生命周期

    学习web的Listener监听器的时,监听域对象创建的监听器.通过监听器重新分析着三个对象的生命周期,清晰明了. 首先域对象的监听器有三个ServletContextListene.ServletR ...