Пример #1
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="target">注册目标</param>
        /// <param name="countDown">注册超时次数</param>
        /// <returns></returns>
        public bool ToCenter(Target target, int countDown = 10)
        {
begin:
            try
            {
                _channel = new Channel($"{target.IpAddress}:{target.Port}", ChannelCredentials.Insecure);
                _client  = new BrokerCenter.BrokerCenterClient(_channel);
                Micro micro = new Micro();
                micro.Timeout = (int)SettingService.TimeOut;
                micro.Name    = SettingService.FuncName;
                micro.Ip      = SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress;

                micro.Port     = SettingService.Local.Port;
                micro.Weight   = SettingService.Weight;
                micro.Nickname = SettingService.AppName;
                bool rlt = (_client.Add_broker(micro).Reply == "1");
                if (rlt)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine($"{DateTime.Now}");
                    Console.WriteLine($"本机【{SettingService.AppName}】:");
                    foreach (var ip in micro.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
                    {
                        _client = new BrokerCenter.BrokerCenterClient(_channel);
                    }
                    catch
                    {
                        //忽略异常
                    }
                    --countDown;
                    goto begin;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......");
                    Console.ResetColor();
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="target">注册目标</param>
        /// <param name="countDown">注册超时次数</param>
        /// <returns></returns>
        public bool ToCenter(Target target, int countDown = 10)
        {
begin:
            try
            {
                _channel = new Channel($"{target.IpAddress}:{target.Port}", ChannelCredentials.Insecure);
                _client  = new BrokerCenter.BrokerCenterClient(_channel);
                Micro micro = new Micro();
                micro.Timeout = (int)SettingService.TimeOut;
                micro.Name    = SettingService.FuncName;
                micro.Ip      = SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress;

                micro.Port     = SettingService.Local.Port;
                micro.Weight   = SettingService.Weight;
                micro.Nickname = SettingService.AppName;
                bool rlt = (_client.Add_broker(micro).Reply == "1");
                if (rlt)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine($"本机【{SettingService.AppName}】:");
                    foreach (var ip in micro.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
                    {
                        _client = new BrokerCenter.BrokerCenterClient(_channel);
                    }
                    catch
                    {
                        //忽略异常
                    }
                    --countDown;
                    goto begin;
                }
                else
                {
                    Log.Anno($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......", typeof(Register));
                }
            }
            return(true);
        }