Ejemplo n.º 1
0
        private TResponse Call <TResponse>(Func <CallInvoker, TResponse> call)
        {
            int maxRetry = _maxRetry;

            while (true)
            {
                EndpointModel endpoint;
                var           callInvoker = _endpointStrategy.Get(_serviceName, out endpoint);
                if (callInvoker == null)
                {
                    throw new GrpcConsulException($"can't find servie: {_serviceName} from {(endpoint != null ? endpoint.ToString() : "")}");
                }
                try
                {
                    return(call(callInvoker));
                }
                catch (Exception ex)
                {
                    if (ex is RpcException && endpoint.IsDisconvry)
                    {
                        _endpointStrategy.AddBlack(endpoint.ToString());
                    } //加入黑名单,避免注册中心还存在,但是实际服务已经关闭时
                    if (0 > --maxRetry)
                    {
                        throw;
                    }
                } //直接异常抛出去
            }
        }