Search in sources :

Example 6 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project druid by druid-io.

the class ServiceAnnouncerTest method createAndAnnounceServices.

private ServiceDiscovery createAndAnnounceServices(List<String> serviceNames) throws Exception {
    int port = 1000;
    ServiceDiscovery<Void> serviceDiscovery = ServiceDiscoveryBuilder.builder(Void.class).basePath("/test").client(curator).build();
    for (String serviceName : serviceNames) {
        String serviceNameToUse = CuratorServiceUtils.makeCanonicalServiceName(serviceName);
        ServiceInstance instance = ServiceInstance.<Void>builder().name(serviceNameToUse).address("localhost").port(port++).build();
        serviceDiscovery.registerService(instance);
    }
    return serviceDiscovery;
}
Also used : ServiceInstance(org.apache.curator.x.discovery.ServiceInstance)

Example 7 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project apex-malhar by apache.

the class ZKAssistedDiscovery method discover.

@Override
public Collection<Service<byte[]>> discover() {
    try {
        new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
        Collection<ServiceInstance<byte[]>> services = discovery.queryForInstances(serviceName);
        ArrayList<Service<byte[]>> returnable = new ArrayList<Service<byte[]>>(services.size());
        for (final ServiceInstance<byte[]> service : services) {
            returnable.add(new Service<byte[]>() {

                @Override
                public String getHost() {
                    return service.getAddress();
                }

                @Override
                public int getPort() {
                    return service.getPort();
                }

                @Override
                public byte[] getPayload() {
                    return service.getPayload();
                }

                @Override
                public String getId() {
                    return service.getId();
                }

                @Override
                public String toString() {
                    return "{" + getId() + " => " + getHost() + ':' + getPort() + '}';
                }
            });
        }
        return returnable;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : EnsurePath(org.apache.curator.utils.EnsurePath) ArrayList(java.util.ArrayList) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) IOException(java.io.IOException)

Example 8 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestServiceDiscovery method testCrashedServer.

@Test
public void testCrashedServer() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance, false) {

            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        closeables.add(discovery);
        discovery.start();
        timing.acquireSemaphore(semaphore);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        server.stop();
        server.restart();
        closeables.add(server);
        timing.acquireSemaphore(semaphore);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
    } finally {
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Semaphore(java.util.concurrent.Semaphore) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 9 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestServiceDiscovery method testCrashedServerMultiInstances.

@Test
public void testCrashedServerMultiInstances() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        Timing timing = new Timing();
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        final Semaphore semaphore = new Semaphore(0);
        ServiceInstance<String> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
        ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance1, false) {

            @Override
            protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
                super.internalRegisterService(service);
                semaphore.release();
            }
        };
        closeables.add(discovery);
        discovery.start();
        discovery.registerService(instance2);
        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        server.stop();
        server.restart();
        closeables.add(server);
        timing.acquireSemaphore(semaphore, 2);
        Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
    } finally {
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Semaphore(java.util.concurrent.Semaphore) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 10 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestServiceDiscovery method testBasic.

@Test
public void testBasic() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build();
        closeables.add(discovery);
        discovery.start();
        Assert.assertEquals(discovery.queryForNames(), Collections.singletonList("test"));
        List<ServiceInstance<String>> list = Lists.newArrayList();
        list.add(instance);
        Assert.assertEquals(discovery.queryForInstances("test"), list);
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Test(org.testng.annotations.Test)

Aggregations

ServiceInstance (org.apache.curator.x.discovery.ServiceInstance)19 CuratorFramework (org.apache.curator.framework.CuratorFramework)9 RetryOneTime (org.apache.curator.retry.RetryOneTime)9 Test (org.testng.annotations.Test)9 Closeable (java.io.Closeable)8 IOException (java.io.IOException)6 HashSet (java.util.HashSet)4 Timing (org.apache.curator.test.Timing)4 DrillbitEndpoint (org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint)4 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 MultiKey (org.apache.commons.collections.keyvalue.MultiKey)2 JSONObject (com.alibaba.fastjson.JSONObject)1 NodeStatus (info.xiancloud.core.distribution.NodeStatus)1 BufferedReader (java.io.BufferedReader)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EnsurePath (org.apache.curator.utils.EnsurePath)1 SimpleTimestampedMessageParser (org.apache.oozie.util.SimpleTimestampedMessageParser)1