http://www.spring4all.com/article/889

本篇文章是Netty专题的第七篇,前面六篇文章如下:

用Netty实现长连接服务,当发生下面的情况时,会发生断线的情况。

  • 网络问题
  • 客户端启动时服务端挂掉了,连接不上服务端
  • 客户端已经连接服务端,服务端突然挂掉了
  • 其它问题等...

##如何解决上面的问题?

1.心跳机制检测连接存活

长连接是指建立的连接长期保持,不管有无数据包的发送都要保持连接通畅。心跳是用来检测一个系统是否存活或者网络链路是否通畅的一种方式,一般的做法是客户端定时向服务端发送心跳包,服务端收到心跳包后进行回复,客户端收到回复说明服务端存活。

通过心跳检测机制,可以检测客户端与服务的长连接是否保持,当客户端发送的心跳包没有收到服务端的响应式,可以认为服务端已经出故障了,这个时候可以重新连接或者选择其他的可用的服务进行连接。

在Netty中提供了一个IdleStateHandler类用于心跳检测,用法如下:

ch.pipeline().addLast("ping", new IdleStateHandler(60, 20, 60 * 10, TimeUnit.SECONDS));
  • 第一个参数 60 表示读操作空闲时间
  • 第二个参数 20 表示写操作空闲时间
  • 第三个参数 60*10 表示读写操作空闲时间
  • 第四个参数 单位/秒

在处理数据的ClientPoHandlerProto中增加userEventTriggered用来接收心跳检测结果,event.state()的状态分别对应上面三个参数的时间设置,当满足某个时间的条件时会触发事件。

public class ClientPoHandlerProto extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection(); @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
MessageProto.Message message = (MessageProto.Message) msg;
System.out.println("client:" + message.getContent());
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
} @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
super.userEventTriggered(ctx, evt);
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state().equals(IdleState.READER_IDLE)) {
System.out.println("长期没收到服务器推送数据");
//可以选择重新连接
} else if (event.state().equals(IdleState.WRITER_IDLE)) {
System.out.println("长期未向服务器发送数据");
//发送心跳包
ctx.writeAndFlush(MessageProto.Message.newBuilder().setType(1));
} else if (event.state().equals(IdleState.ALL_IDLE)) {
System.out.println("ALL");
}
}
}
}

服务端收到客户端发送的心跳消息后,回复一条信息

public class ServerPoHandlerProto extends ChannelInboundHandlerAdapter {

    @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
MessageProto.Message message = (MessageProto.Message) msg;
if (ConnectionPool.getChannel(message.getId()) == null) {
ConnectionPool.putChannel(message.getId(), ctx);
}
System.err.println("server:" + message.getId());
// ping
if (message.getType() == 1) {
ctx.writeAndFlush(message);
} } @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}

当客户端20秒没往服务端发送过数据,就会触发IdleState.WRITER_IDLE事件,这个时候我们就像服务端发送一条心跳数据,跟业务无关,只是心跳。服务端收到心跳之后就会回复一条消息,表示已经收到了心跳的消息,只要收到了服务端回复的消息,那么就不会触发IdleState.READER_IDLE事件,如果触发了IdleState.READER_IDLE事件就说明服务端没有给客户端响应,这个时候可以选择重新连接。

2.启动时连接重试

在Netty中实现重连的操作比较简单,Netty已经封装好了,我们只需要稍微扩展一下即可。

连接的操作是客户端这边执行的,重连的逻辑也得加在客户端,首先我们来看启动时要是连接不上怎么去重试

增加一个负责重试逻辑的监听器,代码如下:

import java.util.concurrent.TimeUnit;

import com.netty.im.client.ImClientApp;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
/**
* 负责监听启动时连接失败,重新连接功能
* @author yinjihuan
*
*/
public class ConnectionListener implements ChannelFutureListener { private ImConnection imConnection = new ImConnection(); @Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (!channelFuture.isSuccess()) {
final EventLoop loop = channelFuture.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
System.err.println("服务端链接不上,开始重连操作...");
imConnection.connect(ImClientApp.HOST, ImClientApp.PORT);
}
}, 1L, TimeUnit.SECONDS);
} else {
System.err.println("服务端链接成功...");
}
}
}

通过channelFuture.isSuccess()可以知道在连接的时候是成功了还是失败了,如果失败了我们就启动一个单独的线程来执行重新连接的操作。

只需要在ConnectionListener添加到ChannelFuture中去即可使用

public class ImConnection {

    private Channel channel;

    public Channel connect(String host, int port) {
doConnect(host, port);
return this.channel;
} private void doConnect(String host, int port) {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception { // 实体类传输数据,protobuf序列化
ch.pipeline().addLast("decoder",
new ProtobufDecoder(MessageProto.Message.getDefaultInstance()));
ch.pipeline().addLast("encoder",
new ProtobufEncoder());
ch.pipeline().addLast(new ClientPoHandlerProto()); }
}); ChannelFuture f = b.connect(host, port);
f.addListener(new ConnectionListener());
channel = f.channel();
} catch(Exception e) {
e.printStackTrace();
}
} }

可以按照如下步骤进行测试:

  • 直接启动客户端,不启动服务端
  • 当连接失败的时候会进入ConnectionListener中的operationComplete方法执行我们的重连逻辑

3.运行中连接断开时重试

使用的过程中服务端突然挂了,就得用另一种方式来重连了,可以在处理数据的Handler中进行处理。

public class ClientPoHandlerProto extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection(); @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
MessageProto.Message message = (MessageProto.Message) msg;
System.out.println("client:" + message.getContent());
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
} @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("掉线了...");
//使用过程中断线重连
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
imConnection.connect(ImClientApp.HOST, ImClientApp.PORT);
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
} }

在连接断开时都会触发 channelInactive 方法, 处理重连的逻辑跟上面的一样。

可以按照如下步骤进行测试:

  • 启动服务端
  • 启动客户端,连接成功
  • 停掉服务端就会触发channelInactive进行重连操作

源码参考:https://github.com/yinjihuan/netty-im

最新文章

  1. 如何完全卸载OneDrive (Windows 10 64bit)
  2. padding
  3. 物理引擎-Physx的源代码去哪里找
  4. Java设计模式(三) 抽象工厂模式
  5. Maven提高篇系列之(三)——使用自己的Repository(Nexus)
  6. 微信公众平台开发(三) 订阅事件(subscribe)处理
  7. 种类并查集(POJ 1703)
  8. wdcp升级php版本到5.3,5.5
  9. 实现android activity之间的跳转
  10. 用C++试着完成Python简明教程后面的练习
  11. [RxJS] Transformation operator: buffer, bufferCount, bufferTime
  12. NHibernate从入门到精通系列(3)——第一个NHibernate应用程序
  13. 如何用代码禁用SpriteBuilder中创建的关节
  14. 如何搭建一个 MySQL 分布式集群
  15. java环境变量---找不到或无法加载主类
  16. 调css时候,设置margin-left没有效果
  17. ExportGrid Aspose.Cells.dll
  18. 使用mapreduce来分析网站的log日志
  19. google云使用记录
  20. postgresql----数组类型和函数

热门文章

  1. Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》
  2. Springmvc ajax请求400
  3. php switch
  4. Jmeter如何监控服务器性能
  5. leetcode744
  6. mysql彻底删除
  7. 【Django】uWSGI和Gunicorn【转】
  8. 在Spring中使用Redis Lua脚本批量删除缓存
  9. ubuntu linux常用指令(1)
  10. Python实现七牛云视频播放