updates to new s-c-commons stuff, remove unused class

This commit is contained in:
Spencer Gibb
2015-02-13 13:53:33 -07:00
parent 78a407fe87
commit 2313a635a3
2 changed files with 3 additions and 80 deletions

View File

@@ -48,7 +48,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
host = (String) member.get("Name");
}
}
return new DefaultServiceInstance(service.getId(), host, service.getPort());
return new DefaultServiceInstance(service.getId(), host, service.getPort(), false);
}
@Override
@@ -56,13 +56,12 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
List<ServiceNode> nodes = catalogClient.getServiceNodes(serviceId);
List<ServiceInstance> instances = new ArrayList<>();
for (ServiceNode node : nodes) {
instances.add(new DefaultServiceInstance(serviceId, node.getNode(), node.getServicePort()));
instances.add(new DefaultServiceInstance(serviceId, node.getNode(), node.getServicePort(), false));
}
return instances;
}
@Override
public List<ServiceInstance> getAllInstances() {
List<ServiceInstance> instances = new ArrayList<>();
@@ -70,7 +69,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
List<ServiceNode> serviceNodes = catalogClient.getServiceNodes(serviceId);
if (serviceNodes != null) {
for (ServiceNode node : serviceNodes) {
instances.add(new DefaultServiceInstance(node.getServiceName(), node.getNode(), node.getServicePort()));
instances.add(new DefaultServiceInstance(node.getServiceName(), node.getNode(), node.getServicePort(), false));
}
}
}

View File

@@ -1,76 +0,0 @@
package org.springframework.cloud.consul.discovery;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.cloud.consul.client.CatalogClient;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.concurrent.ConcurrentHashMap;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
*/
public class ConsulLoadBalancerClient implements LoadBalancerClient {
private ConcurrentHashMap<String, ILoadBalancer> namedLoadBalancers = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, IClientConfig> namedClientConfigs = new ConcurrentHashMap<>();
@Autowired
CatalogClient catalogClient;
@Override
public ServiceInstance choose(String serviceId) {
ILoadBalancer lb = namedLoadBalancers.get(serviceId);
if (lb == null) {
IClientConfig config = namedClientConfigs.get(serviceId);
if (config == null) {
DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
clientConfig.setClientName(serviceId);
config = clientConfig;
namedClientConfigs.put(serviceId, clientConfig);
}
lb = LoadBalancerBuilder.<ConsulServer>newBuilder()
.withClientConfig(config)
//TODO: config to choose rules
.withRule(new AvailabilityFilteringRule())
//TODO: figure out ping
//.withPing()
.withDynamicServerList(new ConsulServerList(catalogClient, serviceId))
.buildDynamicServerListLoadBalancer();
namedLoadBalancers.put(serviceId, lb);
}
Server server = lb.chooseServer(null);
return new DefaultServiceInstance(server.getId(), server.getHost(), server.getPort());
}
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) {
try {
return request.apply(choose(serviceId));
} catch (Exception e) {
rethrowRuntimeException(e);
return null;
}
}
@Override
public URI reconstructURI(ServiceInstance instance, URI original) {
//TODO: move this pattern to a helper method
URI uri = UriComponentsBuilder.fromUri(original)
.host(instance.getHost())
.port(instance.getPort())
.build()
.toUri();
return uri;
}
}