use of io.servicecomb.serviceregistry.client.ClientException in project java-chassis by ServiceComb.
the class RegistryUtils method heartbeat.
public static HeartbeatResponse heartbeat() {
Timer timer = Timer.newForeverTimer();
HeartbeatResponse response;
while (true) {
response = srClient.heartbeat(getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
if (response != null) {
break;
}
if (!needToWatch()) {
exception(new ClientException("could not connect to service center"));
}
try {
LOGGER.warn("Update heartbeat to service center failed, retry after {}s. service={}/{}", timer.getNextTimeout(), getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
timer.sleep();
} catch (TimerException e) {
LOGGER.error("Update heartbeat to service center failed, can not connect to service center. service={}/{}", getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
return null;
}
}
if (!response.isOk()) {
LOGGER.error("Update heartbeat to service center failed, microservice instance={}/{} does not exist", getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
}
return response;
}
use of io.servicecomb.serviceregistry.client.ClientException in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method watch.
public void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
Boolean alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
synchronized (ServiceRegistryClientImpl.class) {
alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
watchServices.put(selfMicroserviceId, true);
String url = Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + selfMicroserviceId + Const.WATCHER_PATH;
IpPort ipPort = IpPortManager.INSTANCE.get();
if (ipPort == null) {
LOGGER.error("request address is null, watch microservice {}", selfMicroserviceId);
watchErrorHandler(new Exception("request address is null"), selfMicroserviceId, callback);
return;
}
WebsocketUtils.open(ipPort, url, o -> {
onOpen.success(o);
LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
}, c -> {
LOGGER.warn("watching microservice {} connection is closed accidentally", selfMicroserviceId);
watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
onClose.success(null);
}, bodyBuffer -> {
MicroserviceInstanceChangedEvent response = null;
try {
response = JsonUtils.readValue(bodyBuffer.getBytes(), MicroserviceInstanceChangedEvent.class);
} catch (Exception e) {
LOGGER.error("watcher handle microservice {} response failed, {}", selfMicroserviceId, bodyBuffer.toString());
return;
}
try {
callback.success(response);
} catch (Exception e) {
LOGGER.error("notify watcher failed, microservice {}", selfMicroserviceId, e);
}
}, e -> {
LOGGER.error("watcher read microservice {} message from service center failed," + " {}", selfMicroserviceId, e.getMessage());
}, f -> {
if (!watchServices.containsKey(selfMicroserviceId)) {
return;
}
LOGGER.error("watcher connect to service center server failed, microservice {}, {}", selfMicroserviceId, f.getMessage());
watchErrorHandler(f, selfMicroserviceId, callback);
});
}
}
}
}
use of io.servicecomb.serviceregistry.client.ClientException in project java-chassis by ServiceComb.
the class TestServiceRegistryClientImpl method testException.
@Test
public void testException() {
Microservice oInstance = RegistryUtils.getMicroservice();
RegistryThread oThread = new RegistryThread();
oThread.start();
oClient.registerMicroservice(oInstance);
oClient.registerMicroserviceInstance(RegistryUtils.getMicroserviceInstance());
try {
oClient.init();
} catch (Exception e) {
Assert.assertEquals(null, e.getMessage());
}
new MockUp<CountDownLatch>() {
@Mock
public void await() throws InterruptedException {
throw new InterruptedException();
}
};
Assert.assertEquals(null, oClient.getMicroserviceId(RegistryUtils.getMicroservice().getAppId(), RegistryUtils.getMicroservice().getServiceName(), RegistryUtils.getMicroservice().getVersion()));
Assert.assertEquals(null, oClient.getAllMicroservices());
Assert.assertEquals(null, oClient.registerMicroservice(RegistryUtils.getMicroservice()));
Assert.assertEquals(null, oClient.getMicroservice("microserviceId"));
Assert.assertEquals(null, oClient.getMicroserviceInstance("consumerId", "providerId"));
Assert.assertEquals(false, oClient.unregisterMicroserviceInstance("microserviceId", "microserviceInstanceId"));
Assert.assertEquals(null, oClient.heartbeat("microserviceId", "microserviceInstanceId"));
Assert.assertEquals(null, oClient.findServiceInstance("selfMicroserviceId", "appId", "serviceName", "versionRule"));
MicroserviceInstance microInstance = Mockito.mock(MicroserviceInstance.class);
Assert.assertEquals(null, oClient.registerMicroserviceInstance(microInstance));
Assert.assertEquals("a", new ClientException("a").getMessage());
}
Aggregations