Exemplo n.º 1
0
        /// <summary>
        /// 加载节点
        /// </summary>
        /// <param name="input">"name=dc1,ip=192.168.X.X,port=6659,timeout=3000,weight=5"</param>
        public bool Add(Dictionary <string, string> input)
        {
            lock (LockAdd)
            {
                try
                {
                    var ip = GetValidIp(input["ip"], Convert.ToInt32(input["port"]));
                    if (ip == string.Empty)
                    {
                        return(false);
                    }
                    ServiceInfo ips = new ServiceInfo
                    {
                        Timeout  = input["timeout"] == null ? TimeOut : Convert.ToInt32(input["timeout"]),
                        Name     = input["name"],
                        NickName = input["nickname"],
                        Ip       = ip,
                        Port     = Convert.ToInt32(input["port"])
                    };
                    int weight = input["weight"] == null ? 1 : (int)Convert.ToDecimal(input["weight"]);
                    ips.Weight = weight;
                    #region 原有服务
                    var oldService = ServiceInfoList.FirstOrDefault(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    #endregion

                    ServiceInfoList.RemoveAll(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    for (int w = 0; w < weight; w++) //权重
                    {
                        ServiceInfoList.Add(ips);
                    }

                    Log.WriteLine($"{ips.Ip}:{ips.Port}", ConsoleColor.DarkGreen);
                    ips.Name.Split(',').ToList().ForEach(f =>
                    {
                        Log.WriteLine($"{f}", ConsoleColor.DarkGreen);
                    });
                    Log.WriteLine($"{"权重:" + ips.Weight}", ConsoleColor.DarkGreen);
                    Log.WriteLine($"{ips.NickName}已登记!", ConsoleColor.DarkGreen);
                    Log.WriteLineNoDate($"-----------------------------------------------------------------------------");
                    #region  线和变更通知
                    if (OnlineNotice != null && oldService == null)
                    {
                        OnlineNotice.Invoke(ips, NoticeType.OnLine);
                    }
                    else if (ChangeNotice != null && oldService != null)
                    {
                        ChangeNotice.Invoke(ips, oldService);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return(false);
                }
                finally
                {
                    Save();
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 加载节点
        /// </summary>
        /// <param name="input">"name=dc1,ip=192.168.X.X,port=6659,timeout=3000,weight=5"</param>
        public bool Add(Dictionary <string, string> input)
        {
            lock (LockAdd)
            {
                try
                {
                    var ip = GetValidIp(input["ip"], Convert.ToInt32(input["port"]));
                    if (ip == string.Empty)
                    {
                        return(false);
                    }
                    ServiceInfo ips = new ServiceInfo
                    {
                        Timeout  = input["timeout"] == null ? TimeOut : Convert.ToInt32(input["timeout"]),
                        Name     = input["name"],
                        NickName = input["nickname"],
                        Ip       = ip,
                        Port     = Convert.ToInt32(input["port"])
                    };
                    int weight = input["weight"] == null ? 1 : (int)Convert.ToDecimal(input["weight"]);
                    ips.Weight = weight;
                    #region 原有服务
                    var oldService = ServiceInfoList.FirstOrDefault(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    #endregion

                    ServiceInfoList.RemoveAll(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    for (int w = 0; w < weight; w++) //权重
                    {
                        ServiceInfoList.Add(ips);
                    }

                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine($"{ips.Ip}:{ips.Port}");
                    foreach (var f in ips.Name.Split(','))
                    {
                        stringBuilder.AppendLine($"{f}");
                    }
                    stringBuilder.AppendLine($"{"权重:" + ips.Weight}");
                    stringBuilder.AppendLine($"{ips.NickName}已登记!");
                    Log.Anno(stringBuilder.ToString(), typeof(ThriftConfig));

                    #region  线和变更通知
                    if (OnlineNotice != null && oldService == null)
                    {
                        OnlineNotice.Invoke(ips, NoticeType.OnLine);
                    }
                    else if (ChangeNotice != null && oldService != null)
                    {
                        ChangeNotice.Invoke(ips, oldService);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    Log.Anno(ex, typeof(ThriftConfig));
                    return(false);
                }
                finally
                {
                    Save();
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 加载节点
        /// </summary>
        /// <param name="input">"name=dc1,ip=192.168.X.X,port=6659,timeout=3000,weight=5"</param>
        public bool Add(Micro input)
        {
            lock (LockAdd)
            {
                try
                {
                    var ip = GetValidIp(input.Ip, input.Port);
                    if (ip == string.Empty)
                    {
                        return(false);
                    }
                    ServiceInfo ips = new ServiceInfo
                    {
                        Timeout  = input.Timeout,
                        Name     = input.Name,
                        NickName = input.Nickname,
                        Ip       = ip,
                        Port     = input.Port
                    };
                    int weight = input.Weight <= 0 ? 1 : input.Weight;
                    ips.Weight = weight;
                    #region  线和变更通知
                    var oldService = ServiceInfoList.FirstOrDefault(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    if (OnlineNotice != null && oldService == null)
                    {
                        OnlineNotice.Invoke(ips, NoticeType.OnLine);
                    }
                    else if (ChangeNotice != null && oldService != null)
                    {
                        ChangeNotice.Invoke(ips, oldService);
                    }
                    #endregion
                    ServiceInfoList.RemoveAll(t => ips.Ip == t.Ip && ips.Port == t.Port);
                    for (int w = 0; w < weight; w++) //权重
                    {
                        ServiceInfoList.Add(ips);
                    }

                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
                    Console.WriteLine($"{ips.Ip}:{ips.Port}");
                    ips.Name.Split(',').ToList().ForEach(f =>
                    {
                        Console.WriteLine($"{f}");
                    });
                    Console.WriteLine($"{"w:" + ips.Weight}");
                    Console.WriteLine($"{ips.NickName}已登记!");
                    Console.ResetColor();
                    Console.WriteLine($"----------------------------------------------------------------- ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return(false);
                }
                finally
                {
                    Save();
                }
            }
            return(true);
        }