From abb6e8ab5d6eb8b926ede2fb05cd9fd21a21a447 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 19 Jan 2016 14:16:56 -0700 Subject: [PATCH] Filter services by tag. fixes gh-18 --- .../discovery/ConsulDiscoveryProperties.java | 8 ++ .../ConsulRibbonClientConfiguration.java | 4 +- .../consul/discovery/ConsulServerList.java | 7 +- .../discovery/ConsulServerListTests.java | 91 +++++++++++++++++++ 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulServerListTests.java diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index a12e532b..1c07549c 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -18,7 +18,9 @@ package org.springframework.cloud.consul.discovery; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import lombok.AccessLevel; import lombok.Data; @@ -101,6 +103,12 @@ public class ConsulDiscoveryProperties { /** Suffix to use when registering management service */ private String managementSuffix = MANAGEMENT; + /** + * Map of serviceId's -> tag to query for in server list. + * This allows filtering services by a single tag. + */ + private Map serverListQueryTags = new HashMap<>(); + private ConsulDiscoveryProperties() {} public ConsulDiscoveryProperties(InetUtils inetUtils) { diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java index 3e757170..822a52ee 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulRibbonClientConfiguration.java @@ -66,8 +66,8 @@ public class ConsulRibbonClientConfiguration { @Bean @ConditionalOnMissingBean - public ServerList ribbonServerList(IClientConfig config) { - ConsulServerList serverList = new ConsulServerList(client); + public ServerList ribbonServerList(IClientConfig config, ConsulDiscoveryProperties properties) { + ConsulServerList serverList = new ConsulServerList(client, properties); serverList.initWithNiwsConfig(config); return serverList; } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java index b913be87..213f3556 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java @@ -33,11 +33,13 @@ import com.netflix.loadbalancer.AbstractServerList; public class ConsulServerList extends AbstractServerList { private final ConsulClient client; + private final ConsulDiscoveryProperties properties; private String serviceId; - public ConsulServerList(ConsulClient client) { + public ConsulServerList(ConsulClient client, ConsulDiscoveryProperties properties) { this.client = client; + this.properties = properties; } @Override @@ -59,8 +61,9 @@ public class ConsulServerList extends AbstractServerList { if (client == null) { return Collections.emptyList(); } + String tag = this.properties.getServerListQueryTags().get(this.serviceId); // null is ok Response> response = client.getCatalogService( - this.serviceId, QueryParams.DEFAULT); + this.serviceId, tag, QueryParams.DEFAULT); if (response.getValue() == null || response.getValue().isEmpty()) { return Collections.emptyList(); } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulServerListTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulServerListTests.java new file mode 100644 index 00000000..a87fdf55 --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulServerListTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2013-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.consul.discovery; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.agent.model.NewService; +import com.netflix.client.config.DefaultClientConfigImpl; + +import org.junit.Test; +import org.springframework.cloud.util.InetUtils; +import org.springframework.cloud.util.InetUtilsProperties; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author Spencer Gibb + */ +public class ConsulServerListTests { + + @Test + public void tagsWork() { + String name = "consulServerListTestsService"; + ConsulClient consul = new ConsulClient(); + NewService nonTagged = new NewService(); + nonTagged.setAddress("localhost"); + nonTagged.setId(name+"NonTagged"); + nonTagged.setName(name); + nonTagged.setPort(8080); + + NewService tagged = new NewService(); + tagged.setAddress("localhost"); + tagged.setId(name+"Tagged"); + tagged.setName(name); + tagged.setPort(9080); + String tag = "mytag"; + tagged.setTags(Arrays.asList(tag)); + + try { + consul.agentServiceRegister(nonTagged); + consul.agentServiceRegister(tagged); + + InetUtils inetUtils = new InetUtils(new InetUtilsProperties()); + DefaultClientConfigImpl config = new DefaultClientConfigImpl(); + config.setClientName(tagged.getName()); + + ConsulServerList serverList = new ConsulServerList(consul, new ConsulDiscoveryProperties(inetUtils)); + serverList.initWithNiwsConfig(config); + + List servers = serverList.getInitialListOfServers(); + assertThat("servers was wrong size", servers, hasSize(2)); + + serverList = new ConsulServerList(consul, getProperties(name, tag, inetUtils)); + serverList.initWithNiwsConfig(config); + + servers = serverList.getInitialListOfServers(); + assertThat("servers was wrong size", servers, hasSize(1)); + ConsulServer server = servers.get(0); + assertThat("server was wrong", server.getPort(), is(9080)); + } finally { + consul.agentServiceDeregister(nonTagged.getId()); + consul.agentServiceDeregister(tagged.getId()); + } + } + + private ConsulDiscoveryProperties getProperties(String name, String tag, InetUtils inetUtils) { + ConsulDiscoveryProperties properties = new ConsulDiscoveryProperties(inetUtils); + HashMap map = new HashMap<>(); + map.put(name, tag); + properties.setServerListQueryTags(map); + return properties; + } +}