gpt4 book ai didi

java - Spring 5 webflux 如何为现有 Webclient 设置超时

转载 作者:行者123 更新时间:2023-12-02 09:41:36 25 4
gpt4 key购买 nike

我在配置类中定义了一个 WebClient bean。它涉及到其连接器的大量配置(包括设置 SSL、代理等)。

    @Bean
@Primary
@Scope("prototype")
public WebClient webClient()
{
SslContextBuilder builder = SslContextBuilder.forClient();
HttpClient httpClient = HttpClient
.create(ConnectionProvider.fixed("webClientPool", maxConnections))
...

ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
WebClient webClient = WebClient.builder()
.clientConnector(connector)
.exchangeStrategies(getExchangeStrategies())
.build();
return webClient;

在注入(inject)此 WebClient 的 bean 之一中,我想设置不同的超时。

我想使用 webClient.mutate().clientConnector(...) 方法,但这需要我从头开始设置整个配置,只是为了设置超时。不幸的是,WebClient 和 HttpClient 没有 .getxxx() 方法,可以帮助我将旧客户端配置复制到新客户端中。

我想要一种方法来仅为变异的 WebClient 设置 tcp 客户端的超时选项。有办法做到吗?

最佳答案

根据文档

WebClient.Builder mutate()

Return a builder to create a new WebClient whose settings are replicated from the current WebClient.

将返回一个新的构建器,其中设置了所有先前的设置,因此您无需复制任何内容。

webclient mutate

如果您想尽可能减少配置,您可以:

定义一个@Bean,在其中配置尽可能多的HttpClient.Builder,以便稍后可以在 HttpClient.Builder 中 Autowiring

然后定义 2 个 Web 客户端,它们在 httpclient 中 Autowiring ,并在每个 Web 客户端中完成 httpclient.build()。

或者你定义一个Web客户端,然后在需要修改的类中注入(inject)Web客户端和httpclient.builder并完成配置并改变Web客户端。

关于java - Spring 5 webflux 如何为现有 Webclient 设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com