예제 #1
0
        /// <summary>
        /// 获取所有服务
        /// </summary>
        /// <returns></returns>
        public Dictionary <string, ServiceInfo> GetAllServices()
        {
            var url = $"{ConsulHost}/consul/GetAllServices";

            try
            {
                var result   = request.Get(url);
                var services = result.ToObject <Dictionary <string, ServiceInfo> >();
                return(services);
            }
            catch (Exception ero)
            {
                throw new Exception($"无法获取consul服务注册,{ero}");
            }
        }
예제 #2
0
        public List <CatalogService> GetService(string serviceName, bool passingOnly)
        {
            if (!_ocelotGateway)
            {
                var client = new consul((cfg) =>
                {
                    var uriBuilder = new UriBuilder(ConsulHost);
                    cfg.Address    = uriBuilder.Uri;
                });
                var result = client.Health.Service(serviceName, "", passingOnly).Result;
                if (result.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception($"无法获取consul服务注册,{result.StatusCode }");
                }
                return(result.Response.Select(b => new CatalogService
                {
                    ServiceAddress = b.Service.Address,
                    ServiceID = b.Service.ID,
                    ServiceName = serviceName,
                    ServicePort = b.Service.Port,
                    ServiceMeta = b.Service.Meta,
                    ServiceTags = b.Service.Tags
                }).ToList());
            }
            var url = _ocelotGateway ? $"{ConsulHost}/consul/GetService?serviceName={serviceName}&passingOnly={passingOnly}" : $"{ConsulHost}/v1/catalog/service/{serviceName}";

            try
            {
                var result   = request.Get(url);
                var services = result.ToObject <List <CatalogService> >();
                return(services);
            }
            catch (Exception ero)
            {
                throw new Exception($"无法获取consul服务注册,{ero}");
            }
        }