コード例 #1
0
ファイル: ConsulGateway.cs プロジェクト: zyj0021/CRL5
        /// <summary>
        /// 按服务名称,随机返回一个服务地址
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="minute"></param>
        /// <returns></returns>
        public ServiceInfo GetServiceInfo(string serviceName, double minute = 0)
        {
            Dictionary <string, ServiceInfo> all;

            if (minute > 0)
            {
                all = DelegateCache.Init("consulServiceCache", minute, () =>
                {
                    return(GetAllServices());
                });
            }
            else
            {
                all = GetAllServices();
            }
            var services = all.Values.Where(b => b.Service == serviceName).ToList();

            if (services.Count == 0)
            {
                throw new Exception($"找不到可用的服务:{serviceName}");
            }
            int k = rng.Next(services.Count);

            return(services[k]);
        }
コード例 #2
0
ファイル: Consul.cs プロジェクト: qkb/CRL.NetStandard
        /// <summary>
        /// 按服务名称,随机返回一个服务地址
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="minute"></param>
        /// <returns></returns>
        public CatalogService GetServiceInfo(string serviceName, bool passingOnly = false, double minute = 0)
        {
            List <CatalogService> all;

            if (minute > 0)
            {
                all = DelegateCache.Init("consulServiceCache", minute, () =>
                {
                    return(GetService(serviceName, passingOnly));
                });
            }
            else
            {
                all = GetService(serviceName, passingOnly);
            }
            if (all.Count == 0)
            {
                throw new Exception($"找不到可用的服务:{serviceName}");
            }
            int k = rng.Next(all.Count);

            return(all[k]);
        }