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

大文件下载遇reactor.netty连接提前关闭问题,求解决方案

大文件下载时触发PrematureCloseException异常

问题描述

下载耗时数分钟的大文件时出现异常:接口端点返回200状态码,但等待约90秒后下载连接被提前关闭,抛出reactor.netty.http.client.PrematureCloseException异常。

异常日志

DEBUG 6 --- [ctor-http-nio-5] r.n.r.DefaultPooledConnectionProvider    : [ecd64cf4-1, L:/* ! R:*] onStateChange(GET{uri=*, connection=PooledConnection{channel=[id: 0xecd64cf4, L:/*! R:*]}}, [response_incomplete])
ERROR 6 --- [ctor-http-nio-5] o.s.w.s.adapter.HttpWebHandlerAdapter    : [de66232f-2] Error [reactor.netty.http.client.PrematureCloseException: Connection prematurely closed DURING response] for HTTP GET "*", but ServerHttpResponse already committed (200 OK)
TRACE 6 --- [ctor-http-nio-5] o.s.h.s.r.ReactorHttpHandlerAdapter      : [de66232f-1, L:/*:3000 - R:/*:32383] Failed to complete: Connection prematurely closed DURING response
ERROR 6 --- [ctor-http-nio-5] r.n.http.server.HttpServerOperations     : [de66232f-1, L:/*:3000 - R:/*:32383] Error finishing response. Closing connection

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed DURING response
     Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
 Error has been observed at the following site(s):
...
[ExceptionHandlingWebHandler]

当前技术版本

  • reactor-netty-http: 1.0.39
  • spring-boot: 2.7.18
  • Java 11(Java 17环境下同样复现)

已尝试的无效配置

cloud:
    gateway:
      httpclient:
        pool:
          type: disabled
          max-idle-time: 600000
          max-life-time: 6000000

可行解决方案建议

1. 调整Reactor Netty超时参数

90秒的断开时间大概率是默认超时触发,需延长响应、连接及空闲超时:

cloud:
  gateway:
    httpclient:
      response-timeout: 3600000 # 设置为1小时,根据实际下载时长调整
      connect-timeout: 10000
      pool:
        type: disabled
        max-idle-time: 600000
        max-life-time: 6000000
      tcp:
        idle-timeout: 3600000 # 延长TCP连接空闲超时

2. 检查后端服务超时配置

后端服务(如Tomcat、Nginx)可能存在主动断开连接的超时设置,需同步调整:

  • Tomcat:修改server.tomcat.connection-timeout、server.tomcat.keep-alive-timeout为足够大的值
  • Nginx:调整proxy_read_timeout、proxy_send_timeout参数

3. 启用分块传输编码

确保响应以分块方式发送,避免因大文件一次性传输导致超时,可在网关或后端添加响应头:

// 网关过滤器示例
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    exchange.getResponse().getHeaders().add("Transfer-Encoding", "chunked");
    return chain.filter(exchange);
}

或在Spring Boot配置中开启:

server:
  servlet:
    http:
      encoding:
        chunked: true

4. 调整连接池配置

若禁用连接池无效,尝试切换为弹性连接池并延长生命周期:

cloud:
  gateway:
    httpclient:
      pool:
        type: elastic
        max-idle-time: 3600000
        max-life-time: 86400000

5. 排查网络中间件超时

负载均衡器、防火墙、代理服务器等中间件可能存在默认90秒超时,需联系运维调整对应设备的超时时间。

内容的提问来源于stack exchange,提问作者Guilherme Debom

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 00:43:15