Search in sources :

Example 11 with MicroserviceInstance

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

the class TestMicroServiceInstance method setUp.

@Before
public void setUp() throws Exception {
    oMicroserviceInstance = new MicroserviceInstance();
    oMapProperties = new HashMap<>();
    oListEndpoints = new ArrayList<>();
    oMockHealthCheck = Mockito.mock(HealthCheck.class);
}
Also used : HealthCheck(org.apache.servicecomb.registry.api.registry.HealthCheck) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Before(org.junit.Before)

Example 12 with MicroserviceInstance

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

the class TestMicroServiceInstance method testCreateMicroserviceInstanceFromFile.

@Test
public void testCreateMicroserviceInstanceFromFile() {
    AbstractConfiguration config = ConfigUtil.createLocalConfig();
    ConcurrentCompositeConfiguration configuration = new ConcurrentCompositeConfiguration();
    configuration.addConfiguration(config);
    ConfigurationManager.install(configuration);
    MicroserviceInstance instance = MicroserviceInstance.createFromDefinition(config);
    Assert.assertEquals(instance.getDataCenterInfo().getName(), "myDC");
    Assert.assertEquals(instance.getDataCenterInfo().getRegion(), "my-Region");
    Assert.assertEquals(instance.getDataCenterInfo().getAvailableZone(), "my-Zone");
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration) Test(org.junit.Test)

Example 13 with MicroserviceInstance

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

the class InstanceCacheChecker method check.

protected InstanceCacheResult check(MicroserviceVersions microserviceVersions) {
    InstanceCacheResult instanceCacheResult = new InstanceCacheResult();
    instanceCacheResult.setAppId(microserviceVersions.getAppId());
    instanceCacheResult.setMicroserviceName(microserviceVersions.getMicroserviceName());
    instanceCacheResult.setPulledInstances(microserviceVersions.getPulledInstances());
    MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances(microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), DefinitionConst.VERSION_RULE_ALL);
    if (microserviceInstances == null) {
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail("failed to find instances from service center");
        return instanceCacheResult;
    }
    if (microserviceInstances.isMicroserviceNotExist()) {
        // no problem, will be deleted from MicroserviceManager in next pull
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail("microservice is not exist anymore, will be deleted from memory in next pull");
        return instanceCacheResult;
    }
    if (!Objects.equals(microserviceInstances.getRevision(), microserviceVersions.getRevision())) {
        // maybe not pull, wait for next pull we get the same revision
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail(String.format("revision is different, will be synchronized in next pull. local revision=%s, remote revision=%s", microserviceVersions.getRevision(), microserviceInstances.getRevision()));
        // better to change revision and more likely to find the correct instances in next pull.
        microserviceVersions.setRevision(null);
        return instanceCacheResult;
    }
    // compare all instances
    List<MicroserviceInstance> remoteInstances = microserviceInstances.getInstancesResponse().getInstances();
    remoteInstances.sort(Comparator.comparing(MicroserviceInstance::getInstanceId));
    String local = Json.encode(microserviceVersions.getPulledInstances());
    String remote = Json.encode(remoteInstances);
    if (local.equals(remote)) {
        LOGGER.info("instance cache match. appId={}, microservice={}.\n" + "current cache: {}\n", microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), remoteInstances.toString());
        instanceCacheResult.setStatus(Status.NORMAL);
        return instanceCacheResult;
    }
    LOGGER.error("instance cache not match. appId={}, microservice={}.\n" + "local cache: {}\n" + "remote cache: {}", microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), microserviceVersions.getPulledInstances().toString(), remoteInstances.toString());
    instanceCacheResult.setStatus(Status.ABNORMAL);
    instanceCacheResult.setDetail("instance cache not match");
    // auto fix, will do a full pull request when invoke MicroserviceVersions.pullInstances
    microserviceVersions.setRevision(null);
    return instanceCacheResult;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 14 with MicroserviceInstance

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

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 15 with MicroserviceInstance

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

the class AbstractServiceRegistry method unregisterInstance.

public boolean unregisterInstance() {
    MicroserviceInstance microserviceInstance = microservice.getInstance();
    if (microserviceInstance.getInstanceId() == null || microserviceInstance.getServiceId() == null) {
        return true;
    }
    boolean result = srClient.unregisterMicroserviceInstance(microserviceInstance.getServiceId(), microserviceInstance.getInstanceId());
    if (!result) {
        LOGGER.error("Unregister microservice instance failed. microserviceId={} instanceId={}", microserviceInstance.getServiceId(), microserviceInstance.getInstanceId());
        return false;
    }
    LOGGER.info("Unregister microservice instance success. microserviceId={} instanceId={}", microserviceInstance.getServiceId(), microserviceInstance.getInstanceId());
    return true;
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

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