Beispiel #1
0
        public ActionResult GetServiceInstances()
        {
            List <ServiceInformation> serviceInformations = new List <ServiceInformation>();
            TTransport transport = new TSocket(Const.SettingService.Local.IpAddress, Const.SettingService.Local.Port, 3000);

            try
            {
                TProtocol           protocol = new TBinaryProtocol(transport);
                BrokerCenter.Client client   = new BrokerCenter.Client(protocol);
                transport.Open();
                var microList = client.GetMicro(string.Empty);
                if (microList != null && microList.Count > 0)
                {
                    foreach (var service in microList)
                    {
                        if (serviceInformations.Any(it => it.Host == service.Ip && it.Port == service.Port))
                        {
                            continue;
                        }
                        ServiceInformation serviceInformation = new ServiceInformation();

                        serviceInformation.Tags = service.Name.Split(new string[] { "," }
                                                                     , StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList();
                        serviceInformation.Host     = service.Ip;
                        serviceInformation.Port     = service.Port;
                        serviceInformation.Timeout  = service.Timeout;
                        serviceInformation.Weight   = service.Weight;
                        serviceInformation.Nickname = service.Nickname;

                        serviceInformations.Add(serviceInformation);
                    }
                }
            }
            finally
            {
                if (transport.IsOpen)
                {
                    transport.Flush();
                    transport.Close();
                }
                transport.Dispose();
            }
            return(new ActionResult(true, serviceInformations));
        }
Beispiel #2
0
        /// <summary>
        /// 健康检查,如果连接不上 每秒做一次尝试。
        /// 尝试 errorCount 次失败,软删除。
        /// 60次失败,永久删除。
        /// </summary>
        /// <param name="service">被检测主机信息</param>
        /// <param name="errorCount">尝试 errorCount 次失败,软删除</param>
        /// <returns></returns>
        public static void HealthCheck(ServiceInfo service, int errorCount = 3)
        {
            int hc = 60; //检查次数

hCheck:                  //再次  心跳检测
            TTransport transport = new TSocket(service.Ip, service.Port, 3000);

            try
            {
                service.Checking = true;
                transport.Open();
                if (transport.IsOpen)
                {
                    if (hc != 60)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: ");
                        Console.WriteLine($"{service.Ip}:{service.Port}");
                        foreach (var f in service.Name.Split(','))
                        {
                            Console.WriteLine($"{f}");
                        }
                        Console.WriteLine($"{"w:" + service.Weight}");
                        Console.WriteLine($"恢复正常!");
                        Console.ResetColor();
                        Console.WriteLine($"----------------------------------------------------------------- ");
                    }
                    if (hc <= (60 - errorCount))
                    {
                        CheckNotice?.Invoke(service, NoticeType.RecoverHealth);
                    }
                    transport.Flush();
                    transport.Close();
                    lock (LockHelper) //防止高并发下 影响权重
                    {
                        if (!Tc.ServiceInfoList.Exists(s => s.Ip == service.Ip && s.Port == service.Port))
                        {
                            //已存在不再添加 不存在则添加
                            for (int i = 0; i < service.Weight; i++)
                            {
                                Tc.ServiceInfoList.Add(service);
                            }
                        }
                    }
                }

                transport.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error Info:{service.Ip}:{service.Port} {ex.Message}");
                if (hc == 60)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: ");
                    Console.WriteLine($"{service.Ip}:{service.Port}");
                    foreach (var f in service.Name.Split(','))
                    {
                        Console.WriteLine($"{f}");
                    }
                    Console.WriteLine($"{"w:" + service.Weight}");
                    Console.WriteLine($"检测中···{hc}!");
                    Console.ResetColor();
                    Console.WriteLine($"----------------------------------------------------------------- ");
                }
                else if (hc == (60 - errorCount))
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: ");
                    Console.WriteLine($"{service.Ip}:{service.Port}");
                    foreach (var f in service.Name.Split(','))
                    {
                        Console.WriteLine($"{f}");
                    }
                    Console.WriteLine($"{"w:" + service.Weight}");
                    Console.WriteLine($"故障恢复中···{hc}!");
                    Console.ResetColor();
                    Console.WriteLine($"----------------------------------------------------------------- ");
                }
                else if (hc == 0) //硬删除
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}:");
                    Console.WriteLine($"{service.Ip}:{service.Port}");
                    foreach (var f in service.Name.Split(','))
                    {
                        Console.WriteLine($"{f}");
                    }
                    Console.WriteLine($"{"w:" + service.Weight}");
                    Console.WriteLine($"永久移除!");
                    Console.ResetColor();
                    Console.WriteLine($"----------------------------------------------------------------- ");
                    CheckNotice?.Invoke(service, NoticeType.OffLine);
                }

                if (hc == (60 - errorCount)) //三次失败之后 临时移除 ,防止更多请求转发给此服务节点
                {
                    //临时移除 并不从配置文件移除
                    Tc.ServiceInfoList.RemoveAll(i => i.Ip == service.Ip && i.Port == service.Port);
                    CheckNotice?.Invoke(service, NoticeType.NotHealth);
                }
                else if (hc == 0) //硬删除
                {
                    Dictionary <string, string> rp = new Dictionary <string, string>
                    {
                        { "ip", service.Ip },
                        { "port", service.Port.ToString() }
                    };
                    Tc.Remove(rp);
                    return;
                }

                Thread.Sleep(1000); //间隔一秒 健康检查
                hc--;
                transport.Dispose();
                goto hCheck;
            }
            finally
            {
                if (transport.IsOpen)
                {
                    transport.Flush();
                    transport.Close();
                }
                transport.Dispose();
                service.Checking = false;
            }
        }