From 5ff796751486e80b3ee22e16a52ea8afc0b6006e Mon Sep 17 00:00:00 2001 From: Joseph Athman Date: Mon, 7 Nov 2016 15:02:24 -0600 Subject: [PATCH] Fixes #246 - allows for easier extending of the ConsulServerList class so customization of it's behavior can be done more easily. --- .../consul/discovery/ConsulServerList.java | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) 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 f4dd6631..db698b57 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 @@ -16,10 +16,6 @@ package org.springframework.cloud.consul.discovery; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.QueryParams; import com.ecwid.consul.v1.Response; @@ -27,6 +23,10 @@ import com.ecwid.consul.v1.health.model.HealthService; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * @author Spencer Gibb */ @@ -42,6 +42,18 @@ public class ConsulServerList extends AbstractServerList { this.properties = properties; } + protected ConsulClient getClient() { + return client; + } + + protected ConsulDiscoveryProperties getProperties() { + return properties; + } + + protected String getServiceId() { + return serviceId; + } + @Override public void initWithNiwsConfig(IClientConfig clientConfig) { this.serviceId = clientConfig.getClientName(); @@ -64,12 +76,23 @@ public class ConsulServerList extends AbstractServerList { String tag = getTag(); // null is ok Response> response = this.client.getHealthServices( this.serviceId, tag, this.properties.isQueryPassing(), - QueryParams.DEFAULT); + createQueryParamsForClientRequest()); if (response.getValue() == null || response.getValue().isEmpty()) { return Collections.emptyList(); } + return transformResponse(response.getValue()); + } + + /** + * Transforms the response from Consul in to a list of usable {@link ConsulServer}s. + * + * @param healthServices the initial list of servers from Consul. Guaranteed to be non-empty list + * @return ConsulServer instances + * @see ConsulServer#ConsulServer(HealthService) + */ + protected List transformResponse(List healthServices) { List servers = new ArrayList<>(); - for (HealthService service : response.getValue()) { + for (HealthService service : healthServices) { ConsulServer server = new ConsulServer(service); if (server.getMetadata().containsKey(this.properties.getDefaultZoneMetadataName())) { server.setZone(server.getMetadata().get(this.properties.getDefaultZoneMetadataName())); @@ -79,7 +102,16 @@ public class ConsulServerList extends AbstractServerList { return servers; } - private String getTag() { + /** + * This method will create teh {@link QueryParams} to use when retrieving the + * services from Consul. By default {@link QueryParams#DEFAULT} is used. + * @return an instance of {@link QueryParams} + */ + protected QueryParams createQueryParamsForClientRequest() { + return QueryParams.DEFAULT; + } + + protected String getTag() { return this.properties.getQueryTagForService(this.serviceId); }