diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java index 694bdd59f..61db77a23 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.QueryParams; @@ -119,12 +120,24 @@ public List getAllInstances() { public List getServices() { String aclToken = properties.getAclToken(); + Map> services; if (StringUtils.hasText(aclToken)) { - return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT, aclToken).getValue() - .keySet()); + services = client.getCatalogServices(QueryParams.DEFAULT, aclToken).getValue(); } else { - return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue() - .keySet()); + services = client.getCatalogServices(QueryParams.DEFAULT).getValue(); + } + + return services.entrySet().stream() + .filter(e -> defaultQueryTagFilter(e.getValue())) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + private boolean defaultQueryTagFilter(List tags) { + if (StringUtils.isEmpty(properties.getDefaultQueryTag())) { + return true; + } else { + return tags.contains(properties.getDefaultQueryTag()); } } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java index c148c0b55..1b7039cb9 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java @@ -84,6 +84,13 @@ public void shouldReturnOnlyIntgInstance() { assertThat("instance is not intg", instances.get(0).getMetadata(), hasEntry("intg", "intg")); } + @Test + public void shouldReturnOnlyIntgService() { + List services = discoveryClient.getServices(); + assertThat("instances was wrong size", services, hasSize(1)); + assertThat("instance is not intg", services.get(0).equals(NAME)); + } + private NewService serviceForEnvironment(String env, int port) { NewService service = new NewService(); service.setAddress("localhost"); diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientTests.java index f9eaa1f68..4e36756bb 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientTests.java @@ -78,6 +78,13 @@ public void getInstancesForServiceRespectsQueryParams() { assertIpAddress(instance); } + @Test + public void getServicesWorks() { + List services = discoveryClient.getServices(); + assertNotNull("services was null", services); + assertFalse("services was empty", services.isEmpty()); + } + private void assertIpAddress(ServiceInstance instance) { assertTrue("host isn't an ip address", Character.isDigit(instance.getHost().charAt(0)));