/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _transport = new TSocket(target.IpAddress, target.Port, 3000); TProtocol protocol = new TBinaryProtocol(_transport); _client = new BrokerCenter.Client(protocol); if (!_transport.IsOpen) { _transport.Open(); } Dictionary <string, string> info = new Dictionary <string, string> { { "timeout", SettingService.TimeOut.ToString() }, { "name", SettingService.FuncName }, { "ip", SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress }, { "port", SettingService.Local.Port.ToString() }, { "weight", SettingService.Weight.ToString() }, { "nickname", SettingService.AppName } }; bool rlt = _client.add_broker(info); _transport.Close(); if (rlt) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now}"); Console.WriteLine($"本机【{SettingService.AppName}】:"); foreach (var ip in info["ip"].Split(',')) { Console.WriteLine($"{ip}"); } Console.WriteLine($"已注册到:{target.IpAddress}"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); } return(rlt); } catch (Exception ex) { Thread.Sleep(1000);//间隔一秒后重新注册 if (countDown > 0) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"{DateTime.Now} 注册到{target.IpAddress}:{target.Port}失败......剩余重试次数({countDown})"); Console.WriteLine($"错误信息:{ex.Message}"); Console.ResetColor(); try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } --countDown; goto begin; } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......"); Console.ResetColor(); } } finally { try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } } return(true); }
/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _transport = new TSocket(target.IpAddress, target.Port, 3000); TProtocol protocol = new TBinaryProtocol(_transport); _client = new BrokerCenter.Client(protocol); if (!_transport.IsOpen) { _transport.Open(); } Dictionary <string, string> info = new Dictionary <string, string> { { "timeout", SettingService.TimeOut.ToString() }, { "name", SettingService.FuncName }, { "ip", SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress }, { "port", SettingService.Local.Port.ToString() }, { "weight", SettingService.Weight.ToString() }, { "nickname", SettingService.AppName } }; bool rlt = _client.add_broker(info); _transport.Close(); if (rlt) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"本机【{SettingService.AppName}】:"); foreach (var ip in info["ip"].Split(',')) { stringBuilder.AppendLine($"{ip}"); } stringBuilder.AppendLine($"已注册到:{target.IpAddress}"); Log.Anno(stringBuilder.ToString(), typeof(Register)); Log.WriteLine($"已注册到:{target.IpAddress}"); } return(rlt); } catch (Exception ex) { Task.Delay(1000).Wait();//间隔一秒后重新注册 if (countDown > 0) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"注册到{target.IpAddress}:{target.Port}失败......剩余重试次数({countDown})"); stringBuilder.AppendLine(ex.Message); Log.Anno(stringBuilder.ToString(), typeof(Register)); try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } --countDown; goto begin; } else { Log.Anno($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......", typeof(Register)); } } finally { try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } } return(true); }