Netty WebClient配置超时未生效,始终使用默认30秒
WebClient超时配置不生效的问题修复
你的代码里有几个关键错误导致超时配置未生效:
- 连接池参数单位错误:
pendingAcquireTimeout和maxIdleTime用了Duration.ofMillis(60),这是60毫秒而非60秒,应该改为Duration.ofSeconds(60)。 - 连接超时参数值错误:
ChannelOption.CONNECT_TIMEOUT_MILLIS的参数是毫秒,你传了60,实际是60毫秒,需要改为60000(即60秒)。 - 代码语法错误:
webClient.mutate()..clientConnector(conn)多了一个点,会导致编译错误,应该改为webClient.mutate().clientConnector(conn)。
修正后的代码如下:
webClient = WebClient.builder().baseUrl(url).build(); WebClient newWebClient = getClient(webClient); private WebClient getClient(WebClient webClient) { ConnectionProvider provider = ConnectionProvider.builder("fixed") .maxConnections(maxTotalConnections) .pendingAcquireTimeout(Duration.ofSeconds(60)) .maxIdleTime(Duration.ofSeconds(60)) .build(); HttpClient httpClient = HttpClient.create(provider) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 60000) .doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(60)) .addHandlerLast(new WriteTimeoutHandler(60))) .wiretap(this.getClass().getCanonicalName(), LogLevel.INFO, AdvancedByteBufFormat.TEXTUAL); ReactorClientHttpConnector conn = new ReactorClientHttpConnector(httpClient); return webClient.mutate().clientConnector(conn).build(); }
注:ReadTimeoutHandler和WriteTimeoutHandler的无单位参数默认单位是秒,你原来的60配置是正确的,无需修改。
内容的提问来源于stack exchange,提问作者stackuser
相关产品推荐
相关产品推荐

