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;
}
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);
}
}
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);
}
}
}
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);
}
}
}
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");
}
Aggregations