> 文章列表 > Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解

Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解

Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解

文章目录

  • 一、编解码器概述
    • 1、编解码器概述
    • 2、编码器类关系图
    • 3、解码器类关系图
  • 二、以编解码器为例理解入站出站
    • 1、Server端
    • 2、Client端
    • 3、编解码器
    • 3、执行查看结果
    • 4、注意事项
  • 三、Netty其他内置编解码器
    • 1、ReplayingDecoder
    • 2、其他编码器
    • 3、内置编解码器处理粘包拆包问题
  • 四、自定义编解码器实现粘包拆包问题

一、编解码器概述

1、编解码器概述

当Netty发送或者接收一个消息的时候,就会发生一次数据转换。入站消息会被解码(从字节转换为另一种格式,比如java对象);出站消息会被编码成字节。

Netty 提供一系列实用的编解码器,他们都实现了 ChannelInboundHadnler 或者 ChannelOutboundHandler 接口。在这些类中,channelRead 方法已经被重写了。以入站为例,对于每个从入站 Channel 读取的消息,这个方法会被调用。随后,它将调用由解码器所提供的 decode()方法进行解码,并将已经解码的字节转发给 ChannelPipeline中的下一个 ChannelInboundHandler。

2、编码器类关系图

编码器都继承了MessageToByteEncoder抽象类,并且需要重写其encode方法。
Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解
我们发现,编码器继承了ChannelOutboundHandlerAdapter,表示出站操作才会执行。

3、解码器类关系图

解码器都继承了ByteToMessageDecoder抽象类,并且需要重写其decode方法。
Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解
我们发现,解码器继承了ChannelInboundHandlerAdapter,表示入站操作才会执行。

二、以编解码器为例理解入站出站

1、Server端


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class MyServer {public static void main(String[] args) throws Exception{EventLoopGroup bossGroup = new NioEventLoopGroup(1);EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();//一会下断点//入站的handler进行解码 MyByteToLongDecoderpipeline.addLast(new MyByteToLongDecoder());//出站的handler进行编码pipeline.addLast(new MyLongToByteEncoder());//自定义的handler 处理业务逻辑pipeline.addLast(new MyServerHandler());System.out.println("end");}}); //自定义初始化pipelineChannelFuture channelFuture = serverBootstrap.bind(7000).sync();channelFuture.channel().closeFuture().sync();}finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}
}import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;public class MyServerHandler extends SimpleChannelInboundHandler<Long> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {System.out.println("从客户端" + ctx.channel().remoteAddress() + " 读取到long " + msg);//给客户端发送一个longctx.writeAndFlush(98765L);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}

2、Client端


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class MyClient {public static void main(String[] args)  throws  Exception{EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();//加入一个出站的handler 对数据进行一个编码pipeline.addLast(new MyLongToByteEncoder());//这时一个入站的解码器(入站handler )pipeline.addLast(new MyByteToLongDecoder());//加入一个自定义的handler , 处理业务pipeline.addLast(new MyClientHandler());}}); //自定义一个初始化类ChannelFuture channelFuture = bootstrap.connect("localhost", 7000).sync();channelFuture.channel().closeFuture().sync();}finally {group.shutdownGracefully();}}
}import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;public class MyClientHandler  extends SimpleChannelInboundHandler<Long> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {System.out.println("服务器的ip=" + ctx.channel().remoteAddress());System.out.println("收到服务器消息=" + msg);}//重写channelActive 发送数据@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("MyClientHandler 发送数据");ctx.writeAndFlush(123456L); //发送的是一个long}
}

3、编解码器


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;public class MyLongToByteEncoder extends MessageToByteEncoder<Long> {//编码方法@Overrideprotected void encode(ChannelHandlerContext ctx, Long msg, ByteBuf out) throws Exception {System.out.println("MyLongToByteEncoder encode 被调用");System.out.println("msg=" + msg);out.writeLong(msg);}
}import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;import java.util.List;public class MyByteToLongDecoder extends ByteToMessageDecoder {/**** decode方法 会根据接收的数据,被调用多次, 直到确定没有新的元素被添加到list* , 或者是ByteBuf 没有更多的可读字节为止* 如果list out 不为空,就会将list的内容传递给下一个 channelinboundhandler处理, 该处理器的方法也会被调用多次** @param ctx 上下文对象* @param in 入站的 ByteBuf* @param out List 集合,将解码后的数据传给下一个handler* @throws Exception*/@Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {System.out.println("MyByteToLongDecoder 被调用");//因为 long 8个字节, 需要判断有8个字节,才能读取一个longif(in.readableBytes() >= 8) {out.add(in.readLong());}}
}

3、执行查看结果

client端:

MyClientHandler 发送数据
MyLongToByteEncoder encode 被调用
msg=123456
MyByteToLongDecoder 被调用
服务器的ip=localhost/127.0.0.1:7000
收到服务器消息=98765

server端:

MyByteToLongDecoder 被调用
从客户端/127.0.0.1:58478 读取到long 123456
MyLongToByteEncoder encode 被调用
msg=98765

根据我们的执行结果,我们可以简单的得出一个结论,执行流程大致是这样的:

Netty编解码器,Netty自定义编解码器解决粘包拆包问题,Netty编解码器的执行过程详解

4、注意事项

编解码器只能处理指定类型的数据,如果数据类型不匹配,会跳过编解码,但是数据不会丢失。

比如说客户端ctx.writeAndFlush(Unpooled.copiedBuffer(“abcdabcdabcdabcd”,CharsetUtil.UTF_8)); 发送一个16位的字符串,Encoder的encode方法虽然会执行,但是并不会编码。因此我们编写 Encoder 是要注意传入的数据类型和处理的数据类型一致。

我们看一下MessageToByteEncoder的write方法:

// io.netty.handler.codec.MessageToByteEncoder#write
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {ByteBuf buf = null;try {if (acceptOutboundMessage(msg)) {//判断当前msg 是不是应该处理的类型,如果是就处理,不是就跳过encode@SuppressWarnings("unchecked")I cast = (I) msg;buf = allocateBuffer(ctx, cast, preferDirect);try {encode(ctx, cast, buf);} finally {ReferenceCountUtil.release(cast);}if (buf.isReadable()) {ctx.write(buf, promise);} else {buf.release();ctx.write(Unpooled.EMPTY_BUFFER, promise);}buf = null;} else {ctx.write(msg, promise);}} catch (EncoderException e) {throw e;} catch (Throwable e) {throw new EncoderException(e);} finally {if (buf != null) {buf.release();}}
}

三、Netty其他内置编解码器

Netty内含许多编解码器,通常来说足够应对我们日常工作了:

1、ReplayingDecoder

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;import java.util.List;public class MyByteToLongDecoder extends ReplayingDecoder<Void> {@Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {System.out.println("MyByteToLongDecoder2 被调用");//在 ReplayingDecoder 不需要判断数据是否足够读取,内部会进行处理判断out.add(in.readLong());}
}

ReplayingDecoder 使用方便,但它也有一些局限性:
1、并不是所有的 ByteBuf操作都被支持,如果调用了一个不被支持的方法,将会抛出一个UnsupportedOperationException。
2、ReplayingDecoder 在某些情况下可能稍慢于 ByteToMessageDecoder,例如网络缓慢并且消息格式复杂时,消息会被拆成了多个碎片,速度变慢。

2、其他编码器

LineBasedFrameDecoder:这个类在 Netty 内部也有使用,它使用行尾控制字符 (\\n 或者\\r\\n)作为分隔符来解析数据。
DelimiterBasedFrameDecoder: 使用自定义的特殊字符作为消息的分隔符。
HttpObiectDecoder: 一个 HTTP 数据的解码器。
LengthFieldBasedFrameDecoder: 通过指定长度来标识整包消息,这样就可以自动的处理黏包和半包消息。

编码器与解码器都是成对出现的。

3、内置编解码器处理粘包拆包问题

Netty解决粘包和拆包问题的四种方案

四、自定义编解码器实现粘包拆包问题

Netty解决粘包拆包问题,Netty使用自定义编解码器解决粘包拆包问题