Search in sources :

Example 1 with ClientException

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;
}
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 2 with ClientException

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);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) IpPort(io.servicecomb.foundation.common.net.IpPort) ClientException(io.servicecomb.serviceregistry.client.ClientException) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 3 with ClientException

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());
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) RegistryThread(io.servicecomb.serviceregistry.RegistryThread) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) MockUp(mockit.MockUp) ClientException(io.servicecomb.serviceregistry.client.ClientException) ClientException(io.servicecomb.serviceregistry.client.ClientException) Test(org.junit.Test)

Aggregations

ClientException (io.servicecomb.serviceregistry.client.ClientException)3 IpPort (io.servicecomb.foundation.common.net.IpPort)1 RegistryThread (io.servicecomb.serviceregistry.RegistryThread)1 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)1 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)1 MicroserviceInstanceChangedEvent (io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)1 Timer (io.servicecomb.serviceregistry.utils.Timer)1 TimerException (io.servicecomb.serviceregistry.utils.TimerException)1 MockUp (mockit.MockUp)1 Test (org.junit.Test)1