Search in sources :

Example 11 with Version

use of org.apache.servicecomb.foundation.common.Version in project java-chassis by ServiceComb.

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) {
        microserviceInstances.setMicroserviceNotExist(true);
        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.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version) 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) VersionRule(org.apache.servicecomb.registry.version.VersionRule)

Example 12 with Version

use of org.apache.servicecomb.foundation.common.Version in project java-chassis by ServiceComb.

the class MockMicroserviceVersions method findInstances.

public List<MicroserviceInstance> findInstances(String... strVersions) {
    List<String> unifyVersions = Arrays.stream(strVersions).map(version -> new Version(version).getVersion()).collect(Collectors.toList());
    List<MicroserviceInstance> instances = new ArrayList<>();
    for (MicroserviceInstance instance : mockedInstances) {
        Microservice microservice = mockedMicroservices.get(instance.getServiceId());
        for (String version : unifyVersions) {
            if (version.equals(microservice.getVersion())) {
                instances.add(instance);
                break;
            }
        }
    }
    return instances;
}
Also used : Arrays(java.util.Arrays) MockUp(mockit.MockUp) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Version(org.apache.servicecomb.foundation.common.Version) Matchers(org.hamcrest.Matchers) HashMap(java.util.HashMap) DiscoveryManager(org.apache.servicecomb.registry.DiscoveryManager) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) AppManager(org.apache.servicecomb.registry.consumer.AppManager) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) Map(java.util.Map) MicroserviceVersions(org.apache.servicecomb.registry.consumer.MicroserviceVersions) Mock(mockit.Mock) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) Assert(org.junit.Assert) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 13 with Version

use of org.apache.servicecomb.foundation.common.Version in project java-chassis by ServiceComb.

the class MockMicroserviceVersions method addMock.

public void addMock(String version, int instanceCount) {
    version = new Version(version).getVersion();
    Microservice microservice = new Microservice();
    microservice.setAppId(getAppId());
    microservice.setServiceName(getMicroserviceName());
    microservice.setServiceId("sid-" + version);
    microservice.setVersion(version);
    mockedMicroservices.put(microservice.getServiceId(), microservice);
    for (int idx = 0; idx < instanceCount; idx++) {
        MicroserviceInstance instance = new MicroserviceInstance();
        instance.setServiceId(microservice.getServiceId());
        instance.setInstanceId(String.format("iid-%s-%d", version, idx));
        mockedInstances.add(instance);
    }
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version) MicroserviceVersion(org.apache.servicecomb.registry.consumer.MicroserviceVersion) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 14 with Version

use of org.apache.servicecomb.foundation.common.Version in project java-chassis by ServiceComb.

the class LocalServiceRegistryClientImpl method findLatest.

protected Microservice findLatest(String appId, String serviceName, VersionRule versionRule) {
    Version latestVersion = null;
    Microservice latest = null;
    for (Entry<String, Microservice> entry : microserviceIdMap.entrySet()) {
        Microservice microservice = entry.getValue();
        if (!isSameMicroservice(microservice, appId, serviceName)) {
            continue;
        }
        Version version = VersionUtils.getOrCreate(microservice.getVersion());
        if (!versionRule.isAccept(version)) {
            continue;
        }
        if (latestVersion == null || version.compareTo(latestVersion) > 0) {
            latestVersion = version;
            latest = microservice;
        }
    }
    return latest;
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Version(org.apache.servicecomb.foundation.common.Version)

Example 15 with Version

use of org.apache.servicecomb.foundation.common.Version in project java-chassis by ServiceComb.

the class TestVersion method compareTo.

@Test
public void compareTo() {
    version = new Version(s1, s1, s1, s1);
    Assert.assertEquals(0, version.compareTo(version));
    Assert.assertEquals(0, version.compareTo(new Version(s1, s1, s1, s1)));
    Assert.assertEquals(-1, version.compareTo(new Version(s1, s1, s2, s1)));
    Assert.assertEquals(-1, version.compareTo(new Version(s1, s2, s1, s1)));
    Assert.assertEquals(-1, version.compareTo(new Version(s2, s1, s1, s1)));
    Assert.assertEquals(1, version.compareTo(new Version((short) 0, Short.MAX_VALUE, Short.MAX_VALUE, Short.MAX_VALUE)));
}
Also used : Version(org.apache.servicecomb.foundation.common.Version) Test(org.junit.Test)

Aggregations

Version (org.apache.servicecomb.foundation.common.Version)28 Test (org.junit.Test)20 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)4 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)3 ArrayList (java.util.ArrayList)2 MicroserviceVersion (org.apache.servicecomb.registry.consumer.MicroserviceVersion)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Mock (mockit.Mock)1 MockUp (mockit.MockUp)1 EnvironmentConfiguration (org.apache.commons.configuration.EnvironmentConfiguration)1 DiscoveryManager (org.apache.servicecomb.registry.DiscoveryManager)1 FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)1 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)1 AppManager (org.apache.servicecomb.registry.consumer.AppManager)1 MicroserviceVersionRule (org.apache.servicecomb.registry.consumer.MicroserviceVersionRule)1 MicroserviceVersions (org.apache.servicecomb.registry.consumer.MicroserviceVersions)1