主要jar包在主目录下 项目需要的相关依赖的jar包在zookeeper的解压文件的lib目录下就有 将这几个jar包导入项目中
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <type>pom</type> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
// zookeeper的服务器地址,配置conf是用主机名,这里一样 private String connectString ="zek00:2181,zek01:2181,zek02:2181"; // 连接超时时间 private int sessionTimeout = 2000; private ZooKeeper zk = null; /** * 设置zookeeper对象 * @throws IOException */ @Before public void setZookeeper() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * 事件触发的回调方法 */ @Override public void process(WatchedEvent event) { } }); System.out.println("---"+zk); }
/** * create 方法参数 * 第一个参数 路径 * 第二个参数 值 bytes * 第三个参数 对节点的访问控制 * 第四个参数 节点的类型 短暂 永久 序号 * @throws KeeperException * @throws InterruptedException */ @Test public void create() throws KeeperException, InterruptedException { String path = zk.create("/app3", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(path); } /** * 判断节点是否存在 * @throws InterruptedException * @throws KeeperException */ @Test public void exist() throws KeeperException, InterruptedException{ // 设置为 true 会调用 zk中的监听器 Stat stat = zk.exists("/app1", true); if(stat!=null){ System.out.println("存在。。。。"+stat.getDataLength()); }else{ System.out.println("不存在。。。"); } } /** * 获取子节点 * @throws InterruptedException * @throws KeeperException */ @Test public void getChilren() throws KeeperException, InterruptedException{ List<String> list = zk.getChildren("/", true); for (String s : list) { System.out.println(s); } } /** * 获取节点的内容 * @throws InterruptedException * @throws KeeperException */ @Test public void getData() throws KeeperException, InterruptedException{ byte[] b = zk.getData("/app1", false, null ); System.out.println(new String(b)); } /** * 修改节点内容 * version -1 自动维护 * @throws InterruptedException * @throws KeeperException */ @Test public void setData() throws KeeperException, InterruptedException{ Stat s = zk.setData("/app1", "test".getBytes(), -1); } /** * 删除节点 * 非空节点删除不掉 * @throws InterruptedException * @throws KeeperException */ @Test public void deleteNode() throws InterruptedException, KeeperException{ zk.delete("/app1", -1); }
@Before public static void setUpBeforeClass() throws Exception { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { /** * 监听器 */ @Override public void process(WatchedEvent event) { System.out.println("事件触发..."); } }); } /** * 获取子节点 * getChildren(path,watch?)监听的事件是:节点下的子节点增减变化事件 * @throws InterruptedException * @throws KeeperException */ @Test public void getChilren() throws KeeperException, InterruptedException{ List<String> list = zk.getChildren("/", new Watcher() { @Override public void process(WatchedEvent event) { // TODO Auto-generated method stub System.out.println("监控节点下的子节点被改变..."+event.getPath()); } }); for (String s : list) { System.out.println(s); } Thread.sleep(Long.MAX_VALUE); } /** * 获取节点的内容 * getData(path,watch?)监听的事件是:节点数据变化事件 * @throws InterruptedException * @throws KeeperException */ @Test public void getData() throws KeeperException, InterruptedException{ byte[] b = zk.getData("/app1", new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("--数据改变了--"); } }, null ); System.out.println(new String(b)); Thread.sleep(Long.MAX_VALUE); }
原文:https://www.cnblogs.com/liyunfeng17/p/10928032.html