spring-cloud-gateway使用websocket出现Max frame length of 65536 has been exceeded

前言:

在 Spring Cloud Gateway 3.1.4 版本中配置 WebSocket 通信时,遇到 Max frame length of 65536 has been exceeded 错误。经过源码分析发现,网上流传的通过 spring.cloud.gateway.httpclient.websocket.max-frame-payload-length 配置参数修改最大帧长度的方法无效。本文通过源码解析问题根源,并提供有效解决方案。

问题现象

WebSocket 配置伪代码

typescript 复制代码
@Autowired
@Bean
public HandlerMapping webSocketMapping(final EchoHandler echoHandler) {
    Map<String, WebSocketHandler> map = new HashMap<>();
    map.put("/echo", echoHandler);
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
    mapping.setUrlMap(map);
    return mapping;
}

@Bean
public WebSocketHandlerAdapter handlerAdapter() {
    return new WebSocketHandlerAdapter(); // 问题关键在这里
}

即使配置了:

yaml 复制代码
spring:
  cloud:
    gateway:
      httpclient:
        websocket:
          max-frame-payload-length: xxxxxx

仍然会触发默认最大帧长度 65536 的限制错误。

为什么配置了 max-frame-payload-length 却无效?

这要从 WebSocketHandlerAdapter 的源码开始分析。

csharp 复制代码
public WebSocketHandlerAdapter() {
    this(new HandshakeWebSocketService()); // 使用默认HandshakeWebSocketService
}

HandshakeWebSocketService 的默认构造器如下:

csharp 复制代码
// gateway 使用的是reactor netty 所以最后初始化出来的是ReactorNettyRequestUpgradeStrategy
public HandshakeWebSocketService() {
    this(initUpgradeStrategy()); // 初始化 ReactorNettyRequestUpgradeStrategy
}

默认会使用 ReactorNettyRequestUpgradeStrategy

csharp 复制代码
public ReactorNettyRequestUpgradeStrategy() {
    this(WebsocketServerSpec::builder); // 使用无参 builder
}

进一步查看 WebsocketServerSpec.Builder 的实现:

scala 复制代码
public static class Builder<SPEC extends Builder<SPEC>> implements Supplier<SPEC> {
    int maxFramePayloadLength = 65536; // 默认值
    ...
}

结论

Spring Cloud Gateway 的 spring.cloud.gateway.httpclient.websocket.max-frame-payload-length 配置项实际上无法修改底层 WebsocketServerSpecmaxFramePayloadLength 值,因为默认构造方式不会读取该配置。

有效解决方案

我们可以自己构建一个带参数的 ReactorNettyRequestUpgradeStrategy,明确设置 maxFramePayloadLength

通过显式配置 ReactorNettyRequestUpgradeStrategy 并自定义 WebsocketServerSpec

java 复制代码
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
    int frameSizeLimit = 655360; // 设置为你所需的帧长度限制
    WebsocketServerSpec.Builder builder = WebsocketServerSpec.builder().maxFramePayloadLength(frameSizeLimit);
    ReactorNettyRequestUpgradeStrategy upgradeStrategy = new ReactorNettyRequestUpgradeStrategy(builder);
    return new WebSocketHandlerAdapter(new HandshakeWebSocketService(upgradeStrategy));
}

补充说明

若需要配置更多 WebSocket 参数(如启用压缩、调整 Ping/Pong 处理策略),可在 WebsocketServerSpec.Builder 中链式调用:

scss 复制代码
WebsocketServerSpec.Builder builder = WebsocketServerSpec.builder()
        .maxFramePayloadLength(655360)
        .compress(true)  // 启用压缩
        .handlePing(false); // 禁止自动处理Ping帧

总结:

  • spring.cloud.gateway.httpclient.websocket.max-frame-payload-length针对 gateway 客户端通信(即作为 WebSocket 客户端)配置的;

  • 如果你是通过自定义 WebSocketHandlerAdapter 来处理 WebSocket 请求(即作为 WebSocket 服务端),则必须手动指定 maxFramePayloadLength

  • 默认构造路径中的帧大小限制是写死在源码中的,必须通过构造器覆盖。

相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码1 天前
别再前后端各写一套表单校验了
java·后端
大勇前进1 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
yuzhi_liu1 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile1 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白801 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端
二月龙1 天前
PHP 接口返回统一响应封装,让前后端对接更省心
后端
只睡四小时1 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas