Spring Webflux 中的背压

Backpressure in Spring Webflux

我对 Spring Webflux 和整个响应式想法还很陌生。我对 Spring Webflux 如何基于其反应性发布者 (Flux/Mono) 处理请求有一些疑问。以一个简单的控制器为例:

@PostMapping("/planets")
public Mono<Void> createNewPlanets(Flux<Planet> planetFlux) {

    return planetService
                       .insert(planetFlux.limitRate(10))
                       .then();
}

我了解 limitRate() 充当下游请求的限制器。我的问题是,这种背压机制仅根据我的参数 (planetFlux) 的单个请求起作用。 WebFlux(或响应式编程)是否解决了请求负载过重的问题?意思是我将如何针对多个并发请求优化上述代码?

我的第二个问题是 planetFlux 的剩余元素究竟存储在哪里,假设我应用了一些背压机制?再拿上面的代码来说,如果我在 planetFlux 中有 100 个元素,当我的下游处理它时,剩下的 90 个元素存储在哪里?它存储在队列中吗?

如果您有参考资料,我很乐意阅读。希望我的问题足够清楚。

Does WebFlux (or reactive programming) in anyway address the issue of having a heavy load of requests? Meaning how would I go about optimizing the above code for multiple concurrent requests

Webflux 的核心设计是通过摆脱“每个请求一个线程”的理念来实现基于事件的链,其中线程仅在发出的事件期间工作。

如文档所述:

There are broadly two ways one can improve a program’s performance:

  • parallelize: use more threads and more hardware resources.

  • seek more efficiency in how current resources are used.

响应式程序会尝试实现第二个要点。

你的一小段代码没有什么可以优化的,所以我不太明白你要优化的是什么,插入?

有人订阅了您的服务并发布了一堆行星。你采用它并将插入率限制为 10,以便插入方法获得一个 Flux,它将发出 10 个批次以插入到你正在插入的任何内容中。所以我不太明白你的问题。

你的第二个问题:

where exactly are the remaining elements of planetFlux stored

如果您查看源代码,您可以看到以下内容。

在 flux 中,单个项目作为直接值存储在 class FluxJust<T>.class 中。而多个值作为简单数组存储在 FluxArray<T>.class 中。