From 142b4deb8ad0ac41bb237b54b3685ebc8d82f89c Mon Sep 17 00:00:00 2001 From: asahi Date: Mon, 10 Mar 2025 12:56:28 +0800 Subject: [PATCH] =?UTF-8?q?doc:=20=E9=98=85=E8=AF=BBwebflux=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/webflux/spring webflux.md | 147 +++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/spring/webflux/spring webflux.md b/spring/webflux/spring webflux.md index c13f48f..fd139bd 100644 --- a/spring/webflux/spring webflux.md +++ b/spring/webflux/spring webflux.md @@ -33,3 +33,150 @@ Spring webflux提供了如下两种编程模型: #### 调用阻塞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 nameServer API usedReactive Streams support

Netty

Netty API

Reactor Netty

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依赖: + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Server nameGroup idArtifact 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

+ +#### server api adapters +如下示例展示了如何使用`HttpHandler` Adapters来适配不同的Server Api: + +##### Reactor Netty +```java +HttpHandler handler = ... +ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); +HttpServer.create().host(host).port(port).handle(adapter).bindNow(); +``` + +##### Undertow +```java +HttpHandler handler = ... +UndertowHttpHandlerAdapter adapter = new UndertowHttpHandlerAdapter(handler); +Undertow server = Undertow.builder().addHttpListener(port, host).setHandler(adapter).build(); +server.start(); +``` + +##### Tomcat +```java +HttpHandler handler = ... +Servlet servlet = new TomcatHttpHandlerAdapter(handler); + +Tomcat server = new Tomcat(); +File base = new File(System.getProperty("java.io.tmpdir")); +Context rootContext = server.addContext("", base.getAbsolutePath()); +Tomcat.addServlet(rootContext, "main", servlet); +rootContext.addServletMappingDecoded("/", "main"); +server.setHost(host); +server.setPort(port); +server.start(); +``` + +##### Jetty +```java +HttpHandler handler = ... +Servlet servlet = new JettyHttpHandlerAdapter(handler); + +Server server = new Server(); +ServletContextHandler contextHandler = new ServletContextHandler(server, ""); +contextHandler.addServlet(new ServletHolder(servlet), "/"); +contextHandler.start(); + +ServerConnector connector = new ServerConnector(server); +connector.setHost(host); +connector.setPort(port); +server.addConnector(connector); +server.start(); +``` + +### WebHandler API +`org.springframework.web.server` package基于`HttpHandler`构建,提供了通用的Web API。Web Api由多个`WebException`, 多个`WebFilter`, 一个`WebHandler`组件构成,组成了一个chain。 + + +