Netty简单的重连机制

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

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

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

整个客户端的代码如下:

[java] view plain copy
 
  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(0, 4, 0, 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,真实的生产场景用法可能并不是这样

原文地址:https://www.cnblogs.com/duan2/p/8919532.html