Netty学习(七):心跳检测机制

一、什么是心跳检测机制

所谓心跳, 即在 TCP 长连接中, 客户端和服务器之间定期发送的一种特殊的数据包, 通知对方自己还在线, 以确保 TCP 连接的有效性.

心跳机制主要是客户端和服务端长时间连接时,客户端需要定时发送心跳包来保证自己是存活的,否则一个连接长时间没有作用就会浪费服务端的资源。

二、心跳检测机制的适用场景

长连接的应用场景非常的广泛,比如监控系统,IM系统,即时报价系统,推送服务等等。像这些场景都是比较注重实时性,如果每次发送数据都要进行一次DNS解析,建立连接的过程肯定是极其影响体验。

而长连接的维护必然需要一套机制来控制。比如 HTTP/1.0 通过在 header 头中添加 Connection:Keep-Alive参数,如果当前请求需要保活则添加该参数作为标识,否则服务端就不会保持该连接的状态,发送完数据之后就关闭连接。HTTP/1.1以后 Keep-Alive 是默认打开的。

Netty 是基于 TCP 协议开发的,在四层协议 TCP 协议的实现中也提供了 keepalive 报文用来探测对端是否可用。TCP 层将在定时时间到后发送相应的 KeepAlive 探针以确定连接可用性。

所以,心跳检测一般存在于建立长连接 或者 需要保活的场景。

三、netty的心跳检测机制

基础协议对应用来说不是那么尽善尽美,一个 Netty 服务端可能会面临上万个连接,如何去维护这些连接是应用应该去处理的事情。在 Netty 中提供了 IdleStateHandler 类专门用于处理心跳。

IdleStateHandler 的构造函数如下:

CopypublicIdleStateHandler(long readerIdleTime, long writerIdleTime, 
                            long allIdleTime,TimeUnit unit){  
}

说明:

  1. IdleStateHandler 是netty 提供的处理空闲状态的处理器

  1. long readerIdleTime : 表示多长时间没有读, 就会发送一个心跳检测包检测是否连接

  1. long writerIdleTime : 表示多长时间没有写, 就会发送一个心跳检测包检测是否连接

  1. long allIdleTime : 表示多长时间没有读写, 就会发送一个心跳检测包检测是否连接

IdleStateHandler 的文档说明:

triggers an {@link IdleStateEvent} when a {@link Channel} has not performed
read, write, or both operation for a while.

四、Netty心跳检测机制实例

我用即将编写的一个例子来解释什么是心跳检测机制。我将要实现这样的功能:

  1. 当服务器超过3秒没有读时,就提示读空闲

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

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

服务端:

public class MyServer {
    public static void main(String[] args) throws Exception{


        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop
        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap.group(bossGroup, workerGroup);
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    //加入一个netty 提供 IdleStateHandler
                    /*
                     *当 IdleStateEvent 触发后 , 就会传递给管道 的下一个handler去处理
                     *通过调用(触发)下一个handler 的 userEventTiggered , 在该方法中去处理 IdleStateEvent(读空闲,写空闲,读写空闲)
                     */
                    pipeline.addLast(new IdleStateHandler(7000,7000,10, TimeUnit.SECONDS));
                    //加入一个对空闲检测进一步处理的handler(自定义)
                    pipeline.addLast(new MyServerHandler());
                }
            });

            //启动服务器
            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            channelFuture.channel().closeFuture().sync();

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

自定义handler(在该handler中决定如何处理):

public class MyServerHandler extends ChannelInboundHandlerAdapter {

    /**
     *
     * @param ctx 上下文
     * @param evt 事件
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if(evt instanceof IdleStateEvent) {

            //将  evt 向下转型 IdleStateEvent
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = null;
            switch (event.state()) {
                case READER_IDLE:
                  eventType = "读空闲";
                  break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
            }
            //这里已经可以知道浏览器所处的空闲是何种空闲,可以执行对应的处理逻辑了
            System.out.println(ctx.channel().remoteAddress() + "--超时时间--" + eventType);
            System.out.println("服务器做相应处理..");

            //如果发生空闲,我们关闭通道
           // ctx.channel().close();
        }
    }
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty 提供了一种称为 "IdleStateHandler" 的内置处理器,用于实现心跳检测机制。它可以帮助你检测连接的空闲状态,并触发相应的事件。 下面是一个使用 `IdleStateHandler` 的示例代码: ```java import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.timeout.IdleStateHandler; import java.util.concurrent.TimeUnit; public class HeartbeatClient { private final String host; private final int port; public HeartbeatClient(String host, int port) { this.host = host; this.port = port; } public void start() throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) { // 添加 IdleStateHandler 处理器 ch.pipeline().addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS)); // 添加自定义的处理器 ch.pipeline().addLast(new HeartbeatHandler()); } }); ChannelFuture future = b.connect(host, port).sync(); future.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws InterruptedException { String host = "localhost"; int port = 8080; HeartbeatClient client = new HeartbeatClient(host, port); client.start(); } } ``` 在上述示例代码中,我们添加了一个 `IdleStateHandler` 处理器到 ChannelPipeline 中。该处理器有三个参数:readerIdleTime、writerIdleTime 和 allIdleTime。这些参数分别表示读空闲时间、写空闲时间和读写空闲时间。在本例中,我们将读空闲时间设置为 5 秒。 当连接的读操作空闲超过指定的时间时,`IdleStateHandler` 会触发一个 "READER_IDLE" 事件。你可以在自定义的处理器中重写 `userEventTriggered` 方法来处理这个事件。在这个方法中,你可以编写发送心跳数据包的逻辑。 需要注意的是,上述示例中的 `HeartbeatHandler` 是一个自定义的处理器,你需要根据你的业务逻辑来实现该处理器。该处理器负责接收服务器的响应,并处理其他业务逻辑。 希望这个示例对你有帮助!如果有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值