完整APIDemo:
import lombok.extern.slf4j.Slf4j; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.concurrent.CountDownLatch; /** @Author mxn * @Description //TODO ZooKeeper Java API测试 * @Date 10:22 2021/9/29 * @Param * @return **/ @Slf4j public class ZookeeperTest { // IP 和端口 private final static String ipAddress = "192.168.2.123:2181"; public static void main(String[] args) { ZookeeperTest test = new ZookeeperTest(); String key = "/zkMxn"; String value = "wo is muxiaonong"; //创建Znode test.add(key,value); // 获取节点数据 // test.get(key); //修改节点数据 // test.modify(key,"wo is zhuzhuxia"); //删除节点 // test.delete(key); } /** * @return * @Author mxn * @Description //TODO 获取ZooKeeper连接 * @Date 10:22 2021/9/29 * @Param **/ public static ZooKeeper getConntection() { ZooKeeper zooKeeper = null; try { final CountDownLatch countDownLatch = new CountDownLatch(1); //watch机制(回调),监听是否连接成功 zooKeeper = new ZooKeeper(ipAddress, 5000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (Event.KeeperState.SyncConnected == watchedEvent.getState()) { //如果受收到了服务端的响应事件,连接成功 countDownLatch.countDown(); } } }); countDownLatch.await(); log.info("zookeeper状态:{}",zooKeeper.getState());//CONNECTED } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return zooKeeper; } /** @Author lyy * @Description //TODO 关闭ZooKeeper连接 * @Date 14:57 2021/9/29 * @Param * @return **/ public static void closeConnection(ZooKeeper zooKeeper) { try { // zooKeeper.close(); } catch (Exception e) { e.printStackTrace(); } } /** @Author lyy * @Description //TODO 添加节点 * @Date 13:36 2021/9/29 * @Param * @return **/ public void add(String key ,String value) { ZooKeeper zooKeeper = ZookeeperTest.getConntection(); try { //参数类型 //1.key //2.value //3.对应的ACL,当前节点的权限控制 //4.设置当前节点类型 zooKeeper.create(key, value.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); ZookeeperTest.closeConnection(zooKeeper); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** @Author lyy * @Description //TODO 获取节点信息 * @Date 14:57 2021/9/29 * @Param * @return **/ public void get(String key) { ZooKeeper zooKeeper = ZookeeperTest.getConntection(); Stat stat = new Stat(); String data = null; try { byte[] bytes = zooKeeper.getData(key, null, stat); data = new String(bytes, "gbk"); log.info("当前节点信息:{}",data); ZookeeperTest.closeConnection(zooKeeper); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** @Author lyy * @Description //TODO 修改节点信息 * @Date 14:57 2021/9/29 * @Param * @return **/ public void modify(String key,String newValue) { ZooKeeper zooKeeper = ZookeeperTest.getConntection(); Stat stat = new Stat(); //version乐观锁概念,此处需要获取version信息,则需要先get拿到节点信息 try { //获取节点(修改需要version信息) zooKeeper.getData(key, null, stat); //再修改 zooKeeper.setData(key, newValue.getBytes(), stat.getVersion()); ZookeeperTest.closeConnection(zooKeeper); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** @Author lyy * @Description //TODO 删除节点 * @Date 14:57 2021/9/29 * @Param * @return **/ public void delete(String key) { ZooKeeper zooKeeper = ZookeeperTest.getConntection(); Stat stat = new Stat(); try { //获取节点(删除需要version信息) zooKeeper.getData(key, null, stat); //删除节点 zooKeeper.delete(key, stat.getVersion()); ZookeeperTest.closeConnection(zooKeeper); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
总结
到这里我们对Zookeeper 大概有个入门级的了解了,不过Zookeeper远远比我们这里讲述的功能多,如何用Zookeeper实现集群管理、分布式锁,队列等等,小农会在后面的文章中进行讲解,关注我,后续精彩内容第一时间推送。
我是牧小农,怕什么真理无穷,进一步有进一步的欢喜,大家加油!!!
————————————————
版权声明:本文为CSDN博主「牧小农」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_14996421/article/details/120543298