
上一篇【第32篇】Netty背压机制——不让发送方“撑死“接收方下一篇【第34篇】 Netty Selector优化——为什么比JDK NIO快这么多开篇故事某物联网平台凌晨3点数据库连接池爆满。排查发现上万个IoT设备网络中断后没有心跳检测连接对象一直未释放TCP没有应用层心跳TCP的KeepAlive默认2小时才检测一次远远不够。一、IdleStateHandler——Netty心跳检测核心// 三种空闲检测pipeline.addLast(newIdleStateHandler(60,// readerIdleTime60秒没读 → 触发读空闲30,// writerIdleTime30秒没写 → 触发写空闲0// allIdleTime0表示不检测));实现原理publicclassIdleStateHandlerextendsChannelDuplexHandler{// 使用时间轮定时任务检测超时privatevoidinitialize(ChannelHandlerContextctx){if(readerIdleTime0){readerIdleTimeoutschedule(ctx,newReaderIdleTimeoutTask(ctx),readerIdleTime,TimeUnit.SECONDS);}}// 每次读/写事件重置定时器publicvoidchannelRead(ChannelHandlerContextctx,Objectmsg){if(readerIdleTime0){readingtrue;}ctx.fireChannelRead(msg);}// 超时后触发userEventTriggeredprotectedvoidchannelIdle(ChannelHandlerContextctx,IdleStateEventevt){ctx.fireUserEventTriggered(evt);}}二、心跳处理HandlerpublicclassHeartbeatHandlerextendsChannelInboundHandlerAdapter{OverridepublicvoiduserEventTriggered(ChannelHandlerContextctx,Objectevt){if(evtinstanceofIdleStateEvent){IdleStateEventevent(IdleStateEvent)evt;switch(event.state()){caseREADER_IDLE:System.out.println(读超时可能客户端已断开);ctx.close();// 关闭死连接break;caseWRITER_IDLE:System.out.println(写超时发送心跳包);ctx.writeAndFlush(newHeartbeatMsg());break;}}}}三、客户端断线重连publicclassReconnectingClient{privatefinalEventLoopGroupgroupnewNioEventLoopGroup();privatefinalBootstrapbootstrap;publicReconnectingClient(){bootstrapnewBootstrap().group(group).channel(NioSocketChannel.class).handler(newChannelInitializerChannel(){protectedvoidinitChannel(Channelch){ch.pipeline().addLast(newIdleStateHandler(0,10,0));ch.pipeline().addLast(newReconnectHandler());}});}publicvoidconnect(){bootstrap.connect(localhost,8080).addListener(f-{if(!f.isSuccess()){// 连接失败延迟重连group.schedule(()-connect(),5,TimeUnit.SECONDS);}});}classReconnectHandlerextendsChannelInboundHandlerAdapter{publicvoidchannelInactive(ChannelHandlerContextctx){System.out.println(连接断开5秒后重连...);ctx.channel().eventLoop().schedule(()-connect(),5,TimeUnit.SECONDS);}}}四、完整实战带心跳的聊天室publicclassHeartbeatChatServer{publicstaticvoidmain(String[]args)throwsException{EventLoopGroupbossnewNioEventLoopGroup(1);EventLoopGroupworkernewNioEventLoopGroup();try{newServerBootstrap().group(boss,worker).channel(NioServerSocketChannel.class).childHandler(newChannelInitializerChannel(){protectedvoidinitChannel(Channelch){ch.pipeline().addLast(newIdleStateHandler(120,0,0),// 2分钟读超时newStringDecoder(),newStringEncoder(),newHeartbeatHandler(),newChatHandler());}}).bind(8080).sync().channel().closeFuture().sync();}finally{boss.shutdownGracefully();worker.shutdownGracefully();}}}五、总结机制配置作用读空闲readerIdleTime检测死连接写空闲writerIdleTime发送心跳保活重连策略指数退避延迟避免重连风暴TCP KeepAlive不依赖间隔太长2小时上一篇【第32篇】Netty背压机制——不让发送方“撑死“接收方下一篇【第34篇】 Netty Selector优化——为什么比JDK NIO快这么多