我可以一起使用SpringMvc和webflux吗? [英] Can I use SpringMvc and webflux together?

查看:378
本文介绍了我可以一起使用SpringMvc和webflux吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个项目中使用两种方法(反应性和标准方法).

I would like to use 2 approaches(reactive and standard) in one project.

我尝试将一个REST API端点迁移到反应式Webflux并测试性能,然后再迁移其余的.但这没有用.我为他添加了路由器和处理程序,但是直到我没有从依赖项中删除 spring-boot-starter-web 并禁用 @RestController ,我才获得http 404 始终进行编码.有没有可能?还是应该将所有项目迁移到被动方法?

I tried to migrate one REST API endpoint to reactive webflux and test performance before migrate rest of them. But it didn't work. I added router and handler for him, but until I didn't remove spring-boot-starter-web from dependencies and disable @RestController I got http 404 code all the time. Is it possible or not? Or should I migrate all project to reactive approach?

推荐答案

如Spring Boot参考文档中所述,

As explained in the Spring Boot reference documentation, Spring Boot will auto-configure a Spring MVC application if both MVC and WebFlux are available. There are several reasons for this:

  • Spring MVC无法在Netty上运行
  • 两个基础架构都将竞争同一工作(例如,提供静态资源,映射等)
  • 在同一个容器中混合两个运行时模型不是一个好主意,并且可能会表现不佳或根本无法工作

根据您要实现的目标,可能有几种方法可以解决此问题.

Depending on the goal you're trying to achieve, there might be several ways to work on this.

如果您想使用 WebClient 优化多个并发的远程HTTP调用并使用Reactor运算符,则可以继续使用带有Spring MVC注释的控制器,并将返回类型作为返回值(在本Spring Boot谈话中对此进行更多的介绍).

If you'd like to use WebClient to optimize for multiple, concurrent remote HTTP calls and use Reactor operators, you can keep using Spring MVC annotated controllers and return reactive types as return values (more on this in this Spring Boot talk).

如果您想使用纯可伸缩性和延迟(因此不一定需要原始吞吐量),则可以开始使用 spring-boot-starter-webflux 并从那里开始工作.请注意,禁止使用阻塞API(例如阻塞数据库调用),并使用 Flux Mono 进行包装,并安排在单独的线程池上工作会对性能产生不利影响一侧.

If you'd like to work on pure scalability and latency (so not necessarily raw throughput), then you could start using spring-boot-starter-webflux and work from there. Note that using blocking APIs (like blocking database calls) is forbidden, and wrapping those with Flux or Mono and scheduling that work on separate thread pools will work against you on the performance side.

最后,如果您想使用Spring WebFlux提供的功能方法,那么它不一定会表现更好.这实际上取决于您的用例以及实现方式.

Finally, if you'd like to use the functional approach provided by Spring WebFlux, then it won't necessarily perform better. It really depends on your use case and how you implement it.

这篇关于我可以一起使用SpringMvc和webflux吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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