Search in sources :

Example 16 with FindInstancesResponse

use of org.apache.servicecomb.registry.api.registry.FindInstancesResponse in project java-chassis by ServiceComb.

the class RegistryUtils method convertCacheToMicroserviceInstances.

/**
 * for compatibility
 */
public static MicroserviceInstances convertCacheToMicroserviceInstances(MicroserviceCache microserviceCache) {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    switch(microserviceCache.getStatus()) {
        case SERVICE_NOT_FOUND:
            microserviceInstances.setMicroserviceNotExist(true);
            microserviceInstances.setNeedRefresh(false);
            microserviceInstances.setRevision("");
            microserviceInstances.setInstancesResponse(null);
            return microserviceInstances;
        case NO_CHANGE:
            microserviceInstances.setMicroserviceNotExist(false);
            microserviceInstances.setNeedRefresh(false);
            microserviceInstances.setRevision(microserviceCache.getRevisionId());
            return microserviceInstances;
        case REFRESHED:
            microserviceInstances.setMicroserviceNotExist(false);
            microserviceInstances.setNeedRefresh(true);
            microserviceInstances.setRevision(microserviceCache.getRevisionId());
            FindInstancesResponse findInstancesResponse = new FindInstancesResponse();
            findInstancesResponse.setInstances(new ArrayList<>(microserviceCache.getInstances()));
            microserviceInstances.setInstancesResponse(findInstancesResponse);
            return microserviceInstances;
        default:
            return null;
    }
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse)

Example 17 with FindInstancesResponse

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

the class RefreshableMicroserviceCacheTest method forceRefresh.

@Test
public void forceRefresh() {
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setInstanceId("instanceId00");
    ArrayList<MicroserviceInstance> instances = new ArrayList<>();
    instances.add(microserviceInstance);
    findServiceInstancesOprHolder.value = params -> {
        Assert.assertEquals("consumerId", params[0]);
        Assert.assertEquals("app", params[1]);
        Assert.assertEquals("svc", params[2]);
        Assert.assertEquals("0.0.0.0+", params[3]);
        Assert.assertNull(params[4]);
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev2");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(instances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
    microserviceCache.revisionId = "rev";
    microserviceCache.forceRefresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    List<MicroserviceInstance> cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(1, cachedInstances.size());
    MicroserviceInstance instance = cachedInstances.iterator().next();
    Assert.assertEquals("instanceId00", instance.getInstanceId());
    Assert.assertEquals("rev2", microserviceCache.getRevisionId());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) Test(org.junit.Test)

Example 18 with FindInstancesResponse

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

the class RefreshableMicroserviceCacheTest method setUp.

@Before
public void setUp() throws Exception {
    srClient = new MockUp<ServiceRegistryClient>() {

        @Mock
        MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
            return findServiceInstancesOprHolder.value.apply(new Object[] { consumerId, appId, serviceName, versionRule, revision });
        }
    }.getMockInstance();
    consumerService = new Microservice();
    consumerService.setServiceId("consumerId");
    microserviceCache = new RefreshableMicroserviceCache(consumerService, MicroserviceCacheKey.builder().env("env").appId("app").serviceName("svc").build(), srClient, false);
    findServiceInstancesOprHolder.value = params -> {
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev0");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(pulledInstances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MockUp(mockit.MockUp) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) Before(org.junit.Before)

Example 19 with FindInstancesResponse

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

the class LocalServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String selfMicroserviceId, String appId, String serviceName, String strVersionRule, String revision) {
    int currentRevision = this.revision.get();
    List<MicroserviceInstance> allInstances = new ArrayList<>();
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    FindInstancesResponse response = new FindInstancesResponse();
    if (revision != null && currentRevision == Integer.parseInt(revision)) {
        microserviceInstances.setNeedRefresh(false);
        return microserviceInstances;
    }
    microserviceInstances.setRevision(String.valueOf(currentRevision));
    VersionRule versionRule = VersionRuleUtils.getOrCreate(strVersionRule);
    Microservice latestMicroservice = findLatest(appId, serviceName, versionRule);
    if (latestMicroservice == null) {
        microserviceInstances.setMicroserviceNotExist(true);
        return microserviceInstances;
    }
    Version latestVersion = VersionUtils.getOrCreate(latestMicroservice.getVersion());
    for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
        Microservice microservice = entry.getValue();
        if (!isSameMicroservice(microservice, appId, serviceName)) {
            continue;
        }
        Version version = VersionUtils.getOrCreate(entry.getValue().getVersion());
        if (!versionRule.isMatch(version, latestVersion)) {
            continue;
        }
        Map<String, MicroserviceInstance> instances = microserviceInstanceMap.get(entry.getValue().getServiceId());
        allInstances.addAll(instances.values());
    }
    response.setInstances(allInstances);
    microserviceInstances.setInstancesResponse(response);
    return microserviceInstances;
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) VersionRule(org.apache.servicecomb.registry.version.VersionRule)

Example 20 with FindInstancesResponse

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

the class TestMicroserviceInstances method setUp.

@Before
public void setUp() throws Exception {
    microserviceInstances = new MicroserviceInstances();
    findInstancesResponse = new FindInstancesResponse();
    instances = new ArrayList<>();
    instances.add(Mockito.mock(MicroserviceInstance.class));
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) Before(org.junit.Before)

Aggregations

FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)22 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)18 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)16 ArrayList (java.util.ArrayList)8 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)6 Before (org.junit.Before)6 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors (java.util.stream.Collectors)2 MockUp (mockit.MockUp)2