Netty学习笔记(四):Netty应用(群聊、心跳机制、长连接)、Protobfuf、编解码器、TCP粘包和拆包

第 5 章 Netty 应用实例

一、Netty 应用实例-群聊系统

1、要求

编写一个 Netty 群聊系统,实现服务器端和客户端之间的数据简单通讯(非阻塞) ,实现多人群聊 。

服务器端:可以监测用户上线,离线,并实现消息转发功能

客户端:通过 channel 可以无阻塞发送消息给其它所有用户,同时可以接受其它用户发送的消息(有服务器转发 得到)

2、实现

服务器端

public class ChatGroupServer {

    private static final int PORT = 6666;

    public ChatGroupServer() {
    }

    public static void main(String[] args) throws InterruptedException {
        ChatGroupServer chatGroupServer = new ChatGroupServer();
        chatGroupServer.run();
    }

    public void run() throws InterruptedException {

        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            //创建初始化器
            ServerBootstrap bootstrap = new ServerBootstrap();

            //设置初始化参数
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //获取pipeline
                            ChannelPipeline pipeline = ch.pipeline();

                            //向pipelinez中添加handler
                            //添加解码器
                            pipeline.addLast(new StringDecoder());
                            //添加编码器
                            pipeline.addLast(new StringEncoder());
                            //添加自定义业务处理handler
                            pipeline.addLast(new ChatGroupServerHandler());
                        }
                    });

            System.out.println("服务器启动");
            ChannelFuture channelFuture = bootstrap.bind(PORT).sync();

            //监听关闭
            channelFuture.channel().closeFuture().sync();

        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

服务器端Handler

public class ChatGroupServerHandler extends SimpleChannelInboundHandler<String> {

    //定义一个channel组,管理所有客户端的channel
    //GlobalEventExecutor是一个全局事件执行器
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);


    //处理客户端的连接事件。表示连接已建立,一旦建立会调用此方法
    //将当前channel加入channelGroup中
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        //将客户加入的消息推送给其他客户
        //channelGroup.writeAndFlush的作用是将消息发送给channelGroup中所有的channel
        channelGroup.writeAndFlush("[客户端 " + channel.remoteAddress() + "] : 加入聊天");
        //将新上线的客户加入channelGroup
        channelGroup.add(channel);
    }

    //表示断开连接,将离开消息推送给其他在线客户端
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.writeAndFlush("[客户端 " + channel.remoteAddress() + "] : 离开群聊");
//        //ChannelGroup会自动执行该方法
//        channelGroup.remove(channel);
        System.out.println("channelGroup size : " + channelGroup.size());
    }

    //表示 channel 处于活动状态,服务器端提示xx上线
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("[客户端 " + ctx.channel().remoteAddress() + "] : 上线");
    }

    //表示 channel 处于非活动状态,服务器端提示xx离线
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("[客户端 " + ctx.channel().remoteAddress() + "] : 离线");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //消息发送者的Channel
        Channel sender = ctx.channel();
        String s = "[客户端 " + sender.remoteAddress() + "] : " + msg;
        System.out.println(s);

        //将消息转发给其他人,并且排除发送者
        channelGroup.forEach((receiver) ->{
            if (receiver != sender) {
                receiver.writeAndFlush(s);
            }
        });
    }

    //处理异常
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //关闭通道
        ctx.close();
    }
}

客户端:

public class ChatGroupClient {

    private static final String HOST = "127.0.0.1";
    private static final int PORT = 6666;

    public ChatGroupClient() {
    }

    public static void main(String[] args) throws InterruptedException {
        ChatGroupClient chatGroupClient = new ChatGroupClient();
        chatGroupClient.run();
    }

    public void run() throws InterruptedException {

        //创建线程组
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

        try {
            //创建初始化器,并设置
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            //加入相关handler
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new ChatGroupClientHandler());
                        }
                    });

            //连接服务器
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(HOST, PORT)).sync();
            Channel channel = channelFuture.channel();
            System.out.println("----------" + channel.localAddress() + "-----------");

            //创建scanner,循环发送信息
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String s = scanner.nextLine();
                channel.writeAndFlush(s + "\n");
            }

            //监听关闭事件
            channel.closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}

客户端Handler:

public class ChatGroupClientHandler extends SimpleChannelInboundHandler<String> {
    //接收客户端转发的消息
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }
}

结果:

客户端1输出:

----------/127.0.0.1:13240-----------
[客户端 /127.0.0.1:13257] : 加入聊天
你好啊
[客户端 /127.0.0.1:13257] : 你好,你叫什么名字
我是你爸爸啊
[客户端 /127.0.0.1:13257] : 谁?我儿子?

客户端2输出:

----------/127.0.0.1:13257-----------
[客户端 /127.0.0.1:13240] : 你好啊
你好,你叫什么名字
[客户端 /127.0.0.1:13240] : 我是你爸爸啊
谁?我儿子?
[客户端 /127.0.0.1:13240] : 离开群聊

服务器端输出:

[客户端 /127.0.0.1:13240] : 上线
[客户端 /127.0.0.1:13257] : 上线
[客户端 /127.0.0.1:13240] : 你好啊
[客户端 /127.0.0.1:13257] : 你好,你叫什么名字
[客户端 /127.0.0.1:13240] : 我是你爸爸啊
[客户端 /127.0.0.1:13257] : 谁?我儿子?
[客户端 /127.0.0.1:13240] : 离线
channelGroup size : 1
[客户端 /127.0.0.1:13257] : 离线
channelGroup size : 0

二、 Netty 应用实例-心跳检测机制

1、要求

编写一个 Netty 心跳检测机制案例, 当服务器超过 3 秒没有读时,就提示读空闲

当服务器超过 5 秒没有写操作时,就提示写空闲

实现当服务器超过 7 秒没有读或者写操作时,就提示读写空闲

2、实现

public class HeartBeatServer {

    public static void main(String[] args) {

        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO)) //增加日志处理器
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            /**
                             * 相关管道中加入一个 netty 提供的 IdleStateHandler
                             * 说明:
                             *      IdleStateHandler 是 netty 处理空闲状态的处理器
                             *      其中参数包括:
                             *      readerIdleTime:表示如果多久没有读,就会发送一个心跳检测包检测是否连接
                             *      writerIdleTime:表示如果多久没有写,就会发送一个心跳检测包检测是否连接
                             *      allIdleTime:表示如果多久没有读写,就会发送一个心跳检测包检测是否连接
                             *      当IdleStateHandler触发之后,就会传递给管道中的下一个handler进行处理
                             *      通过调用下一个handler的userEventTriggered去处理读空闲、写空闲、都写空闲
                             */
                            pipeline.addLast(new IdleStateHandler(
                                    3,5,7, TimeUnit.SECONDS));
                            pipeline.addLast(new HeartBeatServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(6666).sync();

            channelFuture.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
public class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * 对空闲事件进行相应的处理
     * @param ctx 上下文
     * @param evt 事件
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;

            String evtType = null;
            //判断事件类型
            switch (event.state()) {
                case READER_IDLE:
                    evtType = "读空闲";
                    break;
                case WRITER_IDLE:
                    evtType = "写空闲";
                    break;
                case ALL_IDLE:
                    evtType = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + " : " + evtType);
            //TODO 做相应的处理...
        }
    }
}

使用一个客户连接心跳检测服务端之后,输出结果:

/127.0.0.1:1728 : 读空闲
/127.0.0.1:1728 : 写空闲
/127.0.0.1:1728 : 读空闲
/127.0.0.1:1728 : 读写空闲
/127.0.0.1:1728 : 读空闲
/127.0.0.1:1728 : 写空闲
/127.0.0.1:1728 : 读空闲

三、Netty 应用实例-实现长连接

1、要求

Http 协议是无状态的, 浏览器和服务器间的请求响应一次,下一次会重新创建连接.

要求:实现基于 webSocket 的长连接的全双工的交互

改变 Http 协议多次请求的约束,实现长连接了, 服务器可以发送消息给浏览器。客户端浏览器和服务器端会相互感知,比如服务器关闭了,浏览器会感知,同样浏览器关闭了,服务器会感知

2、实现

