Spring MVC(async)vs Spring WebFlux [英] Spring MVC (async) vs Spring WebFlux

查看:644
本文介绍了Spring MVC(async)vs Spring WebFlux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解Spring WebFlux。到目前为止我发现的东西都是内核反应,没有Servlet API,每个请求没有线程,HTTP 2,服务器推送,应用程序/流+ json。

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 MVC中的异步调用有什么区别?我的意思是在Spring MVC中,当你返回Future,DefferedResult等时,你会在一个单独的线程中执行请求处理程序(控制器方法)中的逻辑,因此你也可以从保存线程池资源中受益,以便调度请求。

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.

那么请你强调与此相关的差异吗?为什么WebFlux在这里更好?

So could you please highlight differences related to that? Why WebFlux is better here?

非常感谢您的时间!

推荐答案

Servlet异步模型在容器线程(1个Servlet请求/线程模型)和应用程序中的请求处理之间引入了异步边界。处理可以在不同的线程上发生或等待。最后,你必须发送回容器线程并以阻塞方式读/写( InputStream OutputStream 本质上是阻止API)。

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).

使用该模型,您需要许多线程来实现并发(因为许多线程可以被阻塞等待I / O)。这会花费资源而且可能需要权衡,具体取决于您的使用案例。

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.

使用非阻塞代码,您只需要几个线程来同时处理大量请求。这是一个不同的并发模型;与任何型号一样,它也会带来好处和权衡。

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.

有关该比较的更多信息,请 Servlet vs. Reactive stacks talk 应该是有意义的。

For more information about that comparison, this Servlet vs. Reactive stacks talk should be of interest.

这篇关于Spring MVC(async)vs Spring WebFlux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