use of org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class SimpleLoadBalanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache endpointsVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
if (endpointsVersionedCache.isEmpty()) {
asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule(), endpointsVersionedCache.name()));
return;
}
List<Endpoint> endpoints = endpointsVersionedCache.data();
AtomicInteger index = indexMap.computeIfAbsent(endpointsVersionedCache.name(), name -> {
LOGGER.info("Create loadBalancer for {}.", name);
return new AtomicInteger();
});
LOGGER.debug("invocation {} use discoveryGroup {}.", invocation.getMicroserviceQualifiedName(), endpointsVersionedCache.name());
int idx = Math.abs(index.getAndIncrement());
idx = idx % endpoints.size();
Endpoint endpoint = endpoints.get(idx);
invocation.setEndpoint(endpoint);
invocation.next(asyncResp);
}
use of org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class CseDiscoveryClient method getInstances.
@Override
public List<ServiceInstance> getInstances(final String serviceId) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
});
VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistryUtils.getAppId(), serviceId, DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<ServiceInstance> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return instances;
}
use of org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class ServiceCombServerList method getInitialListOfServers.
@Override
public List<Server> getInitialListOfServers() {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistryUtils.getAppId(), serviceId, DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<Server> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new Server(uri.getHostOrIp(), uri.getPort()));
}
}
return instances;
}
use of org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.
the class LoadbalanceHandler method getOrCreateLoadBalancer.
protected LoadBalancer getOrCreateLoadBalancer(Invocation invocation) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache serversVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
LoadBalancer loadBalancer = loadBalancerMap.computeIfAbsent(serversVersionedCache.name(), name -> {
return createLoadBalancer(invocation.getMicroserviceName(), name);
});
LOGGER.debug("invocation {} use loadBalancer {}.", invocation.getMicroserviceQualifiedName(), loadBalancer.getName());
loadBalancer.setServerList(serversVersionedCache.data());
return loadBalancer;
}
Aggregations