예제 #1
0
        private static string BrokerDnsInner(Dictionary <string, string> input, Micro micro)
        {
            string output = string.Empty;

            #region 调用链

            var trace = TransmitTrace.SetTraceId(input, micro);

            #endregion
            #region 处理请求
            try
            {
                using (Request request = new Request(micro.Ip, micro.Port))
                {
                    output = request.Invoke(input);
                }
            }
            catch (Exception ex) //如果异常则从缓存中清除 该缓存
            {
                _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                throw ex;
            }
            finally
            {
                TracePool.EnQueue(trace, output);
            }

            #endregion

            return(output);
        }
예제 #2
0
        private static string BrokerDnsInner(Dictionary <string, string> input, Micro micro)
        {
            if (micro == null)
            {
                return(FailMessage($"未找到服务【{input[Eng.NAMESPACE]}】"));
            }
            string output = string.Empty;

            #region 调用链

            var trace = TransmitTrace.SetTraceId(input, micro);

            #endregion
            #region 处理请求
            try
            {
                using (Request request = new Request(micro.Ip, micro.Port))
                {
                    output = request.Invoke(input);
                }
            }
            catch (Exception ex) //如果异常则从缓存中清除 该缓存
            {
                if (ex is RpcException && ((RpcException)ex).StatusCode == StatusCode.DeadlineExceeded)
                {
                    output = FailMessage(ex.Message);
                }
                else
                {
                    _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                    throw ex;
                }
            }
            finally
            {
                TracePool.EnQueue(trace, output);
            }

            #endregion

            return(output);
        }
예제 #3
0
        private static string BrokerDnsInner(Dictionary <string, string> input, Micro micro)
        {
            string output = string.Empty;

            #region 调用链

            var trace = TransmitTrace.SetTraceId(input, micro);

            #endregion
            #region 处理请求
            try
            {
                using (Request request = new Request(micro.Ip, micro.Port))
                {
                    output = request.Invoke(input);
                }
            }
            catch (ThriftException ex)
            {
                if (ex.Type.Equals(ExceptionType.NotFoundService))
                {
                    _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                }
                output = FailMessage(ex.Message);
            }
            catch (Exception ex) //如果异常则从缓存中清除 该缓存
            {
                _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                output = FailMessage(ex.Message);
            }
            finally
            {
                TracePool.EnQueue(trace, output);
            }

            #endregion

            return(output);
        }
예제 #4
0
        /// <summary>
        /// 设置调用链 TraceId
        /// </summary>
        /// <param name="input"></param>
        /// <param name="micro">目标服务</param>
        public static sys_trace SetTraceId(Dictionary <string, string> input, Micro micro = null)
        {
            if (!SettingService.TraceOnOff)
            {
                return(null);
            }
            #region 分布式追踪不记录自己,防止调用链循环调用 +TraceRecord

            if (input[Const.Enum.Eng.NAMESPACE] == Namespace && input[Const.Enum.Eng.CLASS] == Class)
            {
                return(null);
            }
            if (input[Const.Enum.Eng.NAMESPACE] == "Anno.Plugs.Monitor" && input[Const.Enum.Eng.CLASS] == "Resource")
            {
                return(null);
            }
            #endregion
            #region 调用链唯一标识+ TraceId
            string TraceId       = "TraceId";       //调用链唯一标识
            string PreTraceId    = "PreTraceId";    //上一级调用链唯一标识
            string AppName       = "AppName";       //调用方AnnoService 名称
            string AppNameTarget = "AppNameTarget"; //目标AnnoService 名称
            string TTL           = "TTL";           //分布式调用计数器 默认0
            string GlobalTraceId = "GlobalTraceId";
            if (input.ContainsKey(TraceId))
            {
                input[PreTraceId] = input[TraceId];            //当前TraceId 变为上一级 TraceId(PreTraceId)
                input[TraceId]    = Guid.NewGuid().ToString(); //生成新的调用链唯一标识
            }
            else
            {
                input.Add(TraceId, Guid.NewGuid().ToString());
                input.Add(PreTraceId, string.Empty);
            }
            //追踪全局标识
            if (!input.ContainsKey(GlobalTraceId))
            {
                input.Add(GlobalTraceId, Guid.NewGuid().ToString());
            }
            #endregion

            #region App名称+AppName
            if (input.ContainsKey(AppName))
            {
                input[AppName] = SettingService.AppName ?? string.Empty;
            }
            else
            {
                input.Add(AppName, SettingService.AppName ?? string.Empty);
            }
            #endregion
            #region 目标APP地址
            if (micro != null)
            {
                string target = "Target";
                if (input.ContainsKey(target))
                {
                    input[target] = micro.Ip + ":" + micro.Port;
                }
                else
                {
                    input.Add(target, micro.Ip + ":" + micro.Port);
                }

                if (input.ContainsKey(AppNameTarget))
                {
                    input[AppNameTarget] = micro.Nickname ?? string.Empty;
                }
                else
                {
                    input.Add(AppNameTarget, micro.Nickname ?? string.Empty);
                }
            }
            #endregion
            #region App+跳转次数
            if (input.ContainsKey(TTL))
            {
                int.TryParse(input[TTL], out int ttl);
                input[TTL] = (ttl + 1).ToString();
            }
            else
            {
                input.Add(TTL, "0");
            }
            #endregion

            return(TracePool.CreateTrance(input));
            //TracePool.EnQueue(input);
        }
예제 #5
0
        private static string BrokerDnsInner(Dictionary <string, string> input, Micro micro)
        {
            if (micro == null)
            {
                return(FailMessage($"未找到服务【{input[Eng.NAMESPACE]}】"));
            }
            string output = string.Empty;

            #region 调用链

            var trace = TransmitTrace.SetTraceId(input, micro);

            #endregion
            #region 处理请求
            int error = 0;
tryRequest:
            if (error > 0)
            {
                micro = Single(input[Eng.NAMESPACE])?.Mi;
                if (micro == null)
                {
                    return(FailMessage($"未找到服务【{input[Eng.NAMESPACE]}】"));
                }
            }
            try
            {
                using (Request request = new Request(micro.Ip, micro.Port))
                {
                    output = request.Invoke(input);
                }
            }
            catch (ThriftException ex)
            {
                if (ex.Type.Equals(ExceptionType.NotFoundService))
                {
                    _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                }
                output = FailMessage(ex.Message);
                error++;
                if (error < 3)
                {
                    goto tryRequest;
                }
            }
            catch (Exception ex) //如果异常则从缓存中清除 该缓存
            {
                _microCaches.RemoveAll(c => c.Mi.Ip == micro.Ip && c.Mi.Port == micro.Port);
                output = FailMessage(ex.Message);
                error++;
                if (error < 3)
                {
                    goto tryRequest;
                }
            }
            finally
            {
                TracePool.EnQueue(trace, output);
            }

            #endregion

            return(output);
        }