/// <summary> /// 探针发送ping 心跳包 /// </summary> public static bool TestPingSendMessage() { bool isServerHelthOK = false; try { //1 首先检查蜘蛛进程是否运行 如果全部蜘蛛进程都挂掉 那么立刻返回false var isCrawlerRun = ShoppingWebCrawlerHostService.IsWebCrawlerHostProcessRunnning; if (false == isCrawlerRun) { return(isServerHelthOK); } // 2 如果已经有了蜘蛛进程,那么监视主进程是否处于激活 using (var conn = new SoapTcpConnection(IPAddress, Port, 5)) { var pingResult = conn.Ping(); if (pingResult == true) { Logger.Info("探针检测服务端返回正确:pong ."); } isServerHelthOK = pingResult; } } catch (Exception ex) { Logger.Error(ex); } return(isServerHelthOK); }
/// <summary> /// 向服务端发送消息,注册登记 /// 并分配端口 /// </summary> /// <param name="cmd"></param> /// <param name="data"></param> public static int RegisterSlaveToMaster(string slaveIdentity) { var result = -1; using (var conn = new SoapTcpConnection("127.0.0.1", GlobalContext.MasterSocketPort)) { if (conn.State == ConnectionState.Closed) { conn.Open(); } if (conn.Ping() == false) { return(result); } //发送soap var paras = new RegisterPortArgument { SlaveIdentity = slaveIdentity }; string msg = JsonConvert.SerializeObject(paras); SoapMessage sopMsg = new SoapMessage() { Head = CommandConstants.CMD_RegisterSlavePort, Body = msg }; var repResult = conn.SendSoapMessage(sopMsg); if (repResult.Status == 1) { result = repResult.Result.ToInt(); } } return(result); }
/// <summary> /// 自身健康监测 /// </summary> /// <param name="callbackHandlerOnFailed">失败的时候 执行的委托回调</param> public void BeginSelfHelthCheck(Action <string> callbackHandlerOnFailed) { if (isRunningHelthCheck == true) { return; } //1 向此节点对应的端口 发送ping //2 失败3次 定性为错误无效节点 Task.Factory.StartNew(() => { while (true) { RunningLocker.CreateNewLock().CancelAfter(1000); try { bool isBeUsed = SocketHelper.IsUsedIPEndPoint(this.Port); if (isBeUsed == false) { if (counterStatusCheck >= 10) { this.IsActiveNode = false; if (null != callbackHandlerOnFailed) { callbackHandlerOnFailed(this.Identity); } break; } else { counterStatusCheck += 1; continue;//端口暂未开启 } } using (var conn = new SoapTcpConnection(this.IpAddress, this.Port, 4)) { if (conn.State != ConnectionState.Open) { conn.Open(); } var result = conn.Ping();//发送Ping 命令,是否可以ping 通子节点端口 if (result != true) { //一旦超过3此ping 不通,表示这是一个坏节点,中集群中心移除此节点--进行回调通知 if (this.failCounter >= 2) { this.IsActiveNode = false; if (null != callbackHandlerOnFailed) { callbackHandlerOnFailed(this.Identity); } break; } this.failCounter += 1; } } } catch (Exception ex) { this.IsActiveNode = false; Common.Logging.Logger.Error(ex); break; } } }); }