赞
踩
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
public static void main(String[] args) throws Exception{ String hostPort = "192.168.193.110:2181"; // 1.连接ZooKeeper ZooKeeper zooKeeper = new ZooKeeper(hostPort, 1000, null); // 2.创建节点 String s = zooKeeper.create("/testRootPath", "testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // 永久节点 // 3.获取节点数据 byte[] data = zooKeeper.getData("/testRootPath", null, null); System.out.println(new String(data,"utf-8")); // 4.判断节点是否存在 Stat exists = zooKeeper.exists("/testRootPath", true); System.out.println(exists); // 5.删除节点 zooKeeper.delete("/testRootPath",0); zooKeeper.close(); }
static ZooKeeper zkCli = null; public static void main(String[] args) throws Exception { zkCli = new ZooKeeper("192.168.193.110:2181", 3000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("监听回调函数"); // try { // byte[] data1 = zkCli.getData("/demo2", true, null); // System.out.println("修改后的值:"+new String(data1)); // } catch (KeeperException e) { // e.printStackTrace(); // } catch (InterruptedException e) { // e.printStackTrace(); // } try { List<String> children = zkCli.getChildren("/", true); for(String c:children){ System.out.println(c); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); // 注册监听目录 zkCli.getChildren("/",true); // 注册监听节点 zkCli.getData("/demo2",true,null); Thread.sleep(Long.MAX_VALUE); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。