Example #1
0
 private void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             try
             {
                 ThriftFactory.ReturnInstance(transport, id);
             }
             catch (Exception)
             {
                 //var x = ex;
             }
         }
         disposed = true;
     }
 }
Example #2
0
        /// <summary>
        /// 处理器
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string Invoke(Dictionary <string, string> input)
        {
            string output;
            //失败计数器 N次 就直接返回异常
            int error = 0;

reStart:
            try
            {
                if (!transport.Transport.IsOpen)
                {
                    transport.Transport.Open();
                }

                output = transport.Client.broker(input); //如果连接不可用,会报IO异常(重试)
            }
            //catch (TTransportException)
            //{
            //    /*
            //     * 连接打开出错
            //     */
            //    ThriftFactory.RemoveServicePool(id);
            //    throw;
            //}
            catch (AggregateException)
            {
                ThriftFactory.RemoveServicePool(id);
                throw;
            }
            catch (Exception ex)
            {
                if (ex is IOException)//连接不可用的时候 直接销毁 从新从连接池拿
                {
                    var sEx = (SocketException)ex.InnerException;
                    if (sEx?.SocketErrorCode == SocketError.TimedOut)
                    {
                        if (transport.Transport.IsOpen)
                        {
                            transport.Transport.Close();
                        }
                        return(Connector.FailMessage(SocketError.TimedOut.ToString()));
                    }
                    if (sEx?.SocketErrorCode == SocketError.ConnectionReset || sEx?.SocketErrorCode == SocketError.Shutdown)
                    {
                        if (error == 100) //消耗完 线程池里面的 失效连接(此值 只是一个参考)
                        {
                            return(Connector.FailMessage(ex.Message));
                        }

                        if (transport.Transport.IsOpen)
                        {
                            transport.Transport.Close();
                        }
                        ThriftFactory.ReturnInstance(transport, id); //归还有问题链接

                        transport = ThriftFactory.BorrowInstance(id);
                        error++;
                        goto reStart;
                    }
                }
                output = Connector.FailMessage(ex.Message);
            }
            return(output);
        }
Example #3
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
        }
Example #4
0
 public Request(string host, int port)
 {
     this.id   = $"{host}:{port}";
     disposed  = false;
     transport = ThriftFactory.BorrowInstance(id);
 }
Example #5
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
        }