java - Can I use SpringMvc and webflux together?
问题描述
I would like to use 2 approaches(reactive and standard) in one project.
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?
解决方案
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 can't run on Netty
- both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc)
- mixing both runtime models within the same container is not a good idea and is likely to perform badly or just not work at all
Depending on the goal you're trying to achieve, there might be several ways to work on this.
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).
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.
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.
推荐阅读
- microsoft-graph-api - 如何删除在 Microsoft Bing 地图上绘制区域的重复区域?
- django - 按日期查询所有对象 | Django REST 框架
- http - 授权标头中的多个令牌
- java - 如何存储来自 JTextField 的值并传递给另一个类进行计算
- c# - c#datatable中的datacolumn设置小数位
- c - 为什么我的 C 程序不适用于评分?
- docker - 私有 docker 注册表和 python
- node.js - 命令 '/bin/sh -c cd X' 返回一个非零代码:2
- reactjs - 在 react-redux 中调度操作后用 null 更新我的状态
- kotlin - Kotlin 编译器错误:类型不匹配。必需:CapturedType(out A) 找到:A