Search in sources :

Example 21 with MicroserviceInstances

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

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)

Example 22 with MicroserviceInstances

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

the class ServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RequestParam requestParam = new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("global", "true").addQueryParam("version", versionRule);
    if (RegistryUtils.getMicroservice().getEnvironment() != null) {
        requestParam.addQueryParam("env", RegistryUtils.getMicroservice().getEnvironment());
    }
    if (consumerId != null) {
        requestParam.addHeader("X-ConsumerId", consumerId);
    }
    if (revision != null) {
        requestParam.addQueryParam("rev", revision);
    }
    restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances));
    try {
        countDownLatch.await();
        if (!microserviceInstances.isNeedRefresh()) {
            return microserviceInstances;
        }
        if (microserviceInstances.getInstancesResponse() == null) {
            // error
            return null;
        }
        List<MicroserviceInstance> list = microserviceInstances.getInstancesResponse().getInstances();
        if (list == null) {
            microserviceInstances.getInstancesResponse().setInstances(new ArrayList<>());
        }
        return microserviceInstances;
    } catch (Exception e) {
        LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
    }
    return null;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with MicroserviceInstances

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

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

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

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

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

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)

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