netty编解码学习

netty客户端和服务端, 都需要继承SimpleChannelInboundHandler 或者实现ChannelInboundHandler或者继承ChannelInboundHandlerAdapter
先看下这两个类的区别:

io.netty.channel.ChannelInboundHandler;
io.netty.channel.SimpleChannelInboundHandler;
io.netty.channel.ChannelInboundHandlerAdapter;

这三个类的继承关系如下:
在这里插入图片描述
io.netty.channel.ChannelInboundHandler;

ChannelInboundHandlerAdapter 对 ChannelInboundHandler进行了封装,
SimpleChannelInboundHandler 又对ChannelInboundHandlerAdapter 进行了封装

再看源码:
在这里插入图片描述
ChannelInboundHandlerAdapter 中 channelRead的参数msg类型为Object类型(需接收ByteBuf类型,其他类型接收不到),
而SimpleChannelInboundHandler用传进来的泛型 对 Object msg 进行了处理,使得channelRead0的参数可以为以下效果
在这里插入图片描述
这就是实现SimpleChannelInboundHandler 和ChannelInboundHandlerAdapter 在channelRead上的区别;

netty是以byteBuf类型传输的 ,所以在传输前后要对传输的信息进行编码,解码.

public class NettyServer {
    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>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new StringDecoder());// 当信息进入服务端的handler之前进行解码
                            pipeline.addLast(new StringEncoder());//  当信息从服务端发送出去之前进行编码
                            //pipeline.addLast(new ObjectDecoder(10240, ClassResolvers.cacheDisabled(null)));
                            pipeline.addLast(new NettyServerHandler());//前面的顺序可以调换,但这个一定是最后添加
                        }
                    });
            System.out.println("netty server start。。");
            ChannelFuture channelFuture = serverBootstrap.bind(9000).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

对象类型传输:(序列化操作慢)

ctx.writeAndFlush(new User(1,"zhangsan")); // 客户端直接传输对象
 pipeline.addLast(new ObjectEncoder()); // 客户端进行编码
 pipeline.addLast(new NettyClientHandler());
pipeline.addLast(new ObjectDecoder(10240, ClassResolvers.cacheDisabled(null)));// 解码
pipeline.addLast(new NettyServerHandler());  // 服务端

对象使用Protostuff传输(序列化快) 序列化工具类:

  ByteBuf buf = Unpooled.copiedBuffer(ProtostuffUtil.serializer(new User(1, "zhangsan")));
  ctx.writeAndFlush(buf);  // 客户端
   ByteBuf buf = (ByteBuf) msg;
   byte[] bytes = new byte[buf.readableBytes()];
  buf.readBytes(bytes);
  ProtostuffUtil.deserializer(bytes, User.class); // 服务端对收到的对象解码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值