Search in sources :

Example 1 with TimerException

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;
        }
    }
}
Also used : HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) TimerException(io.servicecomb.serviceregistry.utils.TimerException)

Example 2 with TimerException

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);
}
Also used : ServiceRegistryConfig(io.servicecomb.serviceregistry.config.ServiceRegistryConfig) TimerException(io.servicecomb.serviceregistry.utils.TimerException)

Example 3 with TimerException

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;
}
Also used : HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) TimerException(io.servicecomb.serviceregistry.utils.TimerException) Timer(io.servicecomb.serviceregistry.utils.Timer) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 4 with TimerException

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());
    }
}
Also used : Expectations(mockit.Expectations) HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) TimerException(io.servicecomb.serviceregistry.utils.TimerException) MockUp(mockit.MockUp) TimerException(io.servicecomb.serviceregistry.utils.TimerException) Test(org.junit.Test)

Aggregations

TimerException (io.servicecomb.serviceregistry.utils.TimerException)4 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)3 ClientException (io.servicecomb.serviceregistry.client.ClientException)1 ServiceRegistryConfig (io.servicecomb.serviceregistry.config.ServiceRegistryConfig)1 Timer (io.servicecomb.serviceregistry.utils.Timer)1 Expectations (mockit.Expectations)1 MockUp (mockit.MockUp)1 Test (org.junit.Test)1