概述
Netty客户端重连机制
场景:
1.Netty初次启动客户端,如果无法连接到服务端,将尝试重连。
2.在客户端与服务端保持长连接的过程中,如果连接断开,尝试与服务端重连。
-
主要代码
Netty客户端启动处理类
@Service("nettyClient") public class NettyClient { private final static Logger log = LoggerFactory.getLogger(NettyClient.class); private EventLoopGroup loop = new NioEventLoopGroup(); @Autowired private NettyFilter nettyFilter; @Autowired private AppConfig appConfig; public void run(){ doConnect(new Bootstrap(), loop); } /** * netty client 连接,连接失败10秒后重试连接 */ public void doConnect(Bootstrap bootstrap, EventLoopGroup eventLoopGroup) { ChannelFuture f = null; try { if (bootstrap != null) { bootstrap.group(eventLoopGroup); bootstrap.channel(NioSocketChannel.class); bootstrap.option(ChannelOption.SO_KEEPALIVE, true); bootstrap.handler(nettyFilter); bootstrap.remoteAddress(appConfig.getHost(), appConfig.getPort()); f = bootstrap.connect().addListener((ChannelFuture futureListener)->{ final EventLoop eventLoop = futureListener.channel().eventLoop(); if (!futureListener.isSuccess()) { log.warn("客户端已启动,与服务端建立连接失败,10s之后尝试重连!"); eventLoop.schedule(() -> doConnect(new Bootstrap(), eventLoop), 10, TimeUnit.SECONDS); }else { log.info("客户端已启动成功,port:{},开始登录服务端",appConfig.getPort()); // 可以在这发送登录成功的消息给server //futureListener.channel().writeAndFlush(Unpooled.copiedBuffer("msg!!!", CharsetUtil.UTF_8)); } }); } } catch (Exception e) { log.error("连接客户端失败,error:" + e); } } }
客户端与服务端心跳处理类,与客户端断开连接后的重试代码在channelInactive()这个方法中
@Service("idleclienthandler") @ChannelHandler.Sharable public class IdleClientHandler extends SimpleChannelInboundHandler<MessageBase> { private final static Logger logger = LoggerFactory.getLogger(IdleClientHandler.class); private long heartbeatCount = 0; @Autowired private NettyClient nettyClient; @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; logger.info(ctx.channel().remoteAddress() + ",超时类型:" + event.state()); sendPingMsg(ctx); } else { super.userEventTriggered(ctx, evt); } } /** * 发送ping消息 * @param context */ protected void sendPingMsg(ChannelHandlerContext context) { MessageBase body = NettyMessage.MessageBase.newBuilder() .setCmd(CommandType.PING) .setPtcode(Cache.ptcode) .setData("ping") .build(); context.writeAndFlush(body); heartbeatCount++; logger.info("Client sent ping msg to " + context.channel().remoteAddress() + ", count: " + heartbeatCount); } /** * 处理断开重连 */ @SuppressWarnings("static-access") @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { logger.info("与服务器连接断开,尝试重新连接..."); nettyClient.retryConnectFlag = true; final EventLoop eventLoop = ctx.channel().eventLoop(); // 立即重连 nettyClient.doConnect(new Bootstrap(), eventLoop); super.channelInactive(ctx); } @Override protected void channelRead0(ChannelHandlerContext ctx, MessageBase msg) throws Exception { logger.info("收到的消息!{}",msg); } }
最后
以上就是殷勤歌曲为你收集整理的Netty客户端断线重连服务端的全部内容,希望文章能够帮你解决Netty客户端断线重连服务端所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复