updates to new s-c-commons stuff, remove unused class
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user