Search in sources :

Example 1 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 2 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 3 with ServiceInstance

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

the class TestServiceDiscovery method testNoServerOnStart.

@Test
public void testNoServerOnStart() throws Exception {
    server.stop();
    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();
        server.restart();
        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)

Example 4 with ServiceInstance

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

the class TestServiceDiscovery method testMultipleInstances.

@Test
public void testMultipleInstances() throws Exception {
    final String SERVICE_ONE = "one";
    final String SERVICE_TWO = "two";
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<Void> s1_i1 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s1_i2 = ServiceInstance.<Void>builder().name(SERVICE_ONE).build();
        ServiceInstance<Void> s2_i1 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();
        ServiceInstance<Void> s2_i2 = ServiceInstance.<Void>builder().name(SERVICE_TWO).build();
        ServiceDiscovery<Void> discovery = ServiceDiscoveryBuilder.builder(Void.class).client(client).basePath("/test").build();
        closeables.add(discovery);
        discovery.start();
        discovery.registerService(s1_i1);
        discovery.registerService(s1_i2);
        discovery.registerService(s2_i1);
        discovery.registerService(s2_i2);
        Assert.assertEquals(Sets.newHashSet(discovery.queryForNames()), Sets.newHashSet(SERVICE_ONE, SERVICE_TWO));
        List<ServiceInstance<Void>> list = Lists.newArrayList();
        list.add(s1_i1);
        list.add(s1_i2);
        Collections.sort(list, comparator);
        List<ServiceInstance<Void>> queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_ONE));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal l: %s - d: %s", list, queriedInstances));
        list.clear();
        list.add(s2_i1);
        list.add(s2_i2);
        Collections.sort(list, comparator);
        queriedInstances = Lists.newArrayList(discovery.queryForInstances(SERVICE_TWO));
        Collections.sort(queriedInstances, comparator);
        Assert.assertEquals(queriedInstances, list, String.format("Not equal 2: %s - d: %s", list, queriedInstances));
    } 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)

Example 5 with ServiceInstance

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

the class TestServiceDiscoveryBuilder method testSetSerializer.

@Test
public void testSetSerializer() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client);
    builder.serializer(new InstanceSerializer<Object>() {

        @Override
        public byte[] serialize(ServiceInstance<Object> instance) {
            return null;
        }

        @Override
        public ServiceInstance<Object> deserialize(byte[] bytes) {
            return null;
        }
    });
    ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build();
    Assert.assertNotNull(discovery.getSerializer(), "default serializer not set");
    Assert.assertFalse(discovery.getSerializer() instanceof JsonInstanceSerializer, "set serializer is JSON");
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) 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