Search in sources :

Example 31 with MicroserviceInstances

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

the class TestRegistry method testDelegate.

@SuppressWarnings("deprecation")
@Test
public void testDelegate() {
    ServiceRegistry serviceRegistry = LocalServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    RegistryUtils.init();
    serviceRegistry.run();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    Assert.assertEquals(serviceRegistry, RegistryUtils.getServiceRegistry());
    Assert.assertEquals(serviceRegistry.getServiceRegistryClient(), RegistryUtils.getServiceRegistryClient());
    Microservice microservice = RegistryUtils.getMicroservice();
    Assert.assertEquals(serviceRegistry.getMicroservice(), microservice);
    Assert.assertEquals(serviceRegistry.getMicroserviceInstance(), RegistryUtils.getMicroserviceInstance());
    List<MicroserviceInstance> instanceList = RegistryUtils.findServiceInstance("default", "default", "0.0.1");
    Assert.assertEquals(1, instanceList.size());
    Assert.assertEquals(RegistryUtils.getMicroservice().getServiceId(), instanceList.get(0).getServiceId());
    instanceList = RegistryUtils.findServiceInstance("default", "notExists", "0.0.1");
    Assert.assertNull(instanceList);
    MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances("default", "default", "0.0.1");
    List<MicroserviceInstance> instanceLists = microserviceInstances.getInstancesResponse().getInstances();
    Assert.assertEquals(1, instanceLists.size());
    Assert.assertEquals(RegistryUtils.getMicroservice().getServiceId(), instanceLists.get(0).getServiceId());
    Map<String, String> properties = new HashMap<>();
    properties.put("k", "v");
    RegistryUtils.updateInstanceProperties(properties);
    Assert.assertEquals(properties, RegistryUtils.getMicroserviceInstance().getProperties());
    Assert.assertEquals(microservice, RegistryUtils.getMicroservice(microservice.getServiceId()));
    RegistryUtils.updateMicroserviceProperties(properties);
    Assert.assertEquals(properties, RegistryUtils.getMicroservice().getProperties());
    Assert.assertEquals("default", RegistryUtils.getAppId());
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) HashMap(java.util.HashMap) MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 32 with MicroserviceInstances

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

the class LocalServiceRegistryClientImplTest method findServiceInstance_noInstances.

@Test
public void findServiceInstance_noInstances() {
    List<MicroserviceInstance> result = registryClient.findServiceInstance("self", appId, microserviceName, DefinitionConst.VERSION_RULE_ALL);
    Assert.assertThat(result, Matchers.nullValue());
    MicroserviceInstances microserviceInstances = registryClient.findServiceInstances("self", appId, microserviceName, DefinitionConst.VERSION_RULE_ALL, null);
    Assert.assertThat(microserviceInstances.isMicroserviceNotExist(), Matchers.is(true));
    Assert.assertThat(microserviceInstances.getInstancesResponse(), Matchers.nullValue());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 33 with MicroserviceInstances

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

the class MicroserviceVersions method pullInstances.

public void pullInstances() {
    lastPullTime = System.currentTimeMillis();
    MicroserviceInstances microserviceInstances = findServiceInstances();
    lastPulledResult = microserviceInstances;
    if (microserviceInstances == null) {
        // will not do anything, consumers will use existing instances
        return;
    }
    if (microserviceInstances.isMicroserviceNotExist()) {
        // pulled failed, SC said target not exist
        waitingDelete = true;
        return;
    }
    if (null != revision && revision.equals(microserviceInstances.getRevision())) {
        return;
    }
    pulledInstances = microserviceInstances.getInstancesResponse().getInstances();
    pulledInstances.sort(Comparator.comparing(MicroserviceInstance::getInstanceId));
    String rev = microserviceInstances.getRevision();
    safeSetInstances(pulledInstances, rev);
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances)

Example 34 with MicroserviceInstances

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

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 35 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances 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)

Aggregations

MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)52 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)34 Test (org.junit.Test)22 FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)18 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)12 ArrayList (java.util.ArrayList)8 MockUp (mockit.MockUp)8 Before (org.junit.Before)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Enumeration (java.util.Enumeration)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2