move more over to new client

This commit is contained in:
Spencer Gibb
2015-02-05 11:11:59 -07:00
parent 8527d68aa1
commit 049eb2db7d
4 changed files with 50 additions and 84 deletions

View File

@@ -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<ConsulClient>
public String getDecoded(String value) {
if (value == null)
return null;
return new String(base64().decode(value), UTF_8);
return new String(decodeFromString(value));
}
@Override

View File

@@ -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<String, Service> services = agentClient.getServices();
Service service = services.get(context.getId());
Response<Map<String, Service>> 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<String, Object> self = agentClient.getSelf();
Map<String, Object> member = (Map<String, Object>) self.get("Member");
Response<Self> 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<ServiceInstance> getInstances(final String serviceId) {
//FIXME: List<ServiceNode> nodes = catalogClient.getServiceNodes(serviceId);
List<ServiceInstance> 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<ServiceInstance> instances, String serviceId) {
Response<List<CatalogService>> services = client.getCatalogService(serviceId, QueryParams.DEFAULT);
for (CatalogService service : services.getValue()) {
instances.add(new DefaultServiceInstance(serviceId, service.getNode(), service.getServicePort()));
}
}
@Override
public List<ServiceInstance> getAllInstances() {
List<ServiceInstance> instances = new ArrayList<>();
/*FIXME: for (String serviceId : catalogClient.getServices().keySet()) {
List<ServiceNode> serviceNodes = catalogClient.getServiceNodes(serviceId);
if (serviceNodes != null) {
for (ServiceNode node : serviceNodes) {
instances.add(new DefaultServiceInstance(node.getServiceName(), node.getNode(), node.getServicePort()));
}
}
}*/
Response<Map<String, List<String>>> services = client.getCatalogServices(QueryParams.DEFAULT);
for (String serviceId : services.getValue().keySet()) {
addInstancesToList(instances, serviceId);
}
return instances;
}
@Override
public List<String> getServices() {
return new ArrayList<>();//FIXME: catalogClient.getServices().keySet());
return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue().keySet());
}
}

View File

@@ -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<ConsulServer> dynamic = (DynamicServerListLoadBalancer<ConsulServer>) balancer;
ServerList<ConsulServer> 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);

View File

@@ -16,19 +16,11 @@ import java.util.List;
*/
public class ConsulServerList extends AbstractServerList<ConsulServer> {
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<ConsulServer> {
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.EMPTY_LIST;
}
List<ConsulServer> servers = new ArrayList<>();
for (ServiceNode node : nodes) {
ConsulServer server = new ConsulServer(node);
servers.add(server);
ArrayList<ConsulServer> servers = new ArrayList<>();
for (CatalogService service : response.getValue()) {
servers.add(new ConsulServer(service));
}
return servers;
}
}