Exemplo n.º 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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更新服务缓存
        /// </summary>
        /// <param name="channel">管道</param>
        internal static void UpdateCache(string channel)
        {
            #region 到DNS中心取服务信息
            try
            {
                if (channel.Equals("cron:"))
                {
                    RefreshServiceMd5();
                    channel = ServiceMd5;
                }
                if (!_proxyCenter.IsOpen)
                {
                    _proxyCenter.Open();
                }
                DateTime now       = DateTime.Now; //获取缓存时间
                var      microList = _client.GetMicro(channel);
                #region Micro +添加到缓存

                if (microList != null && microList.Count > 0)
                {
                    var microCaches = new List <MicroCache>();
                    microList.ForEach(m =>
                    {
                        microCaches.Add(new MicroCache()
                        {
                            LasTime = now,
                            Mi      = m,
                            Tags    = m.Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList()
                        });
                    });
                    _microCaches = microCaches;

                    #region  步服务到连接池
                    var scs = new List <ServiceConfig>();
                    _microCaches.ForEach(mc =>
                    {
                        if (!scs.Exists(s => s.Host == mc.Mi.Ip && s.Port == mc.Mi.Port))
                        {
                            scs.Add(new ServiceConfig()
                            {
                                Host    = mc.Mi.Ip,
                                Port    = mc.Mi.Port,
                                Timeout = mc.Mi.Timeout
                            });
                        }
                    });
                    ThriftFactory.Synchronization(scs);

                    #endregion
                }
                else
                {
                    _microCaches.Clear();
                    ThriftFactory.Synchronization(new List <ServiceConfig>());
                }

                #endregion
            }
            catch (Exception ex)
            {
                try
                {
                    if (connectionCenterInit == false)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + $":注册中心 {SettingService.Local.IpAddress}:{SettingService.Local.Port} " + ex.Message);
                        Console.ResetColor();
                        connectionCenterInit = true;
                    }
                    // return (null, FailMessage($"负载中心连接失败!"));
                    if (_proxyCenter.IsOpen)
                    {
                        _proxyCenter.Flush();
                        _proxyCenter.Close();
                    }

                    _proxyCenter.Dispose();

                    _proxyCenter = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000);
                    _protocol    = new TBinaryProtocol(_proxyCenter);
                    _client      = new BrokerCenter.Client(_protocol);
                }
                catch
                {
                    // ignored
                }
            }
            #endregion
        }
Exemplo n.º 3
0
        /// <summary>
        /// 更新服务缓存
        /// </summary>
        /// <param name="channel">管道</param>
        internal static void UpdateCache(string channel)
        {
            #region 到DNS中心取服务信息
            try
            {
                if (channel.Equals("cron:"))
                {
                    RefreshServiceMd5();
                    channel = ServiceMd5;
                }
                List <Micro> microList = null;
                DateTime     now       = DateTime.Now; //获取缓存时间
                using (var trans = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000))
                    using (var protocol = new TBinaryProtocol(trans))
                        using (var client = new BrokerCenter.Client(protocol))
                        {
                            trans.Open();
                            microList = client.GetMicro(channel);
                            trans.Close();
                            trans.Dispose();
                            protocol.Dispose();
                        }
                #region Micro +添加到缓存

                if (microList != null && microList.Count > 0)
                {
                    var microCaches = new List <MicroCache>();
                    microList.ForEach(m =>
                    {
                        microCaches.Add(new MicroCache()
                        {
                            LasTime = now,
                            Mi      = m,
                            Tags    = m.Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList()
                        });
                    });
                    _microCaches = microCaches;

                    #region  步服务到连接池
                    var scs = new List <ServiceConfig>();
                    _microCaches.ForEach(mc =>
                    {
                        if (!scs.Exists(s => s.Host == mc.Mi.Ip && s.Port == mc.Mi.Port))
                        {
                            scs.Add(new ServiceConfig()
                            {
                                Host    = mc.Mi.Ip,
                                Port    = mc.Mi.Port,
                                Timeout = mc.Mi.Timeout
                            });
                        }
                    });
                    ThriftFactory.Synchronization(scs);

                    #endregion
                }
                else
                {
                    _microCaches.Clear();
                    ThriftFactory.Synchronization(new List <ServiceConfig>());
                }

                #endregion
            }
            catch (Exception ex)
            {
                Log.Log.Anno($"注册中心 {SettingService.Local.IpAddress}:{SettingService.Local.Port} " + ex.Message);
            }
            #endregion
        }