官网介绍:
Reactive systems have certain characteristics that make them ideal for low-latency, high-throughput workloads. Project Reactor and the Spring portfolio work together to enable developers to build enterprise-grade reactive systems that are responsive, resilient, elastic, and message-driven.
Spring WebFlux 是 Spring Framework 5.0中引入的新的响应式web框架。
与Spring MVC不同,它不依赖Servlet API,是完全异步且非阻塞的,并且实现了Reactive Streams规范。
使用场景:Spring WebFlux 用于创建基于事件循环执行模型的完全异步且非阻塞的应用程序。
⚠️注意:异步非阻塞是针对服务端而言的,指的是服务端可以充分利用CPU资源去做更多事情。
Mono VS Flux
Mono is a special type of Publisher. Mono object represents a single or empty value. This means it can emit only one value at most for the onNext() request and then terminates with the onComplete() signal. In case of failure, it only emits a single onError() signal.
Mono
是一个发出(emit)0-1
个元素的Publisher,可以被onComplete
信号或者onError
信号所终止。
Flux is a standard Publisher that represents 0 to N asynchronous sequence values. This means that it can emit 0 to many values, possibly infinite values for onNext() requests, and then terminates with either a completion or an error signal.
Flux
是一个发出(emit)0-N
个元素组成的异步序列的Publisher,可以被onComplete
信号或者onError
信号所终止。
异步非阻塞并不会使程序运行得更快。WebFlux 并不能使接口的响应时间缩短,它仅仅能够提升吞吐量和伸缩性。
Spring WebFlux 是一个异步非阻塞的 Web 框架,所以,它特别适合应用在 IO 密集型的服务中,比如微服务网关(Spring Cloud Gateway)这样的应用中。