java Spring MVC(异步)与 Spring WebFlux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46606246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC (async) vs Spring WebFlux
提问by Dmitry Senkovich
I'm trying to understand Spring WebFlux. The things I've found so far are reactive at the core, no Servlet API, no thread per request, HTTP 2, server pushes, application/stream+json.
我正在尝试了解 Spring WebFlux。到目前为止,我发现的核心是响应式的,没有 Servlet API,没有每个请求的线程,HTTP 2,服务器推送,应用程序/流 + json。
But what is the difference between asynchronous calls in Spring MVC? I mean in Spring MVC when you return Future, DefferedResult and etc you get logic in the request handler (controller method) executed in a separate thread, so you can benefit from saving thread pool resources for dispatching requests as well.
但是 Spring MVC 中的异步调用有什么区别呢?我的意思是在 Spring MVC 中,当您返回 Future、DefferedResult 等时,您会在单独的线程中执行的请求处理程序(控制器方法)中获得逻辑,因此您也可以从节省用于调度请求的线程池资源中受益。
So could you please highlight differences related to that? Why WebFlux is better here?
那么您能否强调与此相关的差异?为什么 WebFlux 在这里更好?
Thank you for your time very much!
非常感谢您的时间!
采纳答案by Brian Clozel
The Servlet async model introduces an async boundary between the container threads (1 Servlet request/thread model) and the processing of the request in your application. Processing can happen on a different thread or wait. In the end, you have to dispatch back to a container thread and read/write in a blocking way (InputStream
and OutputStream
are inherently blocking APIs).
Servlet 异步模型在容器线程(1 Servlet 请求/线程模型)和应用程序中的请求处理之间引入了异步边界。处理可以发生在不同的线程上或等待。最后,您必须分派回容器线程并以阻塞方式进行读/写(InputStream
并且OutputStream
本质上是阻塞 API)。
With that model, you need many threads to achieve concurrency (because many of those can be blocked waiting for I/O). This costs resources and it can be a tradeoff, depending on your use case.
使用该模型,您需要许多线程来实现并发(因为其中许多线程可能会被阻塞以等待 I/O)。这会消耗资源,这可能是一种权衡,具体取决于您的用例。
With non-blocking code, you only need a few threads to process a lot of requests concurrently. This is a different concurrency model; like any model, there are benefits and tradeoffs coming with it.
使用非阻塞代码,您只需要几个线程来并发处理大量请求。这是一个不同的并发模型;像任何模型一样,它也有好处和权衡。
For more information about that comparison, this Servlet vs. Reactive stacks talkshould be of interest.
有关该比较的更多信息,您应该感兴趣这个Servlet 与 Reactive 堆栈的讨论。
回答by Javed A
Servlet API is blocking I/O which requires 1 thread per HTTP request. Spring MVC async relies on Servlet APIs which only provides async behavior between container threads and request processing threads but not end to end.
Servlet API 正在阻塞 I/O,每个 HTTP 请求需要 1 个线程。Spring MVC 异步依赖于 Servlet API,它只提供容器线程和请求处理线程之间的异步行为,而不是端到端的。
Spring WebFlux on the other hand achieves concurrency by a fixed number of threads by using HTTP sockets and pushing chunks of data at a time through the sockets. This mechanism is called event loop, an idea made popular by Node.js. Such an approach is scalable and resilient. Spring 5's spring-webflux uses the event loopapproach to provide async behavior.
另一方面,Spring WebFlux 通过使用 HTTP 套接字并通过套接字一次推送数据块,通过固定数量的线程实现并发。这种机制称为事件循环,这是Node.js流行的一个想法。这种方法具有可扩展性和弹性。Spring 5 的 spring-webflux 使用事件循环方法来提供异步行为。
More can be read from
更多内容可以阅读