如何在SpringWebClient中管理/创建连接池?

如何在SpringWebClient中管理/创建连接池?,spring,connection-pooling,spring-webflux,Spring,Connection Pooling,Spring Webflux,我们使用SpringWebClient调用web服务,使用相同的方法 但是,我不知道如何在SpringWebClient中创建/管理连接池 我知道我们使用了“ReactorClientHttpConnector”,但没有得到任何示例代码 基本上,我希望WebClient池具有maxTotal、maxWaitMillis等功能。Spring WebClient是一个无阻塞IO http客户端,而ReactorClient TTPConnector是一个基于Reactor Netty的实现。我可以建

我们使用SpringWebClient调用web服务,使用相同的方法

但是,我不知道如何在SpringWebClient中创建/管理连接池

我知道我们使用了“ReactorClientHttpConnector”,但没有得到任何示例代码


基本上,我希望WebClient池具有maxTotal、maxWaitMillis等功能。

Spring WebClient是一个无阻塞IO http客户端,而ReactorClient TTPConnector是一个基于Reactor Netty的实现。我可以建议不要担心连接池,而是关注一个完整的无阻塞服务调用。成功使用这种技术的关键在于专注于一个完整的无阻塞服务调用链,该模型不涉及每个请求的线程,它就像浏览器或节点js开发,如果你有什么东西阻塞了你的代码,你就阻塞了任何东西。我知道这并不常见,但基于事件循环模型的基本实现迫使您考虑一个完全不同的模型。 我可以安慰您,通常在基于Netty的实现中,您有许多事件循环,这些事件循环的数量与核心的数量相同,当然是可配置的,但我认为这已经足够了,请记住,反应式和无阻塞IO编程的威力在于在所有代码中采用无阻塞IO,并在每个处理器上添加更多的事件循环,这将使您增加一些并发性,而每个处理器有一个事件循环将使您能够完全并行地使用处理器

我希望这个想法能对你有所帮助

小费。对于http服务调用的超时,您可以在下面的测试中添加类似的超时:

@Test
@WithMockUser(username = "user")
fun `read basic personal details data`() {
    personalDetailsRepository.save("RESUME_ID", TestCase.personalDetails()).toMono().block();

    val expectedJson = TestCase.readFileAsString("personal-details.json")
    webClient.get()
            .uri("/resume/RESUME_ID/personal-details")
            .accept(MediaType.APPLICATION_JSON)
            .exchange().toMono().timeout(Duration.ofMinutes(1))

} 
更新

考虑到在应用程序级别限制webclient的请求,当然可以使用背压功能来处理数据流,该数据流有时可能太大,无法可靠地处理,或者如果流响应像通量限制率()的通量操作员在获取官方文件时可能会很有用:

/**
     * Ensure that backpressure signals from downstream subscribers are split into batches
     * capped at the provided {@code prefetchRate} when propagated upstream, effectively
     * rate limiting the upstream {@link Publisher}.
     * <p>
     * Note that this is an upper bound, and that this operator uses a prefetch-and-replenish
     * strategy, requesting a replenishing amount when 75% of the prefetch amount has been
     * emitted.
     * <p>
     * Typically used for scenarios where consumer(s) request a large amount of data
     * (eg. {@code Long.MAX_VALUE}) but the data source behaves better or can be optimized
     * with smaller requests (eg. database paging, etc...). All data is still processed,
     * unlike with {@link #limitRequest(long)} which will cap the grand total request
     * amount.
     * <p>
     * Equivalent to {@code flux.publishOn(Schedulers.immediate(), prefetchRate).subscribe() }.
     * Note that the {@code prefetchRate} is an upper bound, and that this operator uses a
     * prefetch-and-replenish strategy, requesting a replenishing amount when 75% of the
     * prefetch amount has been emitted.
     *
     * @param prefetchRate the limit to apply to downstream's backpressure
     *
     * @return a {@link Flux} limiting downstream's backpressure
     * @see #publishOn(Scheduler, int)
     * @see #limitRequest(long)
     */
    public final Flux<T> limitRate(int prefetchRate) {
        return onAssembly(this.publishOn(Schedulers.immediate(), prefetchRate));
    }
/**
*确保来自下游用户的背压信号分批发送
*当向上游传播时,有效地以提供的{@code prefetchRate}为上限
*速率限制上游{@link Publisher}。
*
*请注意,这是一个上限,此运算符使用预取和补充
*策略,当预回迁金额的75%已完成时,请求补充金额
*发射。
*
*通常用于消费者请求大量数据的场景
*(例如{@code Long.MAX_VALUE}),但数据源的性能更好,或者可以进行优化
*使用较小的请求(如数据库分页等)。所有数据仍在处理中,
*与{@link#limitRequest(long)}不同,它将限制总请求
*数量。
*
*相当于{@code flux.publishOn(Schedulers.immediate(),prefetchRate).subscribe()}。
*请注意,{@code prefetchRate}是一个上限,该运算符使用
*预取和补充策略,当75%的
*已发出预取量。
*
*@param预取适用于下游背压的限值
*
*@return a{@link Flux}限制下游的背压
*@see#publishOn(调度程序,int)
*@see#limitRequest(长)
*/
公共最终通量限制率(int预取率){
在组装时返回(this.publishOn(Schedulers.immediate(),prefetchRate));
}

我建议使用此功能,不要试图像连接限制那样强制限制数据的使用。反应式编程和无阻塞IO的优势之一在于使用资源和限制资源使用的难以置信的效率。如果我想在应用程序级别限制webclient调用,这似乎与范式精神背道而驰,我如何实现它?如果你想,你可以使用背压功能,这是一种处理数据流的方法,有时数据流可能太大,无法可靠地处理。我想另一个answare可以帮助你:我更新答案