使用Java API调用Zookeeper
一,概述:
我们知道可以通过命令来操作Zookeper的客户端和服务端并进行相应的操作,但是有时候需要使用java在程序中对其进行操作.
即,通过API(JAVA)来操作Zookeeper.
二,依赖:
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
三,操作:
首先我们需要如下的基本信息:
//zookeeper
public static String APPLICATION_MASTER_NAME = "realtime-master";
public static String APPLICATION_SLAVE_NAME_PREFIX = "realtime-slave-";
public static String HA_ROOT_PATH = "/realtimeApp/ha";
private ZooKeeper zk;
private static final int SESSION_TIMEOUT = 5000;
public ZkService(String host) throws IOException, InterruptedException {
zk = new ZooKeeper(host, SESSION_TIMEOUT, (watchedEvent) -> {});
}
1,创建节点:
//创建rootNode
public void createRootNode() throws KeeperException, InterruptedException {
if (!isExists("/realtimeApp")) {
zk.create("/realtimeApp", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
if (!isExists(Constants.HA_ROOT_PATH)) {
zk.create(Constants.HA_ROOT_PATH, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
LOG.info("Success to create root Node : " + Constants.HA_ROOT_PATH);
}
//创建masterNode
public void addMasterNode(String rootPath, String masterName) throws KeeperException, InterruptedException {
String path = rootPath + "/" + masterName;
String createPath = zk.create(path, masterName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
LOG.info("Success to create Master ephemeral Node : " + createPath);
}
//创建slaveNode
public void addSlaveNode(String rootPath, String slaveName) throws KeeperException, InterruptedException {
String path = rootPath + "/" + slaveName;
String createPath = zk.create(path, slaveName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
LOG.info("Success to create Slave ephemeral Node : " + createPath);
}
2,删除节点:
//删除单个节点:
public void deleteNode(String nodePath) throws KeeperException, InterruptedException {
zk.delete(nodePath, -1);
}
//删除所有节点:
public void deleteAllSlaves(String groupName) throws Exception {
String path = "/" + groupName;
try {
List<String> nodes = zk.getChildren(path, false);
for (String node : nodes) {
zk.delete(path + "/" + node, -1);
}
zk.delete(path, -1);
} catch (Exception e) {
e.printStackTrace();
}
}
3,获取节点:
public void listNodes(String path) throws KeeperException, InterruptedException {
List<String> nodes = zk.getChildren(path, false);
if (nodes.isEmpty())
LOG.info("path : " + path + " no any child node.");
for (String node : nodes) {
LOG.info(node);
}
}
4,判断某个节点是否存在:
public boolean isExists(String path) throws KeeperException, InterruptedException {
Stat stat = zk.exists(path, false);
return stat == null ? false : true;
}
5,判断slave节点是否在线:
public boolean hadSlaveOnline(String rootPath) throws KeeperException, InterruptedException {
List<String> nodes = zk.getChildren(rootPath, false);
if (nodes == null || nodes.size() == 0) {
return false;
}
for (String node : nodes) {
if (node.startsWith(Constants.APPLICATION_SLAVE_NAME_PREFIX)) {
LOG.info("Find an slave {} online.", node);
return true;
}
}
return false;
}