博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一起学Netty(十三)之 Netty简单的重连机制
阅读量:4261 次
发布时间:2019-05-26

本文共 2327 字,大约阅读时间需要 7 分钟。

其实重连机制并不是多么多高深的技术,其实就是一个在客户端做一个简单的判断,如果连接断了,那么就重新调用连接服务端的代码

当然,我们重连的动作肯定是发生在断连之后发生的,我们可以在上篇的心跳机制的基础上,简单地修改一下客户端的启动代码就可以了:

我们在连接断了之后,我们一般会在finally的方法中去释放资源,这边我们应该不去释放资源,我们在finally里面进行重连:

整个客户端的代码如下:

[java]   
  1. package com.lyncc.netty.heartbeats;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4.   
  5. import io.netty.bootstrap.Bootstrap;  
  6. import io.netty.channel.ChannelFuture;  
  7. import io.netty.channel.ChannelInitializer;  
  8. import io.netty.channel.ChannelOption;  
  9. import io.netty.channel.ChannelPipeline;  
  10. import io.netty.channel.EventLoopGroup;  
  11. import io.netty.channel.nio.NioEventLoopGroup;  
  12. import io.netty.channel.socket.SocketChannel;  
  13. import io.netty.channel.socket.nio.NioSocketChannel;  
  14. import io.netty.handler.codec.string.StringDecoder;  
  15. import io.netty.handler.codec.string.StringEncoder;  
  16. import io.netty.handler.logging.LogLevel;  
  17. import io.netty.handler.logging.LoggingHandler;  
  18. import io.netty.handler.timeout.IdleStateHandler;  
  19.   
  20. public class HeartBeatsClient {  
  21.   
  22.     public void connect(int port, String host) throws Exception {  
  23.      // Configure the client.  
  24.         EventLoopGroup group = new NioEventLoopGroup();  
  25.         ChannelFuture future = null;  
  26.         try {  
  27.             Bootstrap b = new Bootstrap();  
  28.             b.group(group)  
  29.              .channel(NioSocketChannel.class)  
  30.              .option(ChannelOption.TCP_NODELAY, true)  
  31.              .handler(new LoggingHandler(LogLevel.INFO))  
  32.              .handler(new ChannelInitializer<SocketChannel>() {  
  33.                  @Override  
  34.                  public void initChannel(SocketChannel ch) throws Exception {  
  35.                      ChannelPipeline p = ch.pipeline();  
  36.                      p.addLast("ping"new IdleStateHandler(040, TimeUnit.SECONDS));  
  37.                      p.addLast("decoder"new StringDecoder());  
  38.                      p.addLast("encoder"new StringEncoder());  
  39.                      p.addLast(new HeartBeatClientHandler());  
  40.                  }  
  41.              });  
  42.   
  43.             future = b.connect(host, port).sync();  
  44.             future.channel().closeFuture().sync();  
  45.         } finally {  
  46. //          group.shutdownGracefully();  
  47.           if (null != future) {  
  48.               if (future.channel() != null && future.channel().isOpen()) {  
  49.                   future.channel().close();  
  50.               }  
  51.           }  
  52.           System.out.println("准备重连");  
  53.           connect(port, host);  
  54.           System.out.println("重连成功");  
  55.         }  
  56.     }  
  57.   
  58.     /** 
  59.      * @param args 
  60.      * @throws Exception 
  61.      */  
  62.     public static void main(String[] args) throws Exception {  
  63.         int port = 8080;  
  64.         if (args != null && args.length > 0) {  
  65.             try {  
  66.                 port = Integer.valueOf(args[0]);  
  67.             } catch (NumberFormatException e) {  
  68.                 // 采用默认值  
  69.             }  
  70.         }  
  71.         new HeartBeatsClient().connect(port, "127.0.0.1");  
  72.     }  
  73.   
  74. }  
我们再看看服务器端和客户端启动之后的控制台打印信息:

服务器控制台:

客户端:

好了,这样就可以重连~这只是一个简单的Demo,真实的生产场景用法可能并不是这样的

你可能感兴趣的文章
基于 Flink+Iceberg 构建企业级实时数据湖 | 附 PPT 下载
查看>>
Flink 源码:Checkpoint 元数据详解
查看>>
基于Flink+ClickHouse打造轻量级点击流实时数仓
查看>>
Flink sink schema 字段设计小技巧
查看>>
Flink 使用 union 代替 join 和 cogroup
查看>>
踩坑记 | Flink 天级别窗口中存在的时区问题
查看>>
用了 History Server,妈妈再也不用担心我的 Flink 作业半夜挂了
查看>>
强烈推荐三本 Spark 新书籍
查看>>
ClickHouse 知识讲解
查看>>
ClickHouse 如何玩转时序数据
查看>>
Flink 在腾讯视频的应用实践
查看>>
Flink SQL 1.11 on Zeppelin 平台化实践
查看>>
通过项目逐步深入了解Mybatis<三>
查看>>
奇怪的Java题:为什么128 == 128返回为False,而127 == 127会返回为True?
查看>>
通过项目逐步深入了解Spring MVC(一)
查看>>
java读取文件
查看>>
【二叉树】二叉树打印
查看>>
【字符串】字符串逆序
查看>>
【字符串】判断两字符串是否互为旋转词?
查看>>
【排序】时间复杂度为O(N^2)的排序——冒泡、选择和插入排序
查看>>