From a1d7eb8d0883fc9f1ecfea09facb53473f607a40 Mon Sep 17 00:00:00 2001 From: fleow Date: Wed, 19 Sep 2018 16:12:27 -0700 Subject: [PATCH 1/2] Modify the getServices() method so that it filters services based on the default query tag if it is set --- .../discovery/ConsulDiscoveryClient.java | 21 +++++++++++++++---- ...ulDiscoveryClientDefaultQueryTagTests.java | 12 +++++++++++ .../discovery/ConsulDiscoveryClientTests.java | 7 +++++++ 3 files changed, 36 insertions(+), 4 deletions(-) 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..bd849826f 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,18 @@ 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)); + } + + @Test + public void returnAllServicesIfTagNotSet() { + + } + 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))); From 56358a40f3dd951791eecbfbfab9b2cd651526c6 Mon Sep 17 00:00:00 2001 From: fleow Date: Wed, 19 Sep 2018 16:12:27 -0700 Subject: [PATCH 2/2] Modify the getServices() method so that it filters services based on the default query tag if it is set --- .../discovery/ConsulDiscoveryClientDefaultQueryTagTests.java | 5 ----- 1 file changed, 5 deletions(-) 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 bd849826f..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 @@ -91,11 +91,6 @@ public void shouldReturnOnlyIntgService() { assertThat("instance is not intg", services.get(0).equals(NAME)); } - @Test - public void returnAllServicesIfTagNotSet() { - - } - private NewService serviceForEnvironment(String env, int port) { NewService service = new NewService(); service.setAddress("localhost");