Exemplo n.º 1
0
 private void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             try
             {
                 GrpcFactory.ReturnInstance(transport, id);
             }
             catch (Exception)
             {
                 //var x = ex;
             }
         }
         disposed = true;
     }
 }
Exemplo n.º 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
            {
                BrokerRequest brokerRequest = new BrokerRequest();
                brokerRequest.Input.Add(input);
                output = transport.Client.broker(request: brokerRequest, options: transport.TimeOut.GetCallOptions()).Reply; //如果连接不可用,会报IO异常(重试)
            }
            catch (Exception ex)
            {
                if (ex is RpcException)//连接不可用的时候 直接销毁 从新从连接池拿
                {
                    var sEx = (RpcException)ex;
                    if (sEx.StatusCode == StatusCode.Unavailable || sEx.StatusCode == StatusCode.Aborted)
                    {
                        error++;
                        if (error == 100) //累计3 拿不到有效连接 抛出异常 移除(此值 只是一个参考)
                        {
                            GrpcFactory.RemoveServicePool(id);
                            throw sEx;
                        }

                        GrpcFactory.ReturnInstance(transport, id); //归还有问题链接

                        transport = GrpcFactory.BorrowInstance(id);
                        goto reStart;
                    }
                    else if (sEx.StatusCode == StatusCode.DeadlineExceeded)
                    {
                        GrpcFactory.ReturnInstance(transport, id); //归还有问题链接
                    }
                }
                throw ex;
            }
            return(output);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 更新服务缓存
        /// </summary>
        internal static void UpdateCache(string channel)
        {
            #region 到DNS中心取服务信息
            try
            {
                if (channel.Equals("cron:"))
                {
                    RefreshServiceMd5();
                    channel = ServiceMd5;
                }
                DateTime        now     = DateTime.Now; //获取缓存时间
                GetMicroRequest request = new GetMicroRequest();
                request.Request = channel;
                var microList = _client.GetMicro(request: request, 30000.GetCallOptions()).Micros.ToList();

                #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
                            });
                        }
                    });
                    GrpcFactory.Synchronization(scs);

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

                #endregion
            }
            catch
            {
                try
                {
                    _channel.ShutdownAsync();
                    // return (null, FailMessage($"负载中心连接失败!"));
                    _channel = new Channel($"{SettingService.Local.IpAddress}:{SettingService.Local.Port}", ChannelCredentials.Insecure);
                    _client  = new BrokerCenter.BrokerCenterClient(_channel);
                }
                catch
                {
                    // ignored
                }
            }
            #endregion
        }
Exemplo n.º 4
0
 public Request(string host, int port)
 {
     this.id   = $"{host}:{port}";
     disposed  = false;
     transport = GrpcFactory.BorrowInstance(id);
 }