//实现长连接
public class WebSocketServer {

    public static void main(String[] args) {

        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        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();

                            //加入netty提供的httpServerCodec =》(code + decode)
                            //HttpServerCodec 是netty提供的http编码-解码器
                            pipeline.addLast(new HttpServerCodec());
                            //是以块的方式写,所以添加ChunkedWriteHandler处理器
                            pipeline.addLast(new ChunkedWriteHandler());
                            /*
                                http数据在传输的过程中是分段的,这就是为什么,当浏览器发送大量数据时,会发出多次http请求
                                HttpObjectAggregator就是可以将多个段进行聚合
                             */
                            pipeline.addLast(new HttpObjectAggregator(8192));
                            /*说明:
                                1、对应websocket,它的数据是以 帧(frame)的形式传递
                                2、WebSocketFrame 下面有6个子类
                                3、WebSocketServerProtocolHandler的核心功能是将http协议升级为ws协议,保持长连接
                                4、参数对应其uri
                             */
                            pipeline.addLast(new WebSocketServerProtocolHandler("/hello"));

                            //自定义Handler处理业务逻辑
                            pipeline.addLast(new TextWebSocketFrameHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(6666).sync();

            channelFuture.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
//TextWebSocketFrame表示一个文本帧
public class TextWebSocketFrameHandler
        extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println("服务器收到消息:" + msg.text());

        //回复消息
        String s = "[服务器时间:" + LocalDateTime.now() + "] : " + msg.text();
        ctx.channel().writeAndFlush(new TextWebSocketFrame(s));
    }

    //当web客户端连接后出发该方法
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //id 表示唯一的值,LongText 是唯一的 ShortText 不是唯一
        System.out.println("handlerAdded 被调用" + ctx.channel().id().asLongText());
        System.out.println("handlerAdded 被调用" + ctx.channel().id().asShortText());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerRemoved 被调用" + ctx.channel().id().asLongText());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("出现异常:" + cause.getMessage());
        ctx.close();
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持websocket
    if(window.WebSocket) {
        //go on
        socket = new WebSocket("ws://localhost:6666/hello");
        //相当于channelReado, ev 收到服务器端回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }

        //相当于连接开启(感知到连接开启)
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了.."
        }

        //相当于连接关闭(感知到连接关闭)
        socket.onclose = function (ev) {

            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭了.."
        }
    } else {
        alert("当前浏览器不支持websocket")
    }

