WebSocket 1009 Max frame length of 65536 has been exceeded 问题
项目中使用Spring Cloud Gateway 作为网关,代理WebScoket通信,当请求数据量很大的时候,会出现 1009问题。
onClosing, code = 1009, reason = Max frame length of 65536 has been exceeded.
public final class CloseStatus implements Serializable {
/**
* "1009 indicates that an endpoint is terminating the connection because it has
* received a message that is too big for it to process."
*/
public static final CloseStatus TOO_BIG_TO_PROCESS = new CloseStatus(1009);
}
翻查网上的资料与查看源码发现确实有限制。
// org.springframework.web.reactive.socket.adapter.NettyWebSocketSessionSupport
public abstract class NettyWebSocketSessionSupport<T> extends AbstractWebSocketSession<T> {
/**
* The default max size for inbound WebSocket frames.
*/
public static final int DEFAULT_FRAME_MAX_SIZE = 64 * 1024;
}
网上的资料都是重新定义bean,可能我这使用版本比较高,gateway 中有配置项,配置一下即可。
Spring Clound 与 Spring Boot 版本
plugins {
id("org.springframework.boot") version "2.3.7.RELEASE"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.4.32"
kotlin("plugin.spring") version "1.4.32"
}
extra["springCloudVersion"] = "Hoxton.SR9"
application.yml
中配置。
spring:
cloud:
gateway:
httpclient:
websocket:
max-frame-payload-length: 2097152 # 2M
同时再配置下WebSocket服务。
@Component
class WebSocketHandler : TextWebSocketHandler() {
companion object {
const val MAX_SIZE = 3 * 1024 * 1024 // 3M
}
override fun afterConnectionEstablished(session: WebSocketSession) {
logInfo("afterConnectionEstablished $session")
logInfo("onConnect 收到客户连接 ${session.remoteAddress.ip()} ")
session.binaryMessageSizeLimit = MAX_SIZE
session.textMessageSizeLimit = MAX_SIZE
}
}
修改以上,即可搞定。
网友评论