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;
}
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;
}
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);
}
}
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;
}
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)));
}
Aggregations