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(target => !ServiceBlackPlicy.In(ServiceName, target) || !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 #2
0
        /// <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(serviceName, failedChannel.Target);
                failedChannel.ShutdownAsync();

                // if not exist invoker, call init method
                if (callInvokers.Count <= 0)
                {
                    SetCallInvokers(serviceName, false);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public ServerCallInvoker Get(string serviceName)
        {
            if (_invokers.TryGetValue(serviceName, out List <ServerCallInvoker> callInvokers) &&
                callInvokers.Count > 0)
            {
                return(ServicePollingPlicy.Random(callInvokers));
            }

            lock (_lock)
            {
                if (_invokers.TryGetValue(serviceName, out callInvokers) &&
                    callInvokers.Count > 0)
                {
                    return(ServicePollingPlicy.Random(callInvokers));
                }

                callInvokers = SetCallInvokers(serviceName);
                if ((callInvokers?.Count ?? 0) <= 0 && ServiceBlackPlicy.Exist(serviceName))
                {
                    callInvokers = SetCallInvokers(serviceName, false);
                }

                return(ServicePollingPlicy.Random(callInvokers));
            }
        }
Beispiel #4
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(target => !ServiceBlackPlicy.In(ServiceName, target) || !filterBlack)
                          .ToList();

            return(targets);
        }