Search in sources :

Example 16 with MicroserviceInstance

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

the class RefreshableMicroserviceCache method pullInstance.

void pullInstance(String revisionId) {
    MicroserviceInstances serviceInstances = pullInstanceFromServiceCenter(revisionId);
    if (serviceInstances == null) {
        LOGGER.error("Can not find any instances from service center due to previous errors. service={}/{}/{}", key.getAppId(), key.getServiceName(), key.getVersionRule());
        setStatus(MicroserviceCacheStatus.CLIENT_ERROR);
        return;
    }
    if (serviceInstances.isMicroserviceNotExist()) {
        setStatus(MicroserviceCacheStatus.SERVICE_NOT_FOUND);
        return;
    }
    if (!serviceInstances.isNeedRefresh()) {
        LOGGER.debug("instances revision is not changed, service={}/{}/{}", key.getAppId(), key.getServiceName(), key.getVersionRule());
        setStatus(MicroserviceCacheStatus.NO_CHANGE);
        return;
    }
    List<MicroserviceInstance> instances = serviceInstances.getInstancesResponse().getInstances();
    LOGGER.info("find instances[{}] from service center success. service={}/{}/{}, old revision={}, new revision={}", instances.size(), key.getAppId(), key.getServiceName(), key.getVersionRule(), this.revisionId, serviceInstances.getRevision());
    for (MicroserviceInstance instance : instances) {
        LOGGER.info("service id={}, instance id={}, endpoints={}", instance.getServiceId(), instance.getInstanceId(), instance.getEndpoints());
    }
    safeSetInstances(instances, serviceInstances.getRevision());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 17 with MicroserviceInstance

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

the class RefreshableMicroserviceCache method setInstances.

private void setInstances(List<MicroserviceInstance> pulledInstances, String rev) {
    Set<MicroserviceInstance> mergedInstances = mergeInstances(pulledInstances);
    LOGGER.debug("actually set instances[{}] for {}", mergedInstances.size(), key.plainKey());
    for (MicroserviceInstance mergedInstance : mergedInstances) {
        LOGGER.debug("serviceId={}, instanceId={}, endpoints={}", mergedInstance.getServiceId(), mergedInstance.getInstanceId(), mergedInstance.getEndpoints());
    }
    instances = Collections.unmodifiableList(new ArrayList<>(mergedInstances));
    revisionId = rev;
}
Also used : ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 18 with MicroserviceInstance

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

the class RegistryUtilsTest method convertCacheToMicroserviceInstances.

@Test
public void convertCacheToMicroserviceInstances() {
    MockedMicroserviceCache microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.CLIENT_ERROR);
    MicroserviceInstances microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.SETTING_CACHE_ERROR);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.INIT);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.SERVICE_NOT_FOUND);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertTrue(microserviceInstances.isMicroserviceNotExist());
    Assert.assertFalse(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("", microserviceInstances.getRevision());
    Assert.assertNull(microserviceInstances.getInstancesResponse());
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.REFRESHED);
    microserviceCache.setRevisionId("0166f3c18702617d5e55cf911e4e412cc8760dab");
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceCache.setInstances(Collections.singletonList(microserviceInstance));
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertFalse(microserviceInstances.isMicroserviceNotExist());
    Assert.assertTrue(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("0166f3c18702617d5e55cf911e4e412cc8760dab", microserviceInstances.getRevision());
    Assert.assertEquals(1, microserviceInstances.getInstancesResponse().getInstances().size());
    Assert.assertSame(microserviceInstance, microserviceInstances.getInstancesResponse().getInstances().get(0));
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.NO_CHANGE);
    microserviceCache.setRevisionId("0166f3c18702617d5e55cf911e4e412cc8760dab");
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertFalse(microserviceInstances.isMicroserviceNotExist());
    Assert.assertFalse(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("0166f3c18702617d5e55cf911e4e412cc8760dab", microserviceInstances.getRevision());
    Assert.assertNull(microserviceInstances.getInstancesResponse());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) MockedMicroserviceCache(org.apache.servicecomb.serviceregistry.registry.cache.MockedMicroserviceCache) Test(org.junit.Test)

Example 19 with MicroserviceInstance

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

the class TestConsumers method registryMicroserviceMapping.

@Test
public void registryMicroserviceMapping() {
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    RegistrationManager.INSTANCE.registerMicroserviceMapping("3rd", "1.0.0", Arrays.asList(microserviceInstance), Hello.class);
    MicroserviceVersionRule microserviceVersionRule = appManager.getOrCreateMicroserviceVersionRule(appId, "3rd", "0+");
    Assert.assertThat(microserviceVersionRule.getInstances().values(), Matchers.contains(microserviceInstance));
}
Also used : MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 20 with MicroserviceInstance

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

Aggregations

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12