Search in sources :

Example 1 with ClientException

use of org.apache.servicecomb.serviceregistry.client.ClientException in project java-chassis by ServiceComb.

the class TestServiceRegistryClientImpl method testException.

@Test
public void testException() {
    ArchaiusUtils.setProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION, "app");
    ArchaiusUtils.setProperty(BootStrapProperties.CONFIG_SERVICE_NAME, "ms");
    MicroserviceFactory microserviceFactory = new MicroserviceFactory();
    Microservice microservice = microserviceFactory.create();
    Assert.assertNull(oClient.registerMicroservice(microservice));
    Assert.assertNull(oClient.registerMicroserviceInstance(microservice.getInstance()));
    oClient.init();
    Assert.assertNull(oClient.getMicroserviceId(microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), microservice.getEnvironment()));
    Assert.assertThat(oClient.getAllMicroservices().isEmpty(), is(true));
    Assert.assertNull(oClient.registerMicroservice(microservice));
    Assert.assertNull(oClient.getMicroservice("microserviceId"));
    Assert.assertNull(oClient.getMicroserviceInstance("consumerId", "providerId"));
    Assert.assertFalse(oClient.unregisterMicroserviceInstance("microserviceId", "microserviceInstanceId"));
    Assert.assertNull(oClient.heartbeat("microserviceId", "microserviceInstanceId"));
    Assert.assertNull(oClient.findServiceInstance("selfMicroserviceId", "appId", "serviceName", "versionRule"));
    Assert.assertNull(oClient.findServiceInstances("selfMicroserviceId", "appId", "serviceName", "versionRule", "0"));
    Assert.assertEquals("a", new ClientException("a").getMessage());
    ArchaiusUtils.resetConfig();
}
Also used : Microservice(org.apache.servicecomb.registry.api.registry.Microservice) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) MicroserviceFactory(org.apache.servicecomb.registry.api.registry.MicroserviceFactory) Test(org.junit.Test)

Example 2 with ClientException

use of org.apache.servicecomb.serviceregistry.client.ClientException in project incubator-servicecomb-java-chassis by apache.

the class TestServiceRegistryClientImpl method testException.

@Test
public void testException() {
    MicroserviceFactory microserviceFactory = new MicroserviceFactory();
    Microservice microservice = microserviceFactory.create("app", "ms");
    Assert.assertEquals(null, oClient.registerMicroservice(microservice));
    Assert.assertEquals(null, oClient.registerMicroserviceInstance(microservice.getInstance()));
    oClient.init();
    Assert.assertEquals(null, oClient.getMicroserviceId(microservice.getAppId(), microservice.getServiceName(), microservice.getVersion()));
    Assert.assertThat(oClient.getAllMicroservices().isEmpty(), is(true));
    Assert.assertEquals(null, oClient.registerMicroservice(microservice));
    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"));
    Assert.assertEquals(null, oClient.findServiceInstances("selfMicroserviceId", "appId", "serviceName", "versionRule", "0"));
    Assert.assertEquals("a", new ClientException("a").getMessage());
}
Also used : Microservice(org.apache.servicecomb.serviceregistry.api.registry.Microservice) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) MicroserviceFactory(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory) Test(org.junit.Test)

Example 3 with ClientException

use of org.apache.servicecomb.serviceregistry.client.ClientException in project incubator-servicecomb-java-chassis by apache.

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 = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId);
                IpPort ipPort = ipPortManager.getAvailableAddress();
                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 -> {
                    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 -> {
                    watchErrorHandler(e, selfMicroserviceId, callback);
                    onClose.success(null);
                }, f -> {
                    watchErrorHandler(f, selfMicroserviceId, callback);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 4 with ClientException

use of org.apache.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 = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId);
                IpPort ipPort = ipPortManager.getAvailableAddress();
                websocketClientUtil.open(ipPort, url, o -> {
                    onOpen.success(o);
                    LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
                }, c -> {
                    watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
                    onClose.success(null);
                }, bodyBuffer -> {
                    MicroserviceInstanceChangedEvent response;
                    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 -> {
                    watchErrorHandler(e, selfMicroserviceId, callback);
                    onClose.success(null);
                }, f -> {
                    watchErrorHandler(f, selfMicroserviceId, callback);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)4 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 MicroserviceInstanceChangedEvent (org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent)1 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)1 MicroserviceFactory (org.apache.servicecomb.registry.api.registry.MicroserviceFactory)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceFactory (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceFactory)1 MicroserviceInstanceChangedEvent (org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)1