Example #1
0
 /// <summary>
 /// 从Consul直接获取服务连接信息,不缓存,格式为{ip}:{port}
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <param name="serviceTags">服务内部标签,可用于服务分级</param>
 /// <returns>可用Host字符串数组</returns>
 public string[] GetServiceHostsNoCache(string serviceName, string serviceTags)
 {
     return(ConsulLoader.AvaliableServices(
                serviceName,
                serviceTags ?? ConsulCache.Instance.GetServiceTags(serviceName))
            .GetAwaiter()
            .GetResult()
            .Select(service => service.Node.Address + ":" + service.Service.Port)
            .ToArray());
 }
Example #2
0
        /// <summary>
        /// 同步可用服务信息
        /// </summary>
        /// <param name="serviceNames">服务名集合</param>
        /// <returns>同步更新可用服务负载点数量</returns>
        private int SyncServices()
        {
            var count = 0;

            try
            {
                foreach (var serviceName in ConsulCache.Instance.GetServiceNames())
                {
                    var services = ConsulLoader.AvaliableServices(serviceName,
                                                                  ConsulCache.Instance.GetServiceTags(serviceName))
                                   .GetAwaiter().GetResult();
                    count += ConsulCache.Instance.SetServiceInfo(serviceName, services);
                }
            }
            catch { }
            return(count);
        }
Example #3
0
        /// <summary>
        /// 获取服务连接信息,格式为{ip}:{port}
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="serviceTags">服务内部标签,可用于服务分级</param>
        /// <returns>可用Host字符串数组</returns>
        public string[] GetServiceHosts(string serviceName, string serviceTags)
        {
            var hosts = ConsulCache.Instance.GetServiceHosts(serviceName, serviceTags ?? string.Empty);

            try
            {
                if (hosts.Length == 0 && !ConsulCache.Instance.GetServiceNames().Contains(serviceName))
                {
                    ConsulCache.Instance.SetServiceInfo(serviceName,
                                                        ConsulLoader.AvaliableServices(
                                                            serviceName,
                                                            serviceTags ?? string.Empty)
                                                        .GetAwaiter()
                                                        .GetResult());
                    hosts = ConsulCache.Instance.GetServiceHosts(serviceName, serviceTags ?? string.Empty);
                }
            }
            catch { }
            return(hosts);
        }