    //发送消息到服务器
    function send(message) {
        if(!window.socket) { //先判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket 发送消息
            socket.send(message)
        } else {
            alert("连接没有开启");
        }
    }
</script>
    <form onsubmit="return false">
        <textarea name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>

结果:首先在页面发送信息:

在这里插入图片描述

可以看到消息顺利的回显到了页面,并且服务器端的控制台也显示出来了:

handlerAdded 被调用5ce0c5fffe05000f-00003dc0-00000001-3a2525e615eafff5-5b0722ed
handlerAdded 被调用5b0722ed
服务器收到消息:你好
服务器收到消息:你也好

当页面关闭时,服务器端也出现了断开连接的消息

handlerAdded 被调用5ce0c5fffe05000f-00003dc0-00000001-3a2525e615eafff5-5b0722ed
handlerAdded 被调用5b0722ed
服务器收到消息:你好
服务器收到消息:你也好
出现异常:远程主机强迫关闭了一个现有的连接。
handlerRemoved 被调用5ce0c5fffe05000f-00003dc0-00000001-3a2525e615eafff5-5b0722ed

四、 Log4j 整合到 Netty

1、在Maven 中添加对Log4j的依赖 在 pom.xml

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>

2、配置 Log4j , 添加resources/log4j.properties文件,文件内容如下:

log4j.rootLogger=debug,appender1

log4j.appender.appender1=org.apache.log4j.ConsoleAppender

log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout

log4j.appender.stdout.layout.ConversionPattern=[%p] %C{1} -%m%n

3、输出:

[main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
[main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
[main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
CodecClientHandler::channelActive 发送数据
[nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 32768
[nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
[nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
[nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8

第 6 章 Google Protobuf

一、编码和解码介绍

1、基本介绍

编写网络应用程序时,因为数据在网络中传输的都是二进制字节码数据,在发送数据时就需要编码,接收数据时就需要解码

codec(编解码器) 的组成部分有两个:decoder(解码器)和 encoder(编码器)。 encoder 负责把业务数据转换成字节 码数据,decoder 负责把字节码数据转换成业务数据

在这里插入图片描述

2、Netty 本身的编码解码的机制

Netty 自身提供了一些 codec(编解码器):

  • StringEncoder,对字符串数据进行编码
  • ObjectEncoder,对 Java 对象进行编码
  • StringDecoder, 对字符串数据进行解码
  • ObjectDecoder,对 Java 对象进行解码

Netty 本身自带的 ObjectDecoder 和 ObjectEncoder 可以用来实现 POJO 对象或各种业务对象的编码和解码,底层使用的仍是 Java 序列化技术 , 而 Java 序列化技术本身效率就不高,存在如下问题:

  • 无法跨语言
  • 序列化后的体积太大,是二进制编码的 5 倍多。
  • 序列化性能太低

对于上面的这些问题,可以使用Protobuf进行解决

二、Protobuf

1、基本介绍

Protobuf 是 Google 发布的开源项目,全称 Google Protocol Buffers,是一种轻便高效的结构化数据存储格式, 可以用于结构化数据串行化,或者说序列化。它很适合做数据存储RPC [远程过程调用 remote procedure call] 数据交换格式

参考文档 :https://developers.google.com/protocol-buffers/docs/proto

Protobuf 是以 message 的方式来管理数据的.

支持跨平台、跨语言,即[客户端和服务器端可以是不同的语言编写的] (支持目前绝大多数语言,例如 C++、 C#、Java、python 等) ,高性能,高可靠性

使用 protobuf 编译器能自动生成代码,Protobuf 是将类的定义使用.proto 文件进行描述,然后通过 protoc.exe 编译器根据.proto 自动生成.java 文件
在这里插入图片描述

2、Protobuf 入门实例

参考

第 7 章 Netty 编解码器和 handler 的调用机制

一、基本说明

netty 的组件设计:Netty 的主要组件有 Channel、EventLoop、ChannelFuture、ChannelHandler、ChannelPipe 等

ChannelHandler 充当了处理入站和出站数据的应用程序逻辑的容器。例如,实现 ChannelInboundHandler 接口(或 ChannelInboundHandlerAdapter),你就可以接收入站事件和数据,这些数据会被业务逻辑处理。当要给客户端 发 送 响 应 时 , 也 可 以 从 ChannelInboundHandler 冲 刷 数 据 。 业 务 逻 辑 通 常 写 在 一 个 或 者 多 个 ChannelInboundHandler 中。ChannelOutboundHandler 原理一样,只不过它是用来处理出站数据的

ChannelPipeline 提供了 ChannelHandler 链的容器。以客户端应用程序为例,如果事件的运动方向是从客户端到 服务端的,那么我们称这些事件为出站的,即客户端发送给服务端的数据会通过 pipeline 中的一系列 ChannelOutboundHandler,并被这些 Handler 处理,反之则称为入站的

在这里插入图片描述

二、编码解码器

1、基本介绍

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

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

2、解码器-ByteToMessageDecoder

ByteToMessageDecoder是用于解码的,所以继承了ChannelInboundHadnler。它的作用是将通过二进制字节码发送过来的数据转换为相应的数据,他的继承关系如下图所示:

在这里插入图片描述

由于不可能知道远程节点是否会一次性发送一个完整的信息,tcp 有可能出现粘包拆包的问题,这个类会对入 站数据进行缓冲,直到它准备好被处理,下面用一个例子说明:

这个例子,每次入站从ByteBuf中读取4字节,将其解码为一个int,然后将它添加到下一个List中。当没有更多元素可以被添加到该List中时,它的内容将会被发送给下一个ChannelInboundHandler。int在被添加到List中时,会被自动装箱为Integer。

public class ToIntegerDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (in.readableBytes() >= 4) {
            out.add(in.readInt());
        }
    }
}

在调用readInt()方法前必须验证所输入的ByteBuf是否具有足够的数据(4个字节)来生成int,流程如下图所示:
在这里插入图片描述

3、自定义的编码器和解码器

使用自定义的编码器和解码器来说明 Netty 的 handler 调用机制:

  • 客户端发送 long-> 服务器
  • 服务端发送 long-> 客户端

案例分析图例:

在这里插入图片描述

实现代码:

编解码器:

public class MyByteToLongDecoder extends ByteToMessageDecoder {

    /**
     *  将字节码解析为Long。该方法会被调用多次,知道没有新的元素被添加进List
     *  或者ByteBuf中没有更多的可读。之后List中的数据会被传递给下一个InBoundHandler,
     *  同时该InBoundHandler也会被调用多次
     * @param ctx 上下文
     * @param in ByteBuf
     * @param out 用于存放解码得到的数据,会传递给下一个Handler
     * @throws Exception
     */
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        System.out.println("MyByteToLongDecoder::decode 被调用");
        //对于字节码依次读取8个字节生成Long
        if (in.readableBytes() >= 8) {
            out.add(in.readLong());
        }

    }
}
public class MyLongToByteEncoder extends MessageToByteEncoder<Long> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Long msg, ByteBuf out) throws Exception {
        System.out.println("MyLongToByteEncoder::encode 被调用");
        System.out.println("msg = " + msg);
        System.out.println("-------------------------------------");
        out.writeLong(msg);
    }
}

服务器端:

public class CodecServer {

    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new CodecServerChannelInitializer());

            ChannelFuture channelFuture = bootstrap.bind(6666).sync();

            channelFuture.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
public class CodecServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        //加入InboundHandler进行解码
        pipeline.addLast(new MyByteToLongDecoder());

        //加加入OutboundHandler进行解码
        pipeline.addLast(new MyLongToByteEncoder());

        //加入自定InboundHandler将解码的数据进行输出
        pipeline.addLast(new CodecServerHandler());

    }
}
public class CodecServerHandler extends SimpleChannelInboundHandler<Long> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {

        System.out.println("CodecServerHandler::channelRead0 被调用");

        System.out.println("[客户端" + ctx.channel().remoteAddress() + "] : " + msg);

        System.out.println("-----------------------------------------");

        //给客户端回送一个long
        ctx.writeAndFlush(654321L);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
    }
}

客户端:

public class CodecClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new CodecClientChannelInitializer());

            ChannelFuture channelFuture = bootstrap.connect(
                    new InetSocketAddress("127.0.0.1", 6666)).sync();

            channelFuture.channel().closeFuture().sync();

        } finally {
            group.shutdownGracefully();
        }
    }
}
public class CodecClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        //向管道中加入出栈的 handler 对进行编码
        pipeline.addLast(new MyLongToByteEncoder());

        //向管道中加入出栈的 handler 对进行解码
        pipeline.addLast(new MyByteToLongDecoder());

        //加入处理业务逻辑的handler
        pipeline.addLast(new CodecClientHandler());
    }
}
public class CodecClientHandler extends SimpleChannelInboundHandler<Long> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        //对服务器端回送的数据进行读取
        System.out.println("CodecClientHandler::channelRead0");
        System.out.println("[服务器 " + ctx.channel().remoteAddress() + "] : " + msg);
    }
    
    //发送数据
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("CodecClientHandler::channelActive " + "发送数据");
        ctx.writeAndFlush(123456L);
    }
}

