use of io.servicecomb.serviceregistry.utils.TimerException in project java-chassis by ServiceComb.
the class RegistryThread method run.
@Override
public void run() {
// 本进程微服务及实例注册
this.doInit();
// 心跳
int interval = ServiceRegistryConfig.INSTANCE.getHeartbeatInterval();
while (isRunning()) {
if (!registerSuccess) {
try {
RegistryUtils.ensureRegisterMicroservice();
} catch (TimerException e) {
continue;
}
registerSuccess = RegistryUtils.regsiterInstance();
}
if (registerSuccess) {
RegistryUtils.watch();
}
try {
TimeUnit.SECONDS.sleep(interval);
} catch (InterruptedException e) {
continue;
}
// 定时心跳
HeartbeatResponse response = RegistryUtils.heartbeat();
if (response != null && !response.isOk()) {
// 心跳返回实例不存在,需要重新注册
registerSuccess = false;
}
}
}
use of io.servicecomb.serviceregistry.utils.TimerException in project java-chassis by ServiceComb.
the class RegistryThread method doInit.
public void doInit() {
ServiceRegistryConfig serviceRegistryConfig = ServiceRegistryConfig.INSTANCE;
try {
if (isEmpty(RegistryUtils.getMicroservice().getServiceId())) {
RegistryUtils.ensureRegisterMicroservice();
}
if (isEmpty(RegistryUtils.getMicroserviceInstance().getInstanceId())) {
RegistryUtils.ensureRegisterInstance();
}
// 初始化client发现SR的动态集群扩容能力
if (serviceRegistryConfig.isRegistryAutoDiscovery()) {
IpPortManager.INSTANCE.createServiceRegistryCache();
}
} catch (TimerException e) {
RegistryUtils.exception(e);
return;
}
registerSuccess = true;
NotifyManager.INSTANCE.notify(RegistryEvent.INITIALIZED, null);
}
use of io.servicecomb.serviceregistry.utils.TimerException 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.utils.TimerException in project java-chassis by ServiceComb.
the class TestRegistry method testRegistryUtilsWithStubHeartbeatFailureException.
@Test
public void testRegistryUtilsWithStubHeartbeatFailureException(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
HeartbeatResponse response = new HeartbeatResponse();
response.setOk(true);
response.setMessage("OK");
try {
new Expectations() {
{
oMockServiceRegistryClient.init();
oMockServiceRegistryClient.registerMicroservice((Microservice) any);
result = "sampleServiceID";
oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
result = "sampleInstanceID";
oMockServiceRegistryClient.heartbeat(anyString, anyString);
result = null;
}
};
RegistryUtils.setSrClient(oMockServiceRegistryClient);
RegistryUtils.init();
new MockUp<Timer>() {
@Mock
public void sleep() throws TimerException {
throw new TimerException();
}
};
boolean validAssert = RegistryUtils.heartbeat().isOk();
Assert.assertTrue(validAssert);
} catch (Exception e) {
Assert.assertEquals("java.lang.NullPointerException", e.getClass().getName());
}
}
Aggregations