Merge pull request #164 from jjathman/allow-passing-query-params
* allow-passing-query-params: Add the ability to pass along query parameters to Consul when discovering services. One potential use is for passing a different data center value than the default value.
This commit is contained in:
@@ -41,6 +41,7 @@ import static org.springframework.cloud.consul.discovery.ConsulServerUtils.getMe
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Joe Athman
|
||||
*/
|
||||
@CommonsLog
|
||||
public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
@@ -110,16 +111,22 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(final String serviceId) {
|
||||
return getInstances(serviceId, QueryParams.DEFAULT);
|
||||
}
|
||||
|
||||
public List<ServiceInstance> getInstances(final String serviceId,
|
||||
final QueryParams queryParams) {
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
|
||||
addInstancesToList(instances, serviceId);
|
||||
addInstancesToList(instances, serviceId, queryParams);
|
||||
|
||||
return instances;
|
||||
}
|
||||
|
||||
private void addInstancesToList(List<ServiceInstance> instances, String serviceId) {
|
||||
private void addInstancesToList(List<ServiceInstance> instances, String serviceId,
|
||||
QueryParams queryParams) {
|
||||
Response<List<HealthService>> services = client.getHealthServices(serviceId,
|
||||
this.properties.isQueryPassing(), QueryParams.DEFAULT);
|
||||
this.properties.isQueryPassing(), queryParams);
|
||||
for (HealthService service : services.getValue()) {
|
||||
String host = findHost(service);
|
||||
instances.add(new DefaultServiceInstance(serviceId, host, service
|
||||
@@ -133,7 +140,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
Response<Map<String, List<String>>> services = client
|
||||
.getCatalogServices(QueryParams.DEFAULT);
|
||||
for (String serviceId : services.getValue().keySet()) {
|
||||
addInstancesToList(instances, serviceId);
|
||||
addInstancesToList(instances, serviceId, QueryParams.DEFAULT);
|
||||
}
|
||||
return instances;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -33,17 +31,28 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.Response;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Joe Athman
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ConsulDiscoveryClientTests.MyTestConfig.class)
|
||||
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscovery",
|
||||
"spring.cloud.consul.discovery.preferIpAddress=true"}, randomPort = true)
|
||||
@WebIntegrationTest(value = { "spring.application.name=testConsulDiscovery",
|
||||
"spring.cloud.consul.discovery.preferIpAddress=true" }, randomPort = true)
|
||||
public class ConsulDiscoveryClientTests {
|
||||
|
||||
@Autowired
|
||||
private ConsulDiscoveryClient discoveryClient;
|
||||
@Autowired
|
||||
private ConsulClient consulClient;
|
||||
|
||||
@Test
|
||||
public void getInstancesForServiceWorks() {
|
||||
@@ -55,8 +64,23 @@ public class ConsulDiscoveryClientTests {
|
||||
assertIpAddress(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInstancesForServiceRespectsQueryParams() {
|
||||
Response<List<String>> catalogDatacenters = consulClient.getCatalogDatacenters();
|
||||
|
||||
List<String> dataCenterList = catalogDatacenters.getValue();
|
||||
assertFalse("no data centers found", dataCenterList.isEmpty());
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("consul",
|
||||
new QueryParams(dataCenterList.get(0)));
|
||||
assertFalse("instances was empty", instances.isEmpty());
|
||||
|
||||
ServiceInstance instance = instances.get(0);
|
||||
assertIpAddress(instance);
|
||||
}
|
||||
|
||||
private void assertIpAddress(ServiceInstance instance) {
|
||||
assertTrue("host isn't an ip address", Character.isDigit(instance.getHost().charAt(0)));
|
||||
assertTrue("host isn't an ip address",
|
||||
Character.isDigit(instance.getHost().charAt(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user