public async Task <List <URL> > Lookup(string serviceName) { try { // ConsulService service = ConsulUtils.BuildService(url); ConsulResponse <List <ConsulService> > response = await _client.LookupHealthService(serviceName, 0L); List <ConsulService> services = response.Value; List <URL> urls = new List <URL>(services.Count); foreach (ConsulService service1 in services) { urls.Add(ConsulUtils.BuildUrl(service1)); } return(urls); } catch (Exception e) { throw new Exception("Failed to lookup " + serviceName + " from consul " + GetUrl() + ", cause: " + e.Message, e); } }
/** * 根据service生成URL * * @param service * @return */ public static URL BuildUrl(ConsulService service) { URL url = null; foreach (var tag in service.Tags) { if (tag.StartsWith("URL_")) { var encodeUrl = tag.Substring(tag.IndexOf("_") + 1); url = URL.ValueOf(URL.Decode(encodeUrl)); } } if (url == null) { var param = new Dictionary <string, string>(); var group = service.Name.Substring("providers_".Length); param.Add(Constants.GroupKey, group); param.Add("nodeType", service.Tags[2]); string protocol = ConsulUtils.GetProtocolFromTag(service.Tags[0]); url = new URL(protocol, service.Address, service.Port, ConsulUtils.GetPathFromServiceId(service.Id), param); } return(url); }