/// <summary>
        /// 屏蔽
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="failedCallInvoker"></param>
        public void Revoke(string serviceName, ServerCallInvoker failedCallInvoker)
        {
            lock (_lock)
            {
                // invokers
                var failedChannel = failedCallInvoker.Channel;
                if (!_invokers.TryGetValue(serviceName, out List <ServerCallInvoker> callInvokers) ||
                    callInvokers.All(x => !ReferenceEquals(failedChannel, x.Channel)))
                {
                    return;
                }

                callInvokers.RemoveAt(callInvokers.FindIndex(x => ReferenceEquals(failedChannel, x.Channel)));
                _invokers.AddOrUpdate(serviceName, callInvokers, (key, value) => callInvokers);

                // channels
                if (_channels.TryGetValue(failedChannel.Target, out Channel channel) &&
                    ReferenceEquals(channel, failedChannel))
                {
                    _channels.TryRemove(failedChannel.Target, out failedChannel);

                    // add black
                    ServiceBlackPlicy.Add(failedChannel.Target);
                }

                failedChannel.ShutdownAsync();

                // if not exist invoker, call init method
                if (callInvokers.Count <= 0)
                {
                    InitCallInvoker(serviceName, false);
                }
            }
        }
        public List <string> FindServiceEndpoints(bool filterBlack = true)
        {
            if (_client == null)
            {
                throw new ArgumentNullException("consul client");
            }

            var targets = new List <string>();

            try
            {
                var r = _client.Health.Service(ServiceName, "", true).Result;
                if (r.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException($"failed to query consul server");
                }

                targets = r.Response
                          .Select(x => $"{x.Service.Address}:{x.Service.Port}")
                          .Where(x => !ServiceBlackPlicy.In(x) || !filterBlack)
                          .ToList();

                //var res = _client.Catalog.Service(ServiceName).Result;
                //if (res.StatusCode != HttpStatusCode.OK)
                //    throw new ApplicationException($"Failed to query services");

                //targets = res.Response
                //             .Select(x => $"{x.ServiceAddress}:{x.ServicePort}")
                //             .Where(x => !ServiceBlackPlicy.In(x) || !filterBlack)
                //             .ToList();
                //return targets;
            }
            catch { }
            return(targets);
        }
Beispiel #3
0
        public List <string> FindServiceEndpoints(bool filterBlack = true)
        {
            if ((_ipEndPoints?.Count ?? 0) <= 0)
            {
                throw new ArgumentOutOfRangeException("endpoint not provide");
            }

            var targets = _ipEndPoints.Select(x => $"{x.Item1}:{x.Item2}")
                          .Where(x => !ServiceBlackPlicy.In(x) || !filterBlack)
                          .ToList();

            return(targets);
        }