输出结果:

服务器端:

MyByteToLongDecoder::decode 被调用
CodecServerHandler::channelRead0 被调用
[客户端/127.0.0.1:13502] : 123456
-----------------------------------------
MyLongToByteEncoder::encode 被调用
msg = 654321

客户端:

CodecClientHandler::channelActive 发送数据
MyLongToByteEncoder::encode 被调用
msg = 123456
-------------------------------------
MyByteToLongDecoder::decode 被调用
CodecClientHandler::channelRead0
[服务器 /127.0.0.1:6666] : 654321

要点

  • 不论解码器handler 还是 编码器handler 。接收的消息类型必须与待处理的消息类型一致,否则该handler不会被执行。
  • 在解码器进行数据解码时,需要判断缓存区(ByteBuf)的数据是否足够 ,否则接收到的结果会期望结果可能不一致

4、解码器-ReplayingDecoder

ReplayingDecoder 扩展了 ByteToMessageDecoder 类,使用这个类,我们不必调用 readableBytes()方法。参数 S 指定了用户状态管理的类型,其中 Void 代表不需要状态管理

public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder 

使用:

public class MyByteToLongDecoder2 extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        System.out.println("MyByteToLongDecoder::decode 被调用");
        //在 ReplayingDecoder 不需要判断数据是否足够读取,内部会进行处理判断
        out.add(in.readLong());

    }
}

ReplayingDecoder 使用方便,但它也有一些局限性:

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

5、 其它编解码器

解码器:

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

上面这些解码器都有对应的编码器:
在这里插入图片描述

第 8 章 TCP 粘包和拆包 及解决方案

一、TCP 粘包和拆包基本介绍

TCP 是面向连接的,面向流的,提供高可靠性服务。

收发两端(客户端和服务器端)都要有一一成对的 socket, 因此,发送端为了将多个发给接收端的包,更有效的发给对方,使用了优化方法(Nagle 算法),将多次间隔较小且数据量小的数据,合并成一个大的数据块,然后进行封包。

这样做虽然提高了效率,但是接收端就难于分辨出完整的数据包了,因为面向流的通信是无消息保护边界的

由于 TCP 无消息保护边界, 需要在接收端处理消息边界问题,也就是我们所说的粘包、拆包问题。下面用一张图解释这个问题:

在这里插入图片描述

假设客户端分别发送了两个数据包 D1 和 D2 给服务端,由于服务端一次读取到字节数是不确定的,故可能存在以
下四种情况:

  • 服务端分两次读取到了两个独立的数据包,分别是 D1 和 D2,没有粘包和拆包
  • 服务端一次接受到了两个数据包,D1 和 D2 粘合在一起,称之为 TCP 粘包
  • 服务端分两次读取到了数据包,第一次读取到了完整的 D1 包和 D2 包的部分内容,第二次读取到了 D2 包的剩余内容,这称之为 TCP 拆包
  • 服务端分两次读取到了数据包,第一次读取到了 D1 包的部分内容 D1_1,第二次读取到了 D1 包的剩余部 分内容 D1_2 和完整的 D2 包。

二、TCP 粘包和拆包现象实例

在编写 Netty 程序时,如果没有做处理,就会发生粘包和拆包的问题。下面以一个实例,来展示粘包和拆包现象(这里只粘贴了Handler的代码,其余代码与之前无异):

public class MyServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int cnt;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte[] buffer = new byte[msg.readableBytes()];
        msg.readBytes(buffer);

        String s = new String(buffer, CharsetUtil.UTF_8);

        System.out.println("服务器收到消息:" + s);
        System.out.println("服务器收到的消息量" + (++cnt));

        //服务器端回复随机id给客户端
        ByteBuf response = Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8);
        ctx.writeAndFlush(response);
    }
}
public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int cnt;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //发送10条数据给服务器端
        for (int i = 0; i < 10; i++) {
            ByteBuf buf = Unpooled.copiedBuffer("hello,server " + i, CharsetUtil.UTF_8);
            ctx.writeAndFlush(buf);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte[] buffer = new byte[msg.readableBytes()];
        msg.readBytes(buffer);

        String s = new String(buffer, CharsetUtil.UTF_8);

        System.out.println("服务器收到消息: " + s);
        System.out.println("服务器收到的消息量" + (++cnt));
    }
}

第一次运行客户端,服务器端的输出:

服务器收到消息:hello,server 0hello,server 1hello,server 2hello,server 3hello,server 4hello,server 5hello,server 6hello,server 7hello,server 8hello,server 9
服务器收到的消息量1

第二次运行客户端,服务器端的输出:

服务器收到消息:hello,server 0
服务器收到的消息量1
服务器收到消息:hello,server 1
服务器收到的消息量2
服务器收到消息:hello,server 2hello,server 3hello,server 4
服务器收到的消息量3
服务器收到消息:hello,server 5hello,server 6
服务器收到的消息量4
服务器收到消息:hello,server 7
服务器收到的消息量5
服务器收到消息:hello,server 8
服务器收到的消息量6
服务器收到消息:hello,server 9
服务器收到的消息量7

第三次运行客户端,服务器端的输出:

服务器收到消息:hello,server 0
服务器收到的消息量1
服务器收到消息:hello,server 1
服务器收到的消息量2
服务器收到消息:hello,server 2hello,server 3hello,server 4
服务器收到的消息量3
服务器收到消息:hello,server 5
服务器收到的消息量4
服务器收到消息:hello,server 6
服务器收到的消息量5
服务器收到消息:hello,server 7hello,server 8hello,server 9
服务器收到的消息量6

可以看到三次的运行结果都不相同,这就是TCP的粘包和拆包引起。下面我们来看一下如何使用Netty解决这种问题。

三、TCP 粘包和拆包解决方案

关键就是要解决 服务器端每次读取数据长度的问题, 如果这个问题解决了,就不会出现服务器多读或少读数据的问 题,从而避免的 TCP 粘包、拆包 。

可以使用自定义协议 + 编解码器 来解决 。

实例:

要求客户端发送 5 个 Message 对象, 客户端每次发送一个 Message 对象

服务器端每次接收一个Message, 分5次进行解码, 每读取到 一个Message

代码:

传输类:

public class MessageProtocal {

    private int len;
    private byte[] content;

    public MessageProtocal() {
    }

    public MessageProtocal(int len, byte[] content) {
        this.len = len;
        this.content = content;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }
}

编码器:

public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocal> {
    //对MessageProtocal进行编码,转换为二进制字节流
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocal msg, ByteBuf out) throws Exception {

        System.out.println("MyMessageEncoder::encode 被调用");
        //放入长度和内容
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }
}

解码器:

public class MyMessageDecoder extends ReplayingDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        System.out.println("---------------------------------------------------");
        System.out.println("MyMessageDecoder::decode 被调用");
        //将二进制字节流转换为 MessageProtocal,并放入List中
        int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);
        out.add(new MessageProtocal(len, content));
    }
}

客户端handler:

public class MyClientHandler extends SimpleChannelInboundHandler<MessageProtocal> {

    private int cnt;

    //发送数据
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //发送10条数据给服务器端
        for (int i = 0; i < 5; i++) {
            String msg = "你好,服务器";
            byte[] content = msg.getBytes(Charset.forName("utf-8"));
            MessageProtocal messageProtocal = new MessageProtocal(content.length, content);
            ctx.writeAndFlush(messageProtocal);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocal msg) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

服务端handler:

public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocal> {

    private int cnt;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocal msg) throws Exception {

        String s = new String(msg.getContent(), CharsetUtil.UTF_8);

        System.out.println("服务器收到消息:" + s + " 长度为:" + msg.getLen());
        System.out.println("服务器收到的消息量 " + (++cnt));

        //服务器端回复随机id给客户端
        ByteBuf response = Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8);
        ctx.writeAndFlush(response);
    }
}

其他代码与上面类似,这里不再粘贴,关键是对消息的长度要进行定义。下面是服务器端输出结果:

---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 1
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 2
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 3
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 4
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 5

客户端:

MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
ntext ctx, MessageProtocal msg) throws Exception {

        String s = new String(msg.getContent(), CharsetUtil.UTF_8);

        System.out.println("服务器收到消息:" + s + " 长度为:" + msg.getLen());
        System.out.println("服务器收到的消息量 " + (++cnt));

        //服务器端回复随机id给客户端
        ByteBuf response = Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8);
        ctx.writeAndFlush(response);
    }
}

其他代码与上面类似,这里不再粘贴,关键是对消息的长度要进行定义。下面是服务器端输出结果:

---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 1
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 2
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 3
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 4
---------------------------------------------------
MyMessageDecoder::decode 被调用
服务器收到消息:你好,服务器 长度为:18
服务器收到的消息量 5

客户端:

MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
MyMessageEncoder::encode 被调用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值