栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Dubbo心跳逻辑处理

Dubbo心跳逻辑处理

Dubbo默认客户端和服务器端都会发送心跳报文,用来保持TCP长连接状态。在客户端和服务端,Dubbo内部开启一个线程循环扫描并检测连接是否超时,在服务端如果发现超时则会主动关闭客户端连接,在客户端发现超时则会主动重新创建连接。默认心跳检测60s,具体应用可以通过heartbeat配置

Dubbo在服务端和客户端都复用心跳实现代码,抽象成HeartBeatTask任务进行处理

@Override
public void run(){
  try{
    long now = Systtem.currentTimeMillis();
    //遍历所有的channel
    for(Channel channel : channelProvider.getChannels()){
      //忽略掉关闭的channel
      if(channel.isClosed()){
        continue;
      }
      try{
        long lastRead = (Long)channel.getAttribute(HeadeeExchangeHandler.KEY_READ_TIMESTAMP);
        long lastWrite = (Long)channel.getAttribute(HeaderExchangeHandler.KEY_WRITE_TIMESTAMP);
        if((lastRead != null && now - lastRead > heartBeat)||(lastWrite != null && now - lastWrite > heartBeat)){
          Request req = new Request();
          req.setVersion(Version.getProtocolVersion());
          req.setTwoWay(true);
          req.setEvent(Request.HEARTBEAT_EVENT);
          channel.send(req);
          if(logger.isDubugEnabled()){
            logger.debug("aaaaxxx");
          }
        }
        if(lastRead != null && now - lastRead > heartbeatTimeout){
          logger.warn("Close channel"+channel);
          if(channel instanceof Client){
            try{
              //客户端空闲超时触发重连,默认超市3分钟
              ((Client)channel).reconnect();
            }catch(Exception e){}
          }else{
            //服务端关闭连接
            channel.close();
          }
        }
      }
    }
  }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号