public List <(string serviceId, string target)> FindServiceEndpoints(bool filterBlack = true)
        {
            if (_client == null)
            {
                throw new ArgumentNullException("consul client");
            }

            var targets = new List <(string, string)>();

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

                targets = r.Response
                          .Select(x => (x.Service.ID, $"{x.Service.Address}:{x.Service.Port}"))
                          .Where(target => !ServiceBlackPolicy.In(Options.ServiceName, target.Item2) || !filterBlack)
                          .ToList();
            }
            catch { }
            return(targets);
        }
Пример #2
0
        /// <summary>
        /// 屏蔽
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="failedCallInvoker"></param>
        public void Revoke(string serviceName, ChannelWrapper channelWrapper)
        {
            lock (_lock)
            {
                if (channelWrapper == null)
                {
                    return;
                }

                // channels
                if (_channelWrappers.TryGetValue(serviceName, out List <ChannelWrapper> channels) &&
                    channels.Any(oo => oo == channelWrapper))
                {
                    channels.Remove(channelWrapper);
                    _channelWrappers.AddOrUpdate(serviceName, channels, (k, v) => channels);
                }

                // add black
                ServiceBlackPolicy.Add(serviceName, channelWrapper.Target);

                channelWrapper.ShutdownAsync();

                // reinit callinvoker
                if (channels.Count <= 0)
                {
                    GetSetChannels(serviceName, false);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public List <ChannelWrapper> GetChannelWrappers(string serviceName)
        {
            if (_channelWrappers.TryGetValue(serviceName, out List <ChannelWrapper> channels) &&
                channels.Count > 0)
            {
                return(channels);
            }

            lock (_lock)
            {
                if (_channelWrappers.TryGetValue(serviceName, out channels) &&
                    channels.Count > 0)
                {
                    return(channels);
                }

                channels = GetSetChannels(serviceName);
                if ((channels?.Count ?? 0) <= 0 && ServiceBlackPolicy.Exist(serviceName))
                {
                    channels = GetSetChannels(serviceName, false);
                }

                return(channels);
            }
        }
Пример #4
0
        public List <(string serviceId, string target)> 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(target => !ServiceBlackPolicy.In(Options.ServiceName, target.Item2) || !filterBlack)
                          .ToList();

            return(targets);
        }