zookeeper_demo
参考:全网最详细的zookeeper实战教程,看完你就知道有多值 - 知乎 (zhihu.com)
ZooKeeper入门,看这篇就够了! - 知乎 (zhihu.com)
windows安装zk:windows环境下安装zookeeper教程详解(单机版)_风轩雨墨的博客-CSDN博客
zk的watch机制:Zookeeper——Watch机制原理_zookeeper watch机制_庄小焱的博客-CSDN博客
命令行使用zk:
javaAPI使用zk:
znode增删改查demo:
CRUDMain.java
package com.hmb; import org.apache.zookeeper.*; import java.io.IOException; public class CRUDMain { public static void main(String[] args) throws InterruptedException, KeeperException, IOException { String SERVER_HOST = "127.0.0.1:2181"; int SESSION_TIME_OUT = 2000; ZooKeeper zooKeeper = new ZooKeeper(SERVER_HOST, SESSION_TIME_OUT, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { Event.KeeperState state = watchedEvent.getState(); if (state == Event.KeeperState.SyncConnected) { Event.EventType type = watchedEvent.getType(); if (type == Event.EventType.None) { System.out.println("zk client connect server successfully..."); } } } }); String path = "/java"; zooKeeper.create(path, "HelloWorld".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("add znode successfully."); System.out.println(new String(zooKeeper.getData(path, false, null))); zooKeeper.setData(path, "hhhh".getBytes(), -1); System.out.println(new String(zooKeeper.getData(path, false, null))); zooKeeper.delete(path, -1); System.out.println(zooKeeper.exists(path, false)); zooKeeper.close(); } }
运行结果:
服务器上下线动态感知demo:
ZkClient.java
package com.hmb; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.LinkedList; import java.util.List; public class ZkClient { private static ZooKeeper zooKeeper; private static String path = "/"; public static void main(String[] args) throws IOException, InterruptedException, KeeperException { zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged && event.getPath().equals("/")) { try { showNodeList(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (KeeperException e) { throw new RuntimeException(e); } } } }); showNodeList(); Thread.sleep(Long.MAX_VALUE); } private static void showNodeList() throws InterruptedException, KeeperException { List<String> nodeList = zooKeeper.getChildren(path, true); System.out.println("====nodeList===="); for (String node : nodeList) { System.out.println("nodeName:" + node); } } }
先运行程序,然后到客户端中增删znode,回来可见程序成功监控着“/”目录下znode的变化
DistributedClient.java
package com.hmb; import org.apache.zookeeper.*; import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; public class DistributedClient { private String host = "localhost:2181"; private CountDownLatch latch = new CountDownLatch(1); private String waitPath; private String thisPath; private ZooKeeper zk; private String groupNode = "group"; private String subNode = "sub"; public void connectZkServer() throws IOException, InterruptedException, KeeperException { zk = new ZooKeeper(host, 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { latch.countDown(); } if (event.getType() == Event.EventType.NodeDeleted && event.getPath().equals(waitPath)) { try { doSomething(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (KeeperException e) { throw new RuntimeException(e); } } } }); latch.await(); thisPath = zk.create("/" + groupNode + "/" + subNode, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); List<String> childrens = zk.getChildren("/" + groupNode, false); if (childrens.size() == 1) { doSomething(); } else { String thisNode = thisPath.substring(("/" + groupNode + "/").length()); Collections.sort(childrens); int idx = childrens.indexOf(thisNode); if (idx == 0) { doSomething(); } else { waitPath = "/" + groupNode + "/" + childrens.get(idx - 1); zk.getData(waitPath, true, null); } } } private void doSomething() throws InterruptedException, KeeperException { System.out.println("get lock..."); System.out.println(thisPath); try { Thread.sleep(2000); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { System.out.println("release lock..."); zk.delete(thisPath, -1); } } public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { new Thread() { @Override public void run() { DistributedClient client = new DistributedClient(); try { client.connectZkServer(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (KeeperException e) { throw new RuntimeException(e); } } }.start(); } Thread.sleep(Integer.MAX_VALUE); } }
到zk安装bin目录下,执行zkServer.cmd启动zk服务端,执行zkCli.cmd启动zk客户端,然后在客户端创建好group节点
然后运行程序,结果如下