Search in sources :

Example 1 with DiscoveryContext

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);
}
Also used : DiscoveryContext(org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Endpoint(org.apache.servicecomb.core.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(org.apache.servicecomb.core.Endpoint)

Example 2 with DiscoveryContext

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;
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) DiscoveryContext(org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) DiscoveryTree(org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree)

Example 3 with DiscoveryContext

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;
}
Also used : DiscoveryContext(org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject)

Example 4 with DiscoveryContext

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;
}
Also used : DiscoveryContext(org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache)

Aggregations

VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)4 DiscoveryContext (org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext)4 ArrayList (java.util.ArrayList)2 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)2 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)2 Server (com.netflix.loadbalancer.Server)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 DiscoveryTree (org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree)1 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1