netty : io.netty.handler.codec.TooLongFrameException

版权声明:本文为博主九师兄(QQ群:spark源代码 198279782 欢迎来探讨技术)原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21383435/article/details/90111823

背景

做netty权威指南的私有协议开发,案例报错如

io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 795898487 - discarded
	at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:501)
	at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:477)
	at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:403)
	at com.netty.chapter12.summary3.NettyMessageDecoder.decode(NettyMessageDecoder.java:33)
	

然后查看代码

   public NettyMessageDecoder(int maxFrameLength, int lengthFieldOffset,
                               int lengthFieldLength) throws IOException {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
        marshallingDecoder = new MarshallingDecoder();
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in)
            throws Exception {
        ByteBuf frame = (ByteBuf) super.decode(ctx, in); // 这行报错
        if (frame == null) {
            return null;
        }

【原因分析】

服务中心目前采用的netty通信,netty自身实现的报文编解码器有最大报文长度限制,其默认值为1048576(1M)。

【解决办法】

但是我看见这句代码

NettyMessageDecoder(int maxFrameLength

执行find use 后发现初始化为

 ch.pipeline().addLast(
                                    new NettyMessageDecoder(1024 * 1024, 4, 4));
                            

于是我该小点

 ch.pipeline().addLast(
                                    new NettyMessageDecoder(1024 * 10, 4, 4));
                                    
               

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/90111823