1.创建工程引入依赖
<dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
2.测试连接
public class ZookeeperConnection { public static void main(String[] args) { ZooKeeper zooKeeper = null; try { // 计数器对象 CountDownLatch countDownLatch = new CountDownLatch(1); // 参数1:服务器的ip和端口 参数2:客户端和服务器之间的会话超时时间,以毫秒为单位 参数3:监视器对象 zooKeeper = new ZooKeeper("192.168.43.182:2181", 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { System.out.println("连接创建成功!"); countDownLatch.countDown(); } } }); // 主线程阻塞等待连接对象的创建成功 countDownLatch.await(); // 会话编号 System.out.println(zooKeeper.getSessionId()); } catch (Exception e) { e.printStackTrace(); } finally { if (zooKeeper != null) { try { zooKeeper.close(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
新增节点:
public class ZKCreate { private String IP = "192.168.43.182:2181"; private ZooKeeper zooKeeper; @Before public void before() { try { // 计数器对象 CountDownLatch countDownLatch = new CountDownLatch(1); // 参数1:服务器的ip和端口 参数2:客户端和服务器之间的会话超时时间,以毫秒为单位 参数3:监视器对象 zooKeeper = new ZooKeeper(IP, 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { System.out.println("连接创建成功!"); countDownLatch.countDown(); } } }); // 主线程阻塞等待连接对象的创建成功 countDownLatch.await(); // 会话编号 System.out.println(zooKeeper.getSessionId()); } catch (Exception e) { e.printStackTrace(); } } @After public void after() { if (zooKeeper != null) { try { zooKeeper.close(); } catch (InterruptedException e) { e.printStackTrace(); } } } @Test public void create1 () throws Exception { // 参数1:节点的路径 // 参数2:节点的数据 // 参数3:权限列表 OPEN_ACL_UNSAFE:world:anyone:cdrwa // 参数4:节点类型 持久化节点 // zooKeeper.create("/create/node1", "/node1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // READ_ACL_UNSAFE:world:anyone:r // zooKeeper.create("/create/node2", "/node2".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT); /*********world授权模式*********/ // 权限列表 // List<ACL> acls = new ArrayList<>(); // 授权模式和授权对象 // Id id = new Id("world", "anyone"); // 权限设置 // acls.add(new ACL(ZooDefs.Perms.READ, id)); // acls.add(new ACL(ZooDefs.Perms.WRITE, id)); // zooKeeper.create("/create/node3", "/node3".getBytes(), acls, CreateMode.PERSISTENT); /*********IP授权模式*********/ /*List<ACL> acls = new ArrayList<>(); Id id = new Id("ip", "192.168.43.33"); acls.add(new ACL(ZooDefs.Perms.ALL, id)); zooKeeper.create("/create/node4", "/node4".getBytes(), acls, CreateMode.PERSISTENT);*/ /*********Auth授权模式*********/ // zooKeeper.addAuthInfo("digest", "fan:123456".getBytes()); // zooKeeper.create("/create/node5", "/node5".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); /*zooKeeper.addAuthInfo("digest", "fan:123456".getBytes()); List<ACL> acls = new ArrayList<>(); Id id = new Id("auth", "fan"); acls.add(new ACL(ZooDefs.Perms.READ, id)); zooKeeper.create("/create/node6", "/node6".getBytes(), acls, CreateMode.PERSISTENT);*/ /*********Digest授权模式*********/ /*List<ACL> acls = new ArrayList<>(); Id id = new Id("digest", "fan:c2+ooVbyofyH2yzwMEZOmHYO7cE="); acls.add(new ACL(ZooDefs.Perms.ALL, id)); zooKeeper.create("/create/node7", "/node7".getBytes(), acls, CreateMode.PERSISTENT);*/ /*********持久化有序节点*********/ // OPEN_ACL_UNSAFE:world:anyone:cdrwa // PERSISTENT_SEQUENTIAL // String result = zooKeeper.create("/create/node8", "/node8".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); // System.out.println(result); // /create/node80000000008 /*********临时节点*********/ // EPHEMERAL // String result = zooKeeper.create("/create/node9", "/node9".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); // System.out.println(result); // /create/node9 /*********临时节点*********/ // EPHEMERAL_SEQUENTIAL // String result = zooKeeper.create("/create/node9", "/node9".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); // System.out.println(result); // /create/node90000000010 /*********异步方式创建节点*********/ zooKeeper.create("/create/node11", "node11".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() { @Override public void processResult(int rc, String path, Object ctx, String name) { // 0代表创建成功 System.out.println(rc); // 0 // 节点的路径 System.out.println(path); // /create/node11 // 节点路径 System.out.println(name); // /create/node11 // 上下文参数 System.out.println(ctx); // I am context } }, "I am context"); Thread.sleep(10000); System.out.println("结束"); } }
更新节点:
@Test public void update () throws Exception { // 参数1:节点的路径 参数2:修改的数据 参数3:数据版本号 -1代表版本号不参与更新 // zooKeeper.setData("/set/node1", "node11".getBytes(), -1); // zooKeeper.setData("/set/node1", "node111".getBytes(), 1); // Stat stat = zooKeeper.setData("/set/node1", "node1111".getBytes(), -1); // System.out.println(stat.getVersion()); // 3 /*********异步方式修改节点*********/ zooKeeper.setData("/set/node1", "node11111".getBytes(), -1, new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { // 0代表修改成功 System.out.println(rc); // 0 // 节点路径 System.out.println(path); // /set/node1 // 上下文参数对象 System.out.println(ctx); // I am Context // 属性描述对象 System.out.println(stat.getVersion()); // 4 } }, "I am Context"); Thread.sleep(10000); System.out.println("结束"); }
删除节点:
@Test public void delete () throws Exception { // 参数1:节点路径 参数2:数据版本信息 -1代表删除节点时不考虑版本信息 // zooKeeper.delete("/delete/node1", -1); // 异步使用方式 zooKeeper.delete("/delete/node2", -1, new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { // 0代表删除成功 System.out.println(rc); // 0 // 节点路径 System.out.println(path); // /set/node2 // 上下文参数对象 System.out.println(ctx); // I am Context } }, "I am Context"); Thread.sleep(10000); System.out.println("结束"); }
查看节点:
@Test public void get () throws Exception { // 参数1:节点的路径 参数2:是否使用连接对象中注册的监视器 参数3:读取节点属性的对象 Stat stat = new Stat(); byte[] bys = zooKeeper.getData("/get/node1", false, stat); // 打印读取的数据 System.out.println(new String(bys)); // node1 // 版本信息 System.out.println(stat.getVersion()); // 0 // 异步方式 zooKeeper.getData("/get/node1", false, new AsyncCallback.DataCallback() { @Override public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { // 0代表获取成功 System.out.println(rc); // 0 // 节点路径 System.out.println(path); // /get/node1 // 上下文参数对象 System.out.println(ctx); // I am Context // 数据 System.out.println(new String(data)); // node1 // 属性对象 System.out.println(stat.getVersion()); // 0 } }, "I am Context"); Thread.sleep(10000); System.out.println("结束"); }
查看子节点:
@Test public void getChildren () throws Exception { // 参数1:节点的路径 参数2:是否使用连接对象中注册的监视器 List<String> list = zooKeeper.getChildren("/get", false); int size = list.size(); for (int i = 0; i < size; i++) { System.out.println(list.get(i)); } // 异步用法 zooKeeper.getChildren("/get", false, new AsyncCallback.ChildrenCallback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children) { // 0代表查询成功 System.out.println(rc); // 0 // 节点路径 System.out.println(path); // /get // 上下文参数对象 System.out.println(ctx); // I am Context // 子节点信息 int size = children.size(); for (int i = 0; i < size; i++) { System.out.println(children.get(i)); } } }, "I am Context"); Thread.sleep(10000); System.out.println("结束"); }
检查节点是否存在:
@Test public void exists () throws Exception { // 参数1:节点的路径 参数2:是否使用连接对象中注册的监视器 Stat stat = zooKeeper.exists("/exists1", false); System.out.println(stat.getVersion()); // 0 // 异步用法 zooKeeper.exists("/exists1", false, new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { // 0代表查询成功 System.out.println(rc); // 0 // 节点路径 System.out.println(path); // /exists1 // 上下文参数对象 System.out.println(ctx); // I am Context // 属性对象 System.out.println(stat.getVersion()); } }, "I am Context"); Thread.sleep(10000); System.out.println("结束"); }
连接集群:
public class ZookeeperConnection2 { public static void main(String[] args) { ZooKeeper zooKeeper = null; try { // 计数器对象 CountDownLatch countDownLatch = new CountDownLatch(1); // 参数1:服务器的ip和端口 参数2:客户端和服务器之间的会话超时时间,以毫秒为单位 参数3:监视器对象 zooKeeper = new ZooKeeper("192.168.43.182:2181,192.168.43.182:2182,192.168.43.182:2183", 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { System.out.println("连接创建成功!"); countDownLatch.countDown(); } } }); // 主线程阻塞等待连接对象的创建成功 countDownLatch.await(); // 会话编号 System.out.println(zooKeeper.getSessionId()); } catch (Exception e) { e.printStackTrace(); } finally { if (zooKeeper != null) { try { zooKeeper.close(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }