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