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

Spring Cloud Gateway 30秒后关闭WebSocket连接,如何调整超时?

Spring Cloud Gateway WebSocket超时问题解决方案

可以通过调整Spring Cloud Gateway的配置来延长甚至移除WebSocket连接的超时时间,具体配置方式如下:

1. 全局调整WebSocket超时

在应用配置文件(application.yml/properties)中添加WebSocket相关的超时配置,按需设置超时时长(示例设置为300秒,即5分钟;设置为0则表示永不超时):

spring:
  cloud:
    gateway:
      httpclient:
        websocket:
          # WebSocket握手超时时间,单位毫秒
          handshake-timeout: 300000
          # WebSocket空闲连接超时时间,单位毫秒,0表示无超时
          idle-timeout: 300000

不建议直接设置idle-timeout: 0,避免长期闲置的无效连接占用服务器资源,建议根据业务场景设置合理的超时时长。

2. 针对特定路由单独配置超时

如果仅需要给你的websocket_route路由单独设置超时,可以在路由配置中添加过滤器参数:

spring:
  cloud:
    gateway.routes:
      - id: websocket_route
        # 注意:uri格式建议使用服务发现地址(如lb://websocket-service-svc)或具体服务地址,无需加/**
        uri: lb://websocket-service-svc
        predicates:
          - Path=/api/v1/websocket-service/ws-websocket
        filters:
          - name: WebsocketRoutingFilter
            args:
              handshakeTimeout: 300000
              idleTimeout: 300000

3. 额外注意事项

  • 检查WebSocket服务端自身是否设置了更短的超时时间,避免网关调整后服务端主动断开连接。
  • 若使用服务发现组件(如Eureka、Nacos),路由的uri应使用lb://服务名格式,而非直接写wss地址,确保网关能正确路由到服务实例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 17:22:36