简体   繁体   English

Spring Boot 2. 异步 API。 CompletableFuture 与 Reactive

[英]Spring Boot 2. Async API. CompletableFuture vs. Reactive

My application is heavily relying on asynchronous web-services.我的应用程序严重依赖异步 Web 服务。 It is built with spring boot 1.5.x, which allows me to utilize standard Java 8 CompletableFuture<T> in order to produce deferred async responses.它是用 spring boot 1.5.x 构建的,它允许我利用标准的 Java 8 CompletableFuture<T>来产生延迟的异步响应。 For more info see https://nickebbitt.github.io/blog/2017/03/22/async-web-service-using-completable-future有关更多信息,请参阅https://nickebbitt.github.io/blog/2017/03/22/async-web-service-using-completable-future

Spring boot 2.0.x now comes with the starter pack that can utilize reactive paradigm. Spring Boot 2.0.x 现在带有可以利用反应式范式的入门包。 Spring WebFlux is the framework, which is implementing reactive HTTP. Spring WebFlux 是框架,它实现了响应式 HTTP。

Since I have my API implemented as described in the first paragraph, will I gain much by redoing my services to use non-blocking reactive approach?既然我已经按照第一段中的描述实现了我的 API,那么通过重做我的服务来使用非阻塞响应式方法,我会获得很多好处吗? In a nutshell, I'll have non-blocking API as well, right?简而言之,我也会有非阻塞 API,对吧?

Is there an example how to convert async API that is based on CompletableFuture<T> to Mono<T>\\Flux<T> ?是否有示例如何将基于CompletableFuture<T>异步 API 转换为Mono<T>\\Flux<T>

I was thinking to get rid of servlet-based server altogether (Jetty in my case) and go with Netty + Reactor.我想完全摆脱基于 servlet 的服务器(在我的例子中是 Jetty)并使用 Netty + Reactor。

Needless to say that I am new to the whole reactive paradigm.不用说,我对整个反应式范式不熟悉。

I would like to hear your opinions.我想听听你的意见。

I have two things to say:我有两件事要说:

Q: Is there an example how to convert async API that is based on CompletableFuture to Mono\\Flux?问:有没有一个例子,如何将基于 CompletableFuture 的 async API 转换为 Mono\\Flux?

A: 1) You have to configure endpoint in a bit different way https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html A: 1) 您必须以不同的方式配置端点https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html

2) CompletableFuture to Mono\\Flux example: Mono.fromFuture(...) 2) CompletableFuture 到 Mono\\Flux 示例:Mono.fromFuture(...)

As for the question: "will I gain much by redoing my services to use non-blocking reactive approach".至于问题:“通过重做我的服务以使用非阻塞反应式方法,我会获得很多好处吗”。 The general answer is provided in the documentation: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-performance .. and it is no.文档中提供了一般答案: https : //docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-performance .. 它不是。

Performance has many characteristics and meanings.性能有很多特点和意义。 Reactive and non-blocking generally do not make applications run faster.反应式和非阻塞通常不会使应用程序运行得更快。 They can, in some cases, (for example, if using the WebClient to run remote calls in parallel).在某些情况下,它们可以(例如,如果使用 WebClient 并行运行远程调用)。 On the whole, it requires more work to do things the non-blocking way and that can slightly increase the required processing time.总的来说,以非阻塞方式做事需要更多的工作,这会稍微增加所需的处理时间。

The key expected benefit of reactive and non-blocking is the ability to scale with a small, fixed number of threads and less memory.反应式和非阻塞式的主要预期好处是能够以固定数量的小线程和更少的内存进行扩展。 That makes applications more resilient under load, because they scale in a more predictable way.这使得应用程序在负载下更具弹性,因为它们以更可预测的方式扩展。 In order to observe those benefits, however, you need to have some latency (including a mix of slow and unpredictable network I/O).然而,为了观察这些好处,您需要有一些延迟(包括缓慢和不可预测的网络 I/O 的混合)。 That is where the reactive stack begins to show its strengths, and the differences can be dramatic.这就是反应堆开始显示其优势的地方,并且差异可能是巨大的。

This is general answer, but the specifics will depend and you must measure and see.这是一般性答案,但具体情况将取决于您必须测量并查看。 I would start by recreating a simple part of the application and checking the performance of both in an isolated environment.我将首先重新创建应用程序的一个简单部分,并在隔离环境中检查两者的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM