【Netty】编解码器

当你通过 Netty 发送或者接受一个消息的时候,就将会发生一次数据转换

  • 入站时 – 消息被解码(Decode):从字节(二进制)转换为我们能读懂并操作的格式(int、String、Java对象…)
  • 出站时 – 消息被编码(Encode):从我们能读懂并操作的格式的形式(int、String、Java对象…)转换为字节形式(二进制)

Netty 提供了一系列实用的编码解码器,比如编解码字符串的 StringEncoder 和 StringDecoder,编解码对象的 ObjectEncoder 和ObjectDecoder 等,他们都实现了 ChannelInboundHadnler 或者 ChannelOutcoundHandler 接口。

【Netty】编解码器_第1张图片

在这些类中, channelRead 方法已经被重写了。以入站为例,对于每个从入站 Channel 读取的消息,这个方法会被调用。随后,它将调用由已知解码器所提供的 decode() 方法进行解码,并将已经解码的字节转发给 ChannelPipeline 中的下一个 ChannelInboundHandler。

除了 Netty 已经实现的,我们也可以通过集成 MessageToByteEncoder,ByteToMessageDecoder 自定义编解码器。

【Netty】编解码器_第2张图片
/*
* 自定义编码器
* 实现 MessageToByteEncoder接口,重写 encode 方法
* 注:通过泛型指定可以编码数据类型
*/
public class LongToByteEncoder extends MessageToByteEncoder<Long> {
     
    @Override
    protected void encode(ChannelHandlerContext ctx, Long msg, ByteBuf out) throws Exception {
     
        System.out.println("LongToByteEncoder encode被调用");
        System.out.println("msg=" + msg);
        // 关键!!将编码结果通过 pipeline 继续向下传递,继续接受处理
        out.writeLong(msg);
    }
}
/*
* 自定义解码器
* 实现 ByteToMessageDecoder 接口,重写 decode 方法
*/
public class ByteToLongDecoder extends ByteToMessageDecoder {
     

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
     
        System.out.println("ByteToLongDecoder decode 被调用");
        // 注:因为 long 8个字节, 需要判断有8个字节,才能读取一个long
        if(in.readableBytes() >= 8) {
     
        	// 关键!!将解码结果通过 pipeline 继续向下传递,继续接受处理
            out.add(in.readLong());
        }
    }
}

然后再将我们的自定义编解码器加入到 ChannelPipeline 中

public class NettyServerInitailizer extends ChannelInitializer<SocketChannel> {
     
    @Override
    protected void initChannel(SocketChannel socketChannel) {
     

        ChannelPipeline pipeline = socketChannel.pipeline();
		// 入站-Decode
		pipeline.addLast(new ByteToLongDecoder());
		// 出栈-Encode
		pipeline.addLast(new LongToByteEncoder());
		// 自定义的 Handler
		pipeline.addLast(new NettyServerHandler());
    }
}

你可能感兴趣的:(Netty,RPC框架,netty)