You need to enable JavaScript to run this app.
导航

进阶使用

最近更新时间2022.09.22 16:55:45

首次发布时间2022.09.22 16:55:45

以下提供一个简单的 Java 示例,展示如何建立 ZK 连接已经 list ZK 中的节点。

注意

再次提示:为了保证集群的稳定性,不建议使用 ZK 进行自行编程。

public class ZkTest {
    public static void main(String[] args) throws IOException {
        String hostPort = "localhost:2181";
        List<String> zooChildren = new ArrayList<String>();
        ZooKeeper zk = new ZooKeeper(hostPort, 2000, null);
        if (zk != null) {
            try {
                String zpath = "/";
                zooChildren = zk.getChildren(zpath, false);
                System.out.println("Znodes of '/': ");
                for (String child : zooChildren) {
                    System.out.println(child);
                }
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

其余操作可自行查看 Zookeeper 类提供的方法进行调用。