You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring对接Flask服务超时求助:ReadTimeoutException异常排查

问题分析与解决

错误原因

你遇到的5秒超时问题,根源在于代码中同时配置了两种冲突的超时机制:

  • 虽然通过HttpClient.responseTimeout()设置了60分钟的全局响应超时,但紧接着在doOnConnected中添加了new ReadTimeoutHandler(5, TimeUnit.SECONDS)——这个Netty层级的读取超时处理器会强制在5秒内未收到任何响应数据时触发超时,直接覆盖了全局响应超时的配置。
  • 报错中的ReadTimeoutException正是这个5秒超时处理器抛出的。

解决方案

需要调整超时配置,确保两种机制的时间一致或移除多余的读取超时处理器:

方案1:移除ReadTimeoutHandler

如果不需要单独控制读取间隔超时,直接删除该处理器配置:

@Bean
public NominalNormalizerClient nominalNormalizerClient() {
    //timeoutMinutes = 60;
    HttpClient httpClient = HttpClient.create()
            .responseTimeout(Duration.ofMinutes(timeoutMinutes)); // 仅保留全局响应超时
    WebClient webClient = WebClient.builder()
            .baseUrl(nominalNormalizerBaseUrl)
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .exchangeStrategies(ExchangeStrategies.builder()
                    .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(maxInMemorySize))
                    .build())
            .build();
    HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory
            .builder(WebClientAdapter.forClient(webClient))
            .build();
    return httpServiceProxyFactory.createClient(NominalNormalizerClient.class);
}

方案2:统一超时时间

如果需要保留ReadTimeoutHandler,将其超时时间与全局响应超时同步:

@Bean
public NominalNormalizerClient nominalNormalizerClient() {
    //timeoutMinutes = 60;
    HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMinutes(timeoutMinutes))
            .doOnConnected(conn -> conn.addHandler(new ReadTimeoutHandler(timeoutMinutes, TimeUnit.MINUTES))); // 同步超时时间
    WebClient webClient = WebClient.builder()
            .baseUrl(nominalNormalizerBaseUrl)
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .exchangeStrategies(ExchangeStrategies.builder()
                    .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(maxInMemorySize))
                    .build())
            .build();
    HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory
            .builder(WebClientAdapter.forClient(webClient))
            .build();
    return httpServiceProxyFactory.createClient(NominalNormalizerClient.class);
}

补充说明

  • responseTimeout是Reactor HttpClient的全局响应超时,指从请求发送到完整接收响应的总时长。
  • ReadTimeoutHandler是Netty的读取间隔超时,指两次读取响应数据之间的最大间隔时间。若后端处理耗时较长且期间无任何数据返回,该处理器会触发超时。

内容的提问来源于stack exchange,提问作者Павлюченко Антон

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:04:59