/// <summary> /// 获取指定服务状态 /// </summary> /// <param name="serviceName">服务名</param> /// <param name="currentZone">分片键</param> /// <param name="masterOrSlaver">主从身份</param> /// <returns></returns> public async Task <CurrentServiceStatus> GetCurrentServiceStatus(string serviceName, string currentZone, MasterOrSlaver masterOrSlaver) { var queryResult = await _consul.Health.Service(serviceName, currentZone, false); var tarService = queryResult.Response.Where(serviceEntry => serviceEntry.Service != null && serviceEntry.Service.Tags.Contains(masterOrSlaver.ToString())).FirstOrDefault(); CurrentServiceStatus status = CurrentServiceStatus.Unsetting; if (tarService != null && tarService.Checks.Any(serviceCheck => !string.IsNullOrWhiteSpace(serviceCheck.ServiceID))) { var serviceStatus = tarService.Checks.Where(serviceCheck => !string.IsNullOrWhiteSpace(serviceCheck.ServiceID)).FirstOrDefault(); if (serviceStatus != null) { status = serviceStatus.Status == HealthStatus.Passing ? CurrentServiceStatus.Running : CurrentServiceStatus.Critical; } } return(status); }
private int _currentFailedTimes = 0; //检查到失败次数 private void SlaverProcessOn() { while (true) { try { CurrentServiceStatus masterServiceStatus = _consulServiceSwitchProvider.GetCurrentServiceStatus(_serviceName, _currentZone, MasterOrSlaver.Master).ConfigureAwait(false).GetAwaiter().GetResult(); if (masterServiceStatus != CurrentServiceStatus.Running) { if (!_currentMasterisMasterDown) { if (masterServiceStatus == CurrentServiceStatus.Unsetting) { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] a master instance has not been set up firstly, current slaver instance begins to run now", LogLevel.Warn); _currentFailedTimes = _changeTimes; } _currentFailedTimes = _currentFailedTimes + 1; if (_currentFailedTimes <= _changeTimes) { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] the master instance is in critical status,slaver tries {_currentFailedTimes} times ,after {_changeTimes} times ,slaver instance will start", LogLevel.Info); Thread.Sleep(_checkInsteval); continue; } LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] begin to update current running status", LogLevel.Debug); _consulServiceSwitchProvider.SetCurrentRunningServiceId(_serviceName, _currentZone, _serviceId).ConfigureAwait(false).GetAwaiter().GetResult(); LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] current running status updated complete", LogLevel.Debug); _currentFailedTimes = 0; _currentMasterisMasterDown = true;//master down Task.Run(() => { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] slaver instance begin to run the start event", LogLevel.Info); Run(); LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] the start event has done", LogLevel.Info); }); } else { if (masterServiceStatus == CurrentServiceStatus.Unsetting) { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] master instance is still unseting now", LogLevel.Warn); } if (masterServiceStatus == CurrentServiceStatus.Critical) { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] master instance is still in critical status", LogLevel.Trace); } } } else { if (_currentMasterisMasterDown) { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] master instance alives again ,begin to update current running status", LogLevel.Info); LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] begin to update current running status", LogLevel.Debug); string currentRunningId = _consulServiceSwitchProvider.GetCurrentRunningServiceId(_serviceName, _serviceId).ConfigureAwait(false).GetAwaiter().GetResult(); if (currentRunningId == _serviceId) { _consulServiceSwitchProvider.SetCurrentRunningServiceId(_serviceName, _currentZone, "").ConfigureAwait(false).GetAwaiter().GetResult(); } LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] current running status updated complete", LogLevel.Debug); _currentMasterisMasterDown = false; _currentFailedTimes = 0; End(); } else { LogMsg($"[ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] master instance is still in passing status", LogLevel.Trace); _currentFailedTimes = 0; } } } catch (Exception ex) { LogMsg(ex.ToString(), LogLevel.Warn); _currentFailedTimes = 0; //_currentMasterisMasterDown = false; } Thread.Sleep(_checkInsteval); } }