ZK案例分析
任意一个接入zk集群的客户端,如何知道zk集群内znode节点的上下线?
1.首先模拟集群机器变动,先来看看代码
package zkmanage;
import org.apache.zookeeper.*;
import java.io.IOException;
/**
* 模拟zk集群上下线1台新机器,
* 传参hostname代表上线机器信息,
* 停线程代表下线机器
*
*/
public class DistributeServer {
private String connectStr = "ubuntu001:2181,ubuntu002:2181,ubuntu003:2181";
private int sessionTimeout = 2000;
private ZooKeeper zkClient;
public static void main(String[] args) throws Exception {
//1.连接集群
DistributeServer server = new DistributeServer();
server.getConnect();
//2.注册znode
server.regist(args[0]);
//3.业务代码
server.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void regist(String hostname) throws KeeperException, InterruptedException {
zkClient.create("/leafznodes/leafznode", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname + " is online");
}
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectStr, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
}
});
}
}
这里可以看到是创建了非持久型节点,在创建节点这一过程的client和server断开后znode销毁,所以说停这个程序就相当于下线节点了
2.客户端(这里指的是接入zk想了解集群信息的机器,可以理解为运维人员)想获得所有节点信息(这个信息在上面用的hosts指代),模拟这一情景
package zkmanage;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.omg.PortableServer.THREAD_POLICY_ID;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DistributeClient {
private String connectStr = "ubuntu001:2181,ubuntu002:2181,ubuntu003:2181";
private int sessionTimeout = 2000;
private ZooKeeper zkClient;
public static void main(String[] args) throws Exception {
//模拟客户端感知znode节点变化
//1.连接集群
DistributeClient client = new DistributeClient();
client.getConnect();
//2.注册监听
client.getChildren();
//3.业务代码
client.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void getChildren() throws KeeperException, InterruptedException {
ArrayList<String> hosts = new ArrayList<String>();
List<String> children = zkClient.getChildren("/leafznodes", true);
for (String child: children
) {
byte[] data = zkClient.getData("/leafznodes/" + child, false, null);
hosts.add(new String(data));
}
System.out.println(hosts);
}
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectStr, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
try {
getChildren();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
3.运行起来后,无论在shell里或者是DistributeServer类里,在/leafznodes路径下创建或删除节点时,在DistributeClient控制台上均能打印当前全部节点信息。这是因为getChildren里面注册了监听,而监听执行的process方法又调用了getchildren,又导致注册了一个针对/leafznodes路径的监听(禁止套娃)。确实死循环了,不过这程序也就调用一下api,也不一直在转,没什么好死机的。这么做的目的是能让程序一直运行一直监听,不然监听一次失效,前面说过这个事的。
运行效果类似下面
[]
[]
[node]
[node, node1]
[node2222, node, node1]
[node2222, node444, node, node1]
[node2222, node444, node5555, node, node1]
[node2222, node5555, node, node1]
[node2222, node, node1]