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