- [Spring Webflux](#spring-webflux) - [Concept](#concept) - [核心机制](#核心机制) - [reactive](#reactive) - [back pressure](#back-pressure) - [reactive stream](#reactive-stream) - [编程模型](#编程模型) - [并发模型](#并发模型) - [spring mvc](#spring-mvc) - [webflux](#webflux) - [调用阻塞api](#调用阻塞api) - [Reactive Core](#reactive-core) - [HttpHandler](#httphandler) - [server api adapters](#server-api-adapters) - [Reactor Netty](#reactor-netty) - [Undertow](#undertow) - [Tomcat](#tomcat) - [Jetty](#jetty) - [WebHandler API](#webhandler-api) - [bean types for WebHttpHandlerBuilder auto-detect](#bean-types-for-webhttphandlerbuilder-auto-detect) - [Form Data](#form-data) - [Multipart Data](#multipart-data) - [Filter](#filter) - [UrlHandler](#urlhandler) - [Exceptions](#exceptions) - [Logging](#logging) - [Log Id](#log-id) - [Appenders](#appenders) - [DispatcherHandler](#dispatcherhandler) - [DispatcherHandler委托](#dispatcherhandler委托) - [Processing](#processing) - [Result Handling](#result-handling) - [Annotated Controllers](#annotated-controllers) - [@Controller](#controller) - [AOP](#aop) - [Functional Endpoint](#functional-endpoint) - [Overview](#overview) - [Immutable](#immutable) - [Router Function](#router-function) # Spring Webflux ## Concept ### 核心机制 #### reactive reactive代表基于“事件响应”的编程模型, #### back pressure 在spring webflux中,`back pressure`为反应式编程的核心机制,用于协调生产者和消费者之间的速率差异,令系统在高负载或资源受限的情况下仍能稳定运行。 - 同步场景:在同步场景下,阻塞式调用是一种天然的back pressure形式,调用方会阻塞并等待,直到被调用方执行完成 - 非阻塞场景:在非阻塞的代码中,需要关注事件速率,生产者产生事件的速率不能压过消费者消费的速率 ##### reactive stream reactive stream为一个小型规范,定义了异步组件和back pressure交互的规范。reactive stream的主要用途是让Subscriber控制publisher产生数据的速率。 ### 编程模型 `spring-web` module包含Spring webflux的响应式基础,包括若夏内容: - http抽象 - 对支持server的reactive stream adapters - codecs - 核心WebHandler API,其与servlet api兼容 Spring webflux提供了如下两种编程模型: - `Annotated Controllers`: 和spring mvc一致,基于相同的注解。`spring mvc`和`webflux controllers`都支持`reactive return type`,因此,很难区分`spring mvc`和`webflux controllers` - `Functional Endpoint`:基于lambda的、轻量的函数编程模型。其可以被看做是一个`支持对请求进行route和handle`的工具集合。 ### 并发模型 #### spring mvc 在`spring mvc`(通常为servlet应用)中,其假设当前线程可能会被阻塞(例如,远程调用等会阻塞当前线程)。为了减少处理请求时阻塞所带来的影响,servlet容器会使用包含大量线程的线程池。 #### webflux 对于`webflux`(通常为non-blocking server),其会假设应用并不会阻塞,故而,非阻塞的server可以使用一个线程数较少且固定的线程池(event loop workers)来处理请求。 #### 调用阻塞api 当想要在webflux中使用阻塞api时,可以按如下方式进行使用。`RxJava`和`Reactive`都支持`publushOn`操作,其支持在另一个线程中`继续执行`。 ## Reactive Core `spring-web`对于reactive web应用具有如下支持: - 对于server request的处理,拥有如下两种级别的支持: - `HttpHandler`: 基于`non-blocking I/O`和`Reactive Stream back pressure`,适配`Reactor Netty, Undertow, Tomcat, Jetty以及任一Servlet Container - `WebHandler`:稍高级别的、通用的web api,用于请求处理,在此基础上构建基于`annotated controllers`和`functional endpoints`的编程模型 - 对于client端,`ClientHttpConnector`用于发送http请求,同时兼容`非阻塞io和Reactive Stream back pressure` - codecs用于客户端和服务端的序列化和反序列化 ### HttpHandler HttpHandler中只通过一个单独的方法来处理请求和相应,其对不同的http server api进行了最小化的抽象。 下表描述了支持的api:
| Server name | Server API used | Reactive Streams support |
|---|---|---|
Netty |
Netty API |
|
Undertow |
Undertow API |
spring-web: Undertow to Reactive Streams bridge |
Tomcat |
Servlet non-blocking I/O; Tomcat API to read and write ByteBuffers vs byte[] |
spring-web: Servlet non-blocking I/O to Reactive Streams bridge |
Jetty |
Servlet non-blocking I/O; Jetty API to write ByteBuffers vs byte[] |
spring-web: Servlet non-blocking I/O to Reactive Streams bridge |
Servlet container |
Servlet non-blocking I/O |
spring-web: Servlet non-blocking I/O to Reactive Streams bridge |
| Server name | Group id | Artifact name |
|---|---|---|
Reactor Netty |
io.projectreactor.netty |
reactor-netty |
Undertow |
io.undertow |
undertow-core |
Tomcat |
org.apache.tomcat.embed |
tomcat-embed-core |
Jetty |
org.eclipse.jetty |
jetty-server, jetty-servlet |
| Bean name | Bean type | Count | Description |
|---|---|---|---|
<any> |
|
0..N |
Provide handling for exceptions from the chain of |
<any> |
|
0..N |
Apply interception style logic to before and after the rest of the filter chain and
the target |
|
|
1 |
The handler for the request. |
|
|
0..1 |
The manager for |
|
|
0..1 |
For access to |
|
|
0..1 |
The resolver for |
|
|
0..1 |
For processing forwarded type headers, either by extracting and removing them or by removing them only. Not used by default. |
| Bean type | Explanation |
|---|---|
|
Map a request to a handler. The mapping is based on some criteria, the details of
which vary by The main |
|
Help the |
|
Process the result from the handler invocation and finalize the response. See Result Handling. |
| Result Handler Type | Return Values | Default Order |
|---|---|---|
|
|
0 |
|
|
0 |
|
Handle return values from |
100 |
|
See also View Resolution. |
|