Search in sources :

Example 6 with VersionedCache

use of org.apache.servicecomb.foundation.common.cache.VersionedCache 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 7 with VersionedCache

use of org.apache.servicecomb.foundation.common.cache.VersionedCache 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 8 with VersionedCache

use of org.apache.servicecomb.foundation.common.cache.VersionedCache 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)

Example 9 with VersionedCache

use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.

the class TestLoadbalanceHandler method getOrCreateLoadBalancer.

@Test
public void getOrCreateLoadBalancer() throws Exception {
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("id");
    instance.getEndpoints().add("rest://localhost:8080");
    Map<String, MicroserviceInstance> instanceMap = new HashMap<>();
    instanceMap.put(instance.getInstanceId(), instance);
    VersionedCache instanceVersionedCache = new VersionedCache().autoCacheVersion().name("instanceCache").data(instanceMap);
    new Expectations() {

        {
            invocation.getConfigTransportName();
            result = "rest";
            instanceCacheManager.getOrCreateVersionedCache(anyString, anyString, anyString);
            result = instanceVersionedCache;
        }
    };
    LoadBalancer lb = handler.getOrCreateLoadBalancer(invocation);
    Assert.assertEquals(2, lb.getFilterSize());
    Assert.assertEquals("instanceCache/rest", lb.getName());
    Assert.assertEquals("[rest://localhost:8080]", Deencapsulation.getField(lb, "serverList").toString());
}
Also used : Expectations(mockit.Expectations) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

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