Search in sources :

Example 21 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class TestServiceCombServerList method testServiceCombServerList.

@Test
public void testServiceCombServerList(@Injectable IClientConfig iClientConfig, @Mocked RegistryUtils registryUtils, @Mocked DiscoveryTree discoveryTree, @Injectable DiscoveryTreeNode versionedCache) {
    Map<String, MicroserviceInstance> servers = new HashMap<>();
    List<String> endpoints = new ArrayList<>();
    endpoints.add("rest://localhost:3333");
    endpoints.add("rest://localhost:4444");
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setServiceId("service1");
    instance1.setInstanceId("service1-instance1");
    instance1.setEndpoints(endpoints);
    servers.put("service1-instance1", instance1);
    new Expectations() {

        {
            iClientConfig.getClientName();
            result = "serviceId1";
            RegistryUtils.getAppId();
            result = "app";
            discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString);
            result = versionedCache;
            versionedCache.data();
            result = servers;
        }
    };
    ServiceCombServerList list = new ServiceCombServerList();
    list.initWithNiwsConfig(iClientConfig);
    List<Server> serverList = list.getInitialListOfServers();
    Assert.assertEquals(2, serverList.size());
    Assert.assertEquals(4444, serverList.get(1).getPort());
    Assert.assertEquals(serverList.size(), list.getUpdatedListOfServers().size());
}
Also used : Expectations(mockit.Expectations) Server(com.netflix.loadbalancer.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 22 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance 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 23 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance 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 24 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class ZoneAwareServerListFilterExt method getFilteredListOfServers.

@Override
public List<Server> getFilteredListOfServers(List<Server> list) {
    List<Server> result = new ArrayList<>();
    MicroserviceInstance myself = RegistryUtils.getMicroserviceInstance();
    boolean find = false;
    for (Server server : list) {
        CseServer cseServer = (CseServer) server;
        if (regionAndAZMatch(myself, cseServer.getInstance())) {
            result.add(cseServer);
            find = true;
        }
    }
    if (!find) {
        for (Server server : list) {
            CseServer cseServer = (CseServer) server;
            if (regionMatch(myself, cseServer.getInstance())) {
                result.add(cseServer);
                find = true;
            }
        }
    }
    if (!find) {
        result = list;
    }
    return result;
}
Also used : CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) CseServer(org.apache.servicecomb.loadbalance.CseServer) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 25 with MicroserviceInstance

use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.

the class TestZoneAwareServerListFilterExt method testZoneAwareServerListFilterExt.

@Test
public void testZoneAwareServerListFilterExt(@Mocked RegistryUtils registryUtils) {
    MicroserviceInstance myself = new MicroserviceInstance();
    DataCenterInfo info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    myself.setDataCenterInfo(info);
    MicroserviceInstance allmatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    allmatchInstance.setDataCenterInfo(info);
    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone2");
    regionMatchInstance.setDataCenterInfo(info);
    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region2");
    info.setAvailableZone("test-zone2");
    noneMatchInstance.setDataCenterInfo(info);
    new Expectations() {

        {
            RegistryUtils.getMicroserviceInstance();
            result = myself;
        }
    };
    ZoneAwareServerListFilterExt filter = new ZoneAwareServerListFilterExt();
    List<Server> servers = new ArrayList<>();
    CseServer noneMatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "noneMatchServer";
        }

        @Mock
        public String getHost() {
            return "noneMatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return noneMatchInstance;
        }
    }.getMockInstance();
    CseServer regionMatchregionMatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "regionMatchregionMatchServer";
        }

        @Mock
        public String getHost() {
            return "regionMatchregionMatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return regionMatchInstance;
        }
    }.getMockInstance();
    CseServer allmatchServer = new MockUp<CseServer>() {

        @Mock
        public String toString() {
            return "allmatchServer";
        }

        @Mock
        public String getHost() {
            return "allmatchServer";
        }

        @Mock
        public MicroserviceInstance getInstance() {
            return allmatchInstance;
        }
    }.getMockInstance();
    servers.add(noneMatchServer);
    List<Server> result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), noneMatchServer);
    servers.add(regionMatchregionMatchServer);
    result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), regionMatchregionMatchServer);
    servers.add(allmatchServer);
    result = filter.getFilteredListOfServers(servers);
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0), allmatchServer);
}
Also used : Expectations(mockit.Expectations) CseServer(org.apache.servicecomb.loadbalance.CseServer) Server(com.netflix.loadbalancer.Server) CseServer(org.apache.servicecomb.loadbalance.CseServer) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) DataCenterInfo(org.apache.servicecomb.serviceregistry.api.registry.DataCenterInfo) Mock(mockit.Mock) Test(org.junit.Test)

Aggregations

MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)56 Test (org.junit.Test)30 Expectations (mockit.Expectations)16 ArrayList (java.util.ArrayList)15 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)15 HashMap (java.util.HashMap)11 MicroserviceInstances (org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances)7 Before (org.junit.Before)5 EventBus (com.google.common.eventbus.EventBus)4 Server (com.netflix.loadbalancer.Server)4 BootEvent (org.apache.servicecomb.core.BootListener.BootEvent)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4 Subscribe (com.google.common.eventbus.Subscribe)3 Map (java.util.Map)3 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)3 MicroserviceInstanceRegisterTask (org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask)3 URI (java.net.URI)2 List (java.util.List)2 RSAProviderTokenManager (org.apache.servicecomb.authentication.provider.RSAProviderTokenManager)2 SchemaListenerManager (org.apache.servicecomb.core.definition.loader.SchemaListenerManager)2