Filter services by tag.

fixes gh-18
This commit is contained in:
Spencer Gibb
2016-01-19 14:16:56 -07:00
parent f8f2d87d0d
commit abb6e8ab5d
4 changed files with 106 additions and 4 deletions

View File

@@ -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<String, String> serverListQueryTags = new HashMap<>();
private ConsulDiscoveryProperties() {}
public ConsulDiscoveryProperties(InetUtils inetUtils) {

View File

@@ -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;
}

View File

@@ -33,11 +33,13 @@ import com.netflix.loadbalancer.AbstractServerList;
public class ConsulServerList extends AbstractServerList<ConsulServer> {
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<ConsulServer> {
if (client == null) {
return Collections.emptyList();
}
String tag = this.properties.getServerListQueryTags().get(this.serviceId); // null is ok
Response<List<CatalogService>> response = client.getCatalogService(
this.serviceId, QueryParams.DEFAULT);
this.serviceId, tag, QueryParams.DEFAULT);
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.emptyList();
}

View File

@@ -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<ConsulServer> 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<String, String> map = new HashMap<>();
map.put(name, tag);
properties.setServerListQueryTags(map);
return properties;
}
}