From 049eb2db7d35db7c90277fb164338e142aeccef2 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 5 Feb 2015 11:11:59 -0700 Subject: [PATCH] move more over to new client --- .../consul/config/ConsulPropertySource.java | 5 +- .../discovery/ConsulDiscoveryClient.java | 50 +++++++++------- .../ConsulRibbonClientConfiguration.java | 58 +++++-------------- .../consul/discovery/ConsulServerList.java | 21 ++----- 4 files changed, 50 insertions(+), 84 deletions(-) diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java index 9364c010..e093ce95 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySource.java @@ -8,8 +8,7 @@ import org.springframework.core.env.EnumerablePropertySource; import java.util.*; -import static com.google.common.base.Charsets.UTF_8; -import static com.google.common.io.BaseEncoding.base64; +import static org.springframework.util.Base64Utils.*; /** * @author Spencer Gibb @@ -47,7 +46,7 @@ public class ConsulPropertySource extends EnumerablePropertySource public String getDecoded(String value) { if (value == null) return null; - return new String(base64().decode(value), UTF_8); + return new String(decodeFromString(value)); } @Override diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java index f7a16d9a..9f80a909 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java @@ -1,6 +1,12 @@ package org.springframework.cloud.consul.discovery; import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.agent.model.Member; +import com.ecwid.consul.v1.agent.model.Self; +import com.ecwid.consul.v1.agent.model.Service; +import com.ecwid.consul.v1.catalog.model.CatalogService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; @@ -29,51 +35,51 @@ public class ConsulDiscoveryClient implements DiscoveryClient { @Override public ServiceInstance getLocalServiceInstance() { - /*FIXME: Map services = agentClient.getServices(); - Service service = services.get(context.getId()); + Response> agentServices = client.getAgentServices(); + Service service = agentServices.getValue().get(context.getId()); if (service == null) { throw new IllegalStateException("Unable to locate service in consul agent: "+context.getId()); } String host = "localhost"; - Map self = agentClient.getSelf(); - Map member = (Map) self.get("Member"); + Response agentSelf = client.getAgentSelf(); + Member member = agentSelf.getValue().getMember(); if (member != null) { - if (member.containsKey("Name")) { - host = (String) member.get("Name"); + if (member.getName() != null) { + host = member.getName(); } } - return new DefaultServiceInstance(service.getId(), host, service.getPort());*/ - return null; + return new DefaultServiceInstance(service.getId(), host, service.getPort()); } @Override public List getInstances(final String serviceId) { - //FIXME: List nodes = catalogClient.getServiceNodes(serviceId); List instances = new ArrayList<>(); - /*for (ServiceNode node : nodes) { - instances.add(new DefaultServiceInstance(serviceId, node.getNode(), node.getServicePort())); - }*/ + + addInstancesToList(instances, serviceId); return instances; } - @Override + private void addInstancesToList(List instances, String serviceId) { + Response> services = client.getCatalogService(serviceId, QueryParams.DEFAULT); + for (CatalogService service : services.getValue()) { + instances.add(new DefaultServiceInstance(serviceId, service.getNode(), service.getServicePort())); + } + } + + @Override public List getAllInstances() { List instances = new ArrayList<>(); - /*FIXME: for (String serviceId : catalogClient.getServices().keySet()) { - List serviceNodes = catalogClient.getServiceNodes(serviceId); - if (serviceNodes != null) { - for (ServiceNode node : serviceNodes) { - instances.add(new DefaultServiceInstance(node.getServiceName(), node.getNode(), node.getServicePort())); - } - } - }*/ + Response>> services = client.getCatalogServices(QueryParams.DEFAULT); + for (String serviceId : services.getValue().keySet()) { + addInstancesToList(instances, serviceId); + } return instances; } @Override public List getServices() { - return new ArrayList<>();//FIXME: catalogClient.getServices().keySet()); + return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue().keySet()); } } 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 3a9106f1..96913c10 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 @@ -16,24 +16,23 @@ package org.springframework.cloud.consul.discovery; -import static com.netflix.client.config.CommonClientConfigKey.*; +import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses; +import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity; import javax.annotation.PostConstruct; -import com.ecwid.consul.v1.ConsulClient; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.cloud.netflix.ribbon.ZonePreferenceServerListFilter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.ecwid.consul.v1.ConsulClient; +import com.netflix.client.config.IClientConfig; import com.netflix.config.ConfigurationManager; import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicStringProperty; -import com.netflix.loadbalancer.DynamicServerListLoadBalancer; import com.netflix.loadbalancer.ServerList; -import com.netflix.loadbalancer.ZoneAvoidanceRule; /** * Preprocessor that configures defaults for eureka-discovered ribbon clients. Such as: @@ -44,7 +43,7 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule; * @author Dave Syer */ @Configuration -public class ConsulRibbonClientConfiguration implements BeanPostProcessor { +public class ConsulRibbonClientConfiguration { @Autowired private ConsulClient client; @@ -56,53 +55,26 @@ public class ConsulRibbonClientConfiguration implements BeanPostProcessor { protected static final String DEFAULT_NAMESPACE = "ribbon"; public ConsulRibbonClientConfiguration() { - System.out.println("here"); } public ConsulRibbonClientConfiguration(String serviceId) { this.serviceId = serviceId; } + @Bean + @ConditionalOnMissingBean + public ServerList ribbonServerList(IClientConfig config) { + ConsulServerList serverList = new ConsulServerList(client); + serverList.initWithNiwsConfig(config); + return serverList; + } + @PostConstruct public void preprocess() { - // TODO: should this look more like hibernate spring boot props? - setProp(this.serviceId, NIWSServerListClassName.key(), - ConsulServerList.class.getName()); - // FIXME: what should this be? setProp(this.serviceId, DeploymentContextBasedVipAddresses.key(), this.serviceId); - setProp(this.serviceId, NFLoadBalancerRuleClassName.key(), - ZoneAvoidanceRule.class.getName()); - setProp(this.serviceId, NIWSServerListFilterClassName.key(), - ZonePreferenceServerListFilter.class.getName()); setProp(this.serviceId, EnableZoneAffinity.key(), "true"); } - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } - - @Override - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - if (bean instanceof DynamicServerListLoadBalancer) { - wrapServerList((DynamicServerListLoadBalancer) bean); - } - return bean; - } - - private void wrapServerList(DynamicServerListLoadBalancer balancer) { - // TODO: fix this set client hack - @SuppressWarnings("unchecked") - DynamicServerListLoadBalancer dynamic = (DynamicServerListLoadBalancer) balancer; - ServerList list = dynamic.getServerListImpl(); - if (list instanceof ConsulServerList) { - ConsulServerList csl = (ConsulServerList) list; - csl.setClient(client); - } - } - protected void setProp(String serviceId, String suffix, String value) { // how to set the namespace properly? String key = getKey(serviceId, suffix); 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 2d90e6c8..513eaebd 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,19 +16,11 @@ import java.util.List; */ public class ConsulServerList extends AbstractServerList { - private ConsulClient client; + private final ConsulClient client; private String serviceId; - public ConsulServerList() { - } - - public ConsulServerList(ConsulClient client, String serviceId) { - this.client = client; - this.serviceId = serviceId; - } - - public void setClient(ConsulClient client) { + public ConsulServerList(ConsulClient client) { this.client = client; } @@ -55,13 +47,10 @@ public class ConsulServerList extends AbstractServerList { if (response.getValue() == null || response.getValue().isEmpty()) { return Collections.EMPTY_LIST; } - - List servers = new ArrayList<>(); - for (ServiceNode node : nodes) { - ConsulServer server = new ConsulServer(node); - servers.add(server); + ArrayList servers = new ArrayList<>(); + for (CatalogService service : response.getValue()) { + servers.add(new ConsulServer(service)); } - return servers; } }