Created
April 13, 2017 16:14
-
-
Save gAmUssA/243abb463cd94bbe695958b998b572c9 to your computer and use it in GitHub Desktop.
simple ZK discover / query client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hazelcast.zookeeper; | |
import org.apache.curator.framework.CuratorFramework; | |
import org.apache.curator.framework.CuratorFrameworkFactory; | |
import org.apache.curator.retry.ExponentialBackoffRetry; | |
import org.apache.curator.x.discovery.ServiceDiscovery; | |
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; | |
import org.apache.curator.x.discovery.ServiceInstance; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
/** | |
* Created by vikgamov on 4/13/17. | |
*/ | |
public class ZookeeperTest { | |
private String group; | |
private CuratorFramework client; | |
private ServiceDiscovery<Void> serviceDiscovery; | |
private ServiceInstance<Void> serviceInstance; | |
private static final String DEFAULT_PATH = "/discovery/hazelcast"; | |
private static final String DEFAULT_GROUP = "hazelcast"; | |
private static final int CURATOR_BASE_SLEEP_TIME_MS = 1000; | |
// zookeperUrl {ip-address-of-zookeeper}:{port-of-zookeeper} | |
public static void main(String[] args) throws IOException { | |
final ZookeeperTest zookeeperTest = new ZookeeperTest(); | |
final String zookeeperUrl = args[0]; | |
final CuratorFramework client = zookeeperTest.startCuratorClient(zookeeperUrl); | |
Runtime.getRuntime().addShutdownHook(new Thread(client::close)); | |
System.in.read(); | |
} | |
private void discovery(ServiceDiscovery<Void> serviceDiscovery) { | |
try { | |
Collection<ServiceInstance<Void>> members = serviceDiscovery.queryForInstances(group); | |
List<String> nodes = new ArrayList<>(members.size()); | |
for (ServiceInstance<Void> serviceInstance : members) { | |
String host = serviceInstance.getAddress(); | |
Integer port = serviceInstance.getPort(); | |
nodes.add(host + ":" + port); | |
} | |
System.out.println(nodes); | |
} catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
throw new IllegalStateException("Error while talking to ZooKeeper", e); | |
} catch (Exception e) { | |
throw new IllegalStateException("Error while talking to ZooKeeper", e); | |
} | |
} | |
private CuratorFramework startCuratorClient(String zookeeperUrl) { | |
if (zookeeperUrl == null) { | |
throw new IllegalStateException("Zookeeper URL cannot be null."); | |
} | |
System.out.println("Using " + zookeeperUrl + " as Zookeeper URL"); | |
client = CuratorFrameworkFactory.newClient(zookeeperUrl, new ExponentialBackoffRetry(CURATOR_BASE_SLEEP_TIME_MS, 3)); | |
client.start(); | |
final ServiceDiscovery<Void> discovery = connectToZk(client); | |
discovery(discovery); | |
return client; | |
} | |
private ServiceDiscovery<Void> connectToZk(CuratorFramework client) { | |
group = DEFAULT_GROUP; | |
ServiceDiscovery<Void> serviceDiscovery; | |
try { | |
String path = DEFAULT_PATH; | |
ServiceDiscoveryBuilder<Void> discoveryBuilder = ServiceDiscoveryBuilder.builder(Void.class) | |
.basePath(path) | |
.client(client); | |
serviceDiscovery = discoveryBuilder.build(); | |
serviceDiscovery.start(); | |
} catch (Exception e) { | |
throw new IllegalStateException("Error while talking to ZooKeeper. ", e); | |
} | |
return serviceDiscovery; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment