Search in sources :

Example 41 with MicroserviceInstance

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

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.empty());
    MicroserviceInstances microserviceInstances = registryClient.findServiceInstances("self", appId, microserviceName, DefinitionConst.VERSION_RULE_ALL, null);
    List<MicroserviceInstance> results = microserviceInstances.getInstancesResponse().getInstances();
    Assert.assertThat(results, Matchers.empty());
}
Also used : MicroserviceInstances(org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 42 with MicroserviceInstance

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

the class TestRegistry method testDelegate.

@Test
public void testDelegate() {
    ServiceRegistry serviceRegistry = ServiceRegistryFactory.createLocal();
    serviceRegistry.init();
    serviceRegistry.run();
    RegistryUtils.setServiceRegistry(serviceRegistry);
    Assert.assertEquals(serviceRegistry, RegistryUtils.getServiceRegistry());
    Assert.assertEquals(serviceRegistry.getServiceRegistryClient(), RegistryUtils.getServiceRegistryClient());
    Assert.assertEquals(serviceRegistry.getInstanceCacheManager(), RegistryUtils.getInstanceCacheManager());
    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());
    MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances("default", "default", "0.0.1", "0");
    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()));
    Assert.assertEquals("default", RegistryUtils.getAppId());
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) HashMap(java.util.HashMap) MicroserviceInstances(org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 43 with MicroserviceInstance

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

the class LocalServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String selfMicroserviceId, String appId, String serviceName, String strVersionRule, String revision) {
    int currentRevision = this.revision.get();
    List<MicroserviceInstance> allInstances = new ArrayList<>();
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    FindInstancesResponse response = new FindInstancesResponse();
    if (revision != null && currentRevision == Integer.parseInt(revision)) {
        microserviceInstances.setNeedRefresh(false);
        return microserviceInstances;
    }
    microserviceInstances.setRevision(String.valueOf(currentRevision));
    VersionRule versionRule = VersionRuleUtils.getOrCreate(strVersionRule);
    Microservice latestMicroservice = findLatest(appId, serviceName, versionRule);
    if (latestMicroservice == null) {
        response.setInstances(allInstances);
        microserviceInstances.setInstancesResponse(response);
        return microserviceInstances;
    }
    Version latestVersion = VersionUtils.getOrCreate(latestMicroservice.getVersion());
    for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
        Microservice microservice = entry.getValue();
        if (!isSameMicroservice(microservice, appId, serviceName)) {
            continue;
        }
        Version version = VersionUtils.getOrCreate(entry.getValue().getVersion());
        if (!versionRule.isMatch(version, latestVersion)) {
            continue;
        }
        Map<String, MicroserviceInstance> instances = microserviceInstanceMap.get(entry.getValue().getServiceId());
        allInstances.addAll(instances.values());
    }
    response.setInstances(allInstances);
    microserviceInstances.setInstancesResponse(response);
    return microserviceInstances;
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) Version(org.apache.servicecomb.serviceregistry.version.Version) MicroserviceInstances(org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.serviceregistry.api.response.FindInstancesResponse) VersionRule(org.apache.servicecomb.serviceregistry.version.VersionRule)

Example 44 with MicroserviceInstance

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

the class MicroserviceVersions method setInstances.

private void setInstances(List<MicroserviceInstance> pulledInstances, String rev) {
    synchronized (lock) {
        instances = pulledInstances.stream().filter(instance -> {
            return MicroserviceInstanceStatus.UP.equals(instance.getStatus());
        }).collect(Collectors.toList());
        for (MicroserviceInstance instance : instances) {
            // ensure microserviceVersion exists
            versions.computeIfAbsent(instance.getServiceId(), microserviceId -> {
                MicroserviceVersion microserviceVersion = appManager.getMicroserviceVersionFactory().create(microserviceName, microserviceId);
                for (MicroserviceVersionRule microserviceVersionRule : versionRules.values()) {
                    microserviceVersionRule.addMicroserviceVersion(microserviceVersion);
                }
                return microserviceVersion;
            });
        }
        for (MicroserviceVersionRule microserviceVersionRule : versionRules.values()) {
            microserviceVersionRule.setInstances(instances);
        }
        revision = rev;
    }
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Example 45 with MicroserviceInstance

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

the class TestOperationInstancesDiscoveryFilter method createInstance.

private MicroserviceInstance createInstance(String serviceId) {
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId(UUID.randomUUID().toString());
    instance.setServiceId(serviceId);
    return instance;
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Aggregations

MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)56 Test (org.junit.Test)30 Expectations (mockit.Expectations)16 ArrayList (java.util.ArrayList)15 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)15 HashMap (java.util.HashMap)11 MicroserviceInstances (org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances)7 Before (org.junit.Before)5 EventBus (com.google.common.eventbus.EventBus)4 Server (com.netflix.loadbalancer.Server)4 BootEvent (org.apache.servicecomb.core.BootListener.BootEvent)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4 Subscribe (com.google.common.eventbus.Subscribe)3 Map (java.util.Map)3 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)3 MicroserviceInstanceRegisterTask (org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask)3 URI (java.net.URI)2 List (java.util.List)2 RSAProviderTokenManager (org.apache.servicecomb.authentication.provider.RSAProviderTokenManager)2 SchemaListenerManager (org.apache.servicecomb.core.definition.loader.SchemaListenerManager)2