Search in sources :

Example 1 with FindInstancesResponse

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

the class LocalRegistryStore method findServiceInstances.

// local registry do not care about version and revision
public MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule, String revision) {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    FindInstancesResponse findInstancesResponse = new FindInstancesResponse();
    List<MicroserviceInstance> instances = new ArrayList<>();
    Collectors.toList();
    microserviceInstanceMap.values().forEach(allInstances -> allInstances.values().stream().filter(aInstance -> {
        Microservice service = microserviceMap.get(aInstance.getServiceId());
        return service.getAppId().equals(appId) && service.getServiceName().equals(serviceName);
    }).forEach(item -> instances.add(item)));
    if (instances.isEmpty()) {
        microserviceInstances.setMicroserviceNotExist(true);
    } else {
        findInstancesResponse.setInstances(instances);
        microserviceInstances.setMicroserviceNotExist(false);
        microserviceInstances.setInstancesResponse(findInstancesResponse);
    }
    return microserviceInstances;
}
Also used : BeanUtils(org.apache.servicecomb.foundation.common.utils.BeanUtils) MicroserviceFactory(org.apache.servicecomb.registry.api.registry.MicroserviceFactory) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Enumeration(java.util.Enumeration) URL(java.net.URL) SwaggerGenerator(org.apache.servicecomb.swagger.generator.SwaggerGenerator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) HashMap(java.util.HashMap) SwaggerUtils(org.apache.servicecomb.swagger.SwaggerUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) JvmUtils(org.apache.servicecomb.foundation.common.utils.JvmUtils) Instance(org.apache.servicecomb.localregistry.RegistryBean.Instance) ArrayList(java.util.ArrayList) YAMLUtil(org.apache.servicecomb.config.YAMLUtil) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) List(java.util.List) Map(java.util.Map) Entry(java.util.Map.Entry) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse) InputStream(java.io.InputStream) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) 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)

Example 2 with FindInstancesResponse

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

the class MicroserviceStore method findServiceInstances.

public MicroserviceInstances findServiceInstances(String revision) {
    if (instancesRevision.equals(revision)) {
        return new MicroserviceInstances().setRevision(instancesRevision).setNeedRefresh(false);
    }
    List<MicroserviceInstance> instances = instancesById.values().stream().map(InstanceStore::getInstance).collect(Collectors.toList());
    FindInstancesResponse response = new FindInstancesResponse().setInstances(instances);
    return new MicroserviceInstances().setRevision(instancesRevision).setInstancesResponse(response);
}
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)

Example 3 with FindInstancesResponse

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

the class TestInstanceCacheCheckerMock method createFindServiceInstancesResult.

private Holder<MicroserviceInstances> createFindServiceInstancesResult() {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    microserviceInstances.setNeedRefresh(true);
    microserviceInstances.setRevision("first");
    FindInstancesResponse findInstancesResponse = new FindInstancesResponse();
    findInstancesResponse.setInstances(new ArrayList<>());
    microserviceInstances.setInstancesResponse(findInstancesResponse);
    Holder<MicroserviceInstances> findHolder = new Holder<>();
    findHolder.value = microserviceInstances;
    return findHolder;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) Holder(org.apache.servicecomb.foundation.common.Holder) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse)

Example 4 with FindInstancesResponse

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

the class RefreshableMicroserviceCacheTest method refresh.

@Test
public void refresh() {
    ArrayList<MicroserviceInstance> instances = new ArrayList<>();
    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("rev0");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(instances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
    // at the beginning, no instances in cache
    List<MicroserviceInstance> cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(0, cachedInstances.size());
    Assert.assertNull(microserviceCache.getRevisionId());
    // find 1 instance from sc
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    instances.add(microserviceInstance);
    microserviceInstance.setInstanceId("instanceId00");
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(1, cachedInstances.size());
    MicroserviceInstance instance = cachedInstances.iterator().next();
    Assert.assertEquals("instanceId00", instance.getInstanceId());
    Assert.assertEquals("rev0", microserviceCache.getRevisionId());
    // 2nd time, find 2 instances, one of them is the old instance
    MicroserviceInstance microserviceInstance1 = new MicroserviceInstance();
    instances.add(microserviceInstance1);
    microserviceInstance1.setInstanceId("instanceId01");
    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.assertEquals("rev0", params[4]);
        MicroserviceInstances microserviceInstances = new MicroserviceInstances();
        microserviceInstances.setNeedRefresh(true);
        microserviceInstances.setRevision("rev1");
        microserviceInstances.setMicroserviceNotExist(false);
        FindInstancesResponse instancesResponse = new FindInstancesResponse();
        instancesResponse.setInstances(instances);
        microserviceInstances.setInstancesResponse(instancesResponse);
        return microserviceInstances;
    };
    microserviceCache.refresh();
    Assert.assertEquals(MicroserviceCacheStatus.REFRESHED, microserviceCache.getStatus());
    cachedInstances = microserviceCache.getInstances();
    Assert.assertEquals(2, cachedInstances.size());
    Assert.assertEquals("instanceId00", cachedInstances.get(0).getInstanceId());
    Assert.assertEquals("instanceId01", cachedInstances.get(1).getInstanceId());
}
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 5 with FindInstancesResponse

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

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)

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