Adds configurable default service tag for queries.

This commit is contained in:
Fitzgerald, Andrew
2016-04-02 01:09:42 -04:00
committed by Spencer Gibb
parent 2b38a3c275
commit 18456bccbf
3 changed files with 62 additions and 1 deletions

View File

@@ -114,6 +114,11 @@ public class ConsulDiscoveryProperties {
*/
private Map<String, String> serverListQueryTags = new HashMap<>();
/**
* Tag to query for in service list if one is not listed in serverListQueryTags.
*/
private String defaultQueryTag;
/**
* Add the 'passing` parameter to /v1/health/service/serviceName.
* This pushes health check passing to the server.
@@ -139,6 +144,16 @@ public class ConsulDiscoveryProperties {
this.hostname = this.hostInfo.getHostname();
}
/**
*
* @param serviceId The service who's filtering tag is being looked up
* @return The tag the given service id should be filtered by, or null.
*/
public String getQueryTagForService(String serviceId){
String tag = serverListQueryTags.get(serviceId);
return tag != null ? tag : defaultQueryTag;
}
public String getHostname() {
return this.preferIpAddress ? this.ipAddress : this.hostname;
}

View File

@@ -76,7 +76,7 @@ public class ConsulServerList extends AbstractServerList<ConsulServer> {
}
private String getTag() {
return this.properties.getServerListQueryTags().get(this.serviceId);
return this.properties.getQueryTagForService(this.serviceId);
}
@Override

View File

@@ -0,0 +1,46 @@
package org.springframework.cloud.consul.discovery;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import java.util.Collections;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ConsulDiscoveryPropertiesTests {
private static final String DEFAULT_TAG = "defaultTag";
private static final String MAP_TAG = "mapTag";
private static final String SERVICE_NAME_IN_MAP = "serviceNameInMap";
private static final String SERVICE_NAME_NOT_IN_MAP = "serviceNameNotInMap";
private ConsulDiscoveryProperties properties;
private Map<String, String> serverListQueryTags = Collections.singletonMap(SERVICE_NAME_IN_MAP, MAP_TAG);
@Before
public void setUp() throws Exception {
properties = new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties()));
properties.setDefaultQueryTag(DEFAULT_TAG);
properties.setServerListQueryTags(serverListQueryTags);
}
@Test
public void testReturnsNullWhenNoDefaultAndNotInMap() throws Exception {
properties.setDefaultQueryTag(null);
assertNull(properties.getQueryTagForService(SERVICE_NAME_NOT_IN_MAP));
}
@Test
public void testGetTagReturnsDefaultWhenNotInMap() throws Exception {
assertEquals(DEFAULT_TAG, properties.getQueryTagForService(SERVICE_NAME_NOT_IN_MAP));
}
@Test
public void testGetTagReturnsMapValueWhenInMap() throws Exception {
assertEquals(MAP_TAG, properties.getQueryTagForService(SERVICE_NAME_IN_MAP));
}
}