From fca6114746124f56850d1d51ca0afe46ae0682f5 Mon Sep 17 00:00:00 2001 From: fangpeng <591930734@qq.com> Date: Wed, 9 Mar 2022 18:05:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8zookeeper=E7=9A=84?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AFapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zookeeper-demo/.gitignore | 46 +++++++ zookeeper-demo/pom.xml | 38 ++++++ .../main/java/com/lemon/api/CreateNode.java | 105 +++++++++++++++ .../java/com/lemon/api/CreateSession.java | 53 ++++++++ .../main/java/com/lemon/api/DeleteNote.java | 89 ++++++++++++ .../main/java/com/lemon/api/GetNoteData.java | 127 ++++++++++++++++++ .../java/com/lemon/api/UpdateNoteData.java | 89 ++++++++++++ .../java/com/lemon/curator/CreateSession.java | 9 ++ .../com/lemon/zkclient/CreateSession.java | 28 ++++ .../java/com/lemon/zkclient/Create_Note.java | 35 +++++ .../java/com/lemon/zkclient/Delete_Note.java | 34 +++++ .../com/lemon/zkclient/Get_NoteChildren.java | 60 +++++++++ .../java/com/lemon/zkclient/Note_API.java | 71 ++++++++++ 13 files changed, 784 insertions(+) create mode 100644 zookeeper-demo/.gitignore create mode 100644 zookeeper-demo/pom.xml create mode 100644 zookeeper-demo/src/main/java/com/lemon/api/CreateNode.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/api/CreateSession.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/api/DeleteNote.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/api/GetNoteData.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/api/UpdateNoteData.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/zkclient/CreateSession.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/zkclient/Create_Note.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/zkclient/Delete_Note.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/zkclient/Get_NoteChildren.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/zkclient/Note_API.java diff --git a/zookeeper-demo/.gitignore b/zookeeper-demo/.gitignore new file mode 100644 index 00000000..1616a87e --- /dev/null +++ b/zookeeper-demo/.gitignore @@ -0,0 +1,46 @@ +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen diff --git a/zookeeper-demo/pom.xml b/zookeeper-demo/pom.xml new file mode 100644 index 00000000..8c70fefc --- /dev/null +++ b/zookeeper-demo/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.lemon + zookeeper-demo + 1.0-SNAPSHOT + + + 8 + 8 + + + + + org.apache.zookeeper + zookeeper + 3.6.3 + + + + com.101tec + zkclient + 0.11 + + + + org.apache.curator + curator-framework + 2.13.0 + + + + + + \ No newline at end of file diff --git a/zookeeper-demo/src/main/java/com/lemon/api/CreateNode.java b/zookeeper-demo/src/main/java/com/lemon/api/CreateNode.java new file mode 100644 index 00000000..8b476030 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/api/CreateNode.java @@ -0,0 +1,105 @@ +package com.lemon.api; + +import org.apache.zookeeper.*; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +/** + * @Description: TODO + * @Author : Lemon-CS + * @Date : 2022年03月09日 5:38 下午 + */ +public class CreateNode implements Watcher { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + private static ZooKeeper zooKeeper; + + + /* + 建立会话 + */ + public static void main(String[] args) throws IOException, InterruptedException, KeeperException { + + /* + 客户端可以通过创建一个zk实例来连接zk服务器 + new Zookeeper(connectString,sesssionTimeOut,Wather) + connectString: 连接地址:IP:端口 + sesssionTimeOut:会话超时时间:单位毫秒 + Wather:监听器(当特定事件触发监听时,zk会通过watcher通知到客户端) + */ + + zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new CreateNode()); + System.out.println(zooKeeper.getState()); + + // 计数工具类:CountDownLatch:不让main方法结束,让线程处于等待阻塞 + //countDownLatch.await();\ + Thread.sleep(Integer.MAX_VALUE); + + } + + + + /* + 回调方法:处理来自服务器端的watcher通知 + */ + @Override + public void process(WatchedEvent watchedEvent) { + // SyncConnected + if(watchedEvent.getState() == Event.KeeperState.SyncConnected){ + + //解除主程序在CountDownLatch上的等待阻塞 + System.out.println("process方法执行了..."); + // 创建节点 + try { + createNoteSync(); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + + } + + + /* + 创建节点的方法 + */ + private static void createNoteSync() throws KeeperException, InterruptedException { + + /** + * path :节点创建的路径 + * data[] :节点创建要保存的数据,是个byte类型的 + * acl :节点创建的权限信息(4种类型) + * ANYONE_ID_UNSAFE : 表示任何人 + * AUTH_IDS :此ID仅可用于设置ACL。它将被客户机验证的ID替换。 + * OPEN_ACL_UNSAFE :这是一个完全开放的ACL(常用)--> world:anyone + * CREATOR_ALL_ACL :此ACL授予创建者身份验证ID的所有权限 + * createMode :创建节点的类型(4种类型) + * PERSISTENT:持久节点 + * PERSISTENT_SEQUENTIAL:持久顺序节点 + * EPHEMERAL:临时节点 + * EPHEMERAL_SEQUENTIAL:临时顺序节点 + String node = zookeeper.create(path,data,acl,createMode); + */ + + // 持久节点 + String note_persistent = zooKeeper.create("/lg-persistent", "持久节点内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // 临时节点 + String note_ephemeral = zooKeeper.create("/lg-ephemeral", "临时节点内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + // 持久顺序节点 + String note_persistent_sequential = zooKeeper.create("/lg-persistent_sequential", "持久顺序节点内容".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + + System.out.println("创建的持久节点" + note_persistent); + System.out.println("创建的临时节点" + note_ephemeral); + System.out.println("创建的持久顺序节点" + note_persistent_sequential); + + } +} + diff --git a/zookeeper-demo/src/main/java/com/lemon/api/CreateSession.java b/zookeeper-demo/src/main/java/com/lemon/api/CreateSession.java new file mode 100644 index 00000000..5e07738d --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/api/CreateSession.java @@ -0,0 +1,53 @@ +package com.lemon.api; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +/** + * @Description: 建立会话 + * @Author : Lemon-CS + * @Date : 2022年03月09日 5:25 下午 + */ +public class CreateSession implements Watcher { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + /* + 建立会话 + */ + public static void main(String[] args) throws IOException, InterruptedException { + + /* + * 客户端可以通过创建一个zk实例来连接zk服务器 + * new Zookeeper(connectString,sesssionTimeOut,Wather) + * connectString: 连接地址:IP:端口 + * sesssionTimeOut:会话超时时间:单位毫秒 + * Wather:监听器(当特定事件触发监听时,zk会通过watcher通知到客户端) + */ + ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new CreateSession()); + System.out.println(zooKeeper.getState()); + + // 计数工具类:CountDownLatch:不让main方法结束,让线程处于等待阻塞 + countDownLatch.await(); + + System.out.println("客户端与服务端会话真正建立了: " + zooKeeper.getState()); + + } + + /* + 回调方法:处理来自服务器端的watcher通知 + */ + @Override + public void process(WatchedEvent watchedEvent) { + // SyncConnected + if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { + //解除主程序在CountDownLatch上的等待阻塞 + System.out.println("process方法执行了..."); + countDownLatch.countDown(); + } + } +} diff --git a/zookeeper-demo/src/main/java/com/lemon/api/DeleteNote.java b/zookeeper-demo/src/main/java/com/lemon/api/DeleteNote.java new file mode 100644 index 00000000..ed54c2da --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/api/DeleteNote.java @@ -0,0 +1,89 @@ +package com.lemon.api; + +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.Stat; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +public class DeleteNote implements Watcher { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + private static ZooKeeper zooKeeper; + + + /* + 建立会话 + */ + public static void main(String[] args) throws IOException, InterruptedException, KeeperException { + + /* + 客户端可以通过创建一个zk实例来连接zk服务器 + new Zookeeper(connectString,sesssionTimeOut,Wather) + connectString: 连接地址:IP:端口 + sesssionTimeOut:会话超时时间:单位毫秒 + Wather:监听器(当特定事件触发监听时,zk会通过watcher通知到客户端) + */ + + zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new DeleteNote()); + System.out.println(zooKeeper.getState()); + + // 计数工具类:CountDownLatch:不让main方法结束,让线程处于等待阻塞 + //countDownLatch.await();\ + Thread.sleep(Integer.MAX_VALUE); + + } + + + + /* + 回调方法:处理来自服务器端的watcher通知 + */ + public void process(WatchedEvent watchedEvent) { + // SyncConnected + if(watchedEvent.getState() == Event.KeeperState.SyncConnected){ + + //解除主程序在CountDownLatch上的等待阻塞 + System.out.println("process方法执行了..."); + // 删除节点 + try { + deleteNoteSync(); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + + } + + + } + + /* + 删除节点的方法 + */ + private void deleteNoteSync() throws KeeperException, InterruptedException { + + /* + zooKeeper.exists(path,watch) :判断节点是否存在 + zookeeper.delete(path,version) : 删除节点 + */ + + Stat stat = zooKeeper.exists("/lg-persistent/c1", false); + System.out.println(stat == null ? "该节点不存在":"该节点存在"); + + if(stat != null){ + zooKeeper.delete("/lg-persistent/c1",-1); + } + + Stat stat2 = zooKeeper.exists("/lg-persistent/c1", false); + System.out.println(stat2 == null ? "该节点不存在":"该节点存在"); + + + + } + + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/api/GetNoteData.java b/zookeeper-demo/src/main/java/com/lemon/api/GetNoteData.java new file mode 100644 index 00000000..e8d65d07 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/api/GetNoteData.java @@ -0,0 +1,127 @@ +package com.lemon.api; + +import org.apache.zookeeper.*; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +public class GetNoteData implements Watcher { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + private static ZooKeeper zooKeeper; + + + /* + 建立会话 + */ + public static void main(String[] args) throws IOException, InterruptedException, KeeperException { + + /* + 客户端可以通过创建一个zk实例来连接zk服务器 + new Zookeeper(connectString,sesssionTimeOut,Wather) + connectString: 连接地址:IP:端口 + sesssionTimeOut:会话超时时间:单位毫秒 + Wather:监听器(当特定事件触发监听时,zk会通过watcher通知到客户端) + */ + + zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new GetNoteData()); + System.out.println(zooKeeper.getState()); + + // 计数工具类:CountDownLatch:不让main方法结束,让线程处于等待阻塞 + //countDownLatch.await();\ + Thread.sleep(Integer.MAX_VALUE); + + } + + + + /* + 回调方法:处理来自服务器端的watcher通知 + */ + public void process(WatchedEvent watchedEvent) { + + /* + 子节点列表发生改变时,服务器端会发生noteChildrenChanged事件通知 + 要重新获取子节点列表,同时注意:通知是一次性的,需要反复注册监听 + */ + if(watchedEvent.getType() == Event.EventType.NodeChildrenChanged){ + + List children = null; + try { + children = zooKeeper.getChildren("/lg-persistent", true); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(children); + + } + + + + // SyncConnected + if(watchedEvent.getState() == Event.KeeperState.SyncConnected){ + + //解除主程序在CountDownLatch上的等待阻塞 + System.out.println("process方法执行了..."); + + // 获取节点数据的方法 + try { + getNoteData(); + + // 获取节点的子节点列表方法 + getChildrens(); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + + } + + + } + + /* + 获取某个节点的内容 + */ + private void getNoteData() throws KeeperException, InterruptedException { + + /** + * path : 获取数据的路径 + * watch : 是否开启监听 + * stat : 节点状态信息 + * null: 表示获取最新版本的数据 + * zk.getData(path, watch, stat); + */ + byte[] data = zooKeeper.getData("/lg-persistent", false, null); + System.out.println(new String(data)); + + + } + + + /* + 获取某个节点的子节点列表方法 + */ + public static void getChildrens() throws KeeperException, InterruptedException { + + /* + path:路径 + watch:是否要启动监听,当子节点列表发生变化,会触发监听 + zooKeeper.getChildren(path, watch); + */ + List children = zooKeeper.getChildren("/lg-persistent", true); + System.out.println(children); + + + + } + + + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/api/UpdateNoteData.java b/zookeeper-demo/src/main/java/com/lemon/api/UpdateNoteData.java new file mode 100644 index 00000000..6308d4f6 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/api/UpdateNoteData.java @@ -0,0 +1,89 @@ +package com.lemon.api; + +import org.apache.zookeeper.*; +import org.apache.zookeeper.data.Stat; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +public class UpdateNoteData implements Watcher { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + private static ZooKeeper zooKeeper; + + + /* + 建立会话 + */ + public static void main(String[] args) throws IOException, InterruptedException, KeeperException { + + /* + 客户端可以通过创建一个zk实例来连接zk服务器 + new Zookeeper(connectString,sesssionTimeOut,Wather) + connectString: 连接地址:IP:端口 + sesssionTimeOut:会话超时时间:单位毫秒 + Wather:监听器(当特定事件触发监听时,zk会通过watcher通知到客户端) + */ + + zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, new UpdateNoteData()); + System.out.println(zooKeeper.getState()); + + // 计数工具类:CountDownLatch:不让main方法结束,让线程处于等待阻塞 + //countDownLatch.await();\ + Thread.sleep(Integer.MAX_VALUE); + + } + + + + /* + 回调方法:处理来自服务器端的watcher通知 + */ + public void process(WatchedEvent watchedEvent) { + // SyncConnected + if(watchedEvent.getState() == Event.KeeperState.SyncConnected){ + + //解除主程序在CountDownLatch上的等待阻塞 + System.out.println("process方法执行了..."); + + // 更新数据节点内容的方法 + try { + updateNoteSync(); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + + } + + /* + 更新数据节点内容的方法 + */ + private void updateNoteSync() throws KeeperException, InterruptedException { + + /* + path:路径 + data:要修改的内容 byte[] + version:为-1,表示对最新版本的数据进行修改 + zooKeeper.setData(path, data,version); + */ + + + byte[] data = zooKeeper.getData("/lg-persistent", false, null); + System.out.println("修改前的值:" + new String(data)); + + //修改/lg-persistent 的数据 stat: 状态信息对象 + Stat stat = zooKeeper.setData("/lg-persistent", "客户端修改了节点数据".getBytes(), -1); + + byte[] data2 = zooKeeper.getData("/lg-persistent", false, null); + System.out.println("修改后的值:" + new String(data2)); + + } + + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java b/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java new file mode 100644 index 00000000..0df4d76c --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java @@ -0,0 +1,9 @@ +package com.lemon.curator; + +/** + * @Description: TODO + * @Author : Lemon-CS + * @Date : 2022年03月09日 6:04 下午 + */ +public class CreateSession { +} diff --git a/zookeeper-demo/src/main/java/com/lemon/zkclient/CreateSession.java b/zookeeper-demo/src/main/java/com/lemon/zkclient/CreateSession.java new file mode 100644 index 00000000..4fb10214 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/zkclient/CreateSession.java @@ -0,0 +1,28 @@ +package com.lemon.zkclient; + +import org.I0Itec.zkclient.ZkClient; + +public class CreateSession { + + + /* + 借助zkclient完成会话的创建 + */ + public static void main(String[] args) { + + /* + 创建一个zkclient实例就可以完成连接,完成会话的创建 + serverString : 服务器连接地址 + + 注意:zkClient通过对zookeeperAPI内部封装,将这个异步创建会话的过程同步化了.. + */ + + ZkClient zkClient = new ZkClient("127.0.0.1:2181"); + System.out.println("会话被创建了.."); + + + + + } + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/zkclient/Create_Note.java b/zookeeper-demo/src/main/java/com/lemon/zkclient/Create_Note.java new file mode 100644 index 00000000..1c67a24c --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/zkclient/Create_Note.java @@ -0,0 +1,35 @@ +package com.lemon.zkclient; + +import org.I0Itec.zkclient.ZkClient; + +public class Create_Note { + + + /* + 借助zkclient完成会话的创建 + */ + public static void main(String[] args) { + + /* + 创建一个zkclient实例就可以完成连接,完成会话的创建 + serverString : 服务器连接地址 + + 注意:zkClient通过对zookeeperAPI内部封装,将这个异步创建会话的过程同步化了.. + */ + + ZkClient zkClient = new ZkClient("127.0.0.1:2181"); + System.out.println("会话被创建了.."); + + //创建节点 + /* + cereateParents : 是否要创建父节点,如果值为true,那么就会递归创建节点 + */ + zkClient.createPersistent("/lg-zkclient/c1",true); + System.out.println("节点递归创建完成"); + + + + + } + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/zkclient/Delete_Note.java b/zookeeper-demo/src/main/java/com/lemon/zkclient/Delete_Note.java new file mode 100644 index 00000000..a5214ebf --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/zkclient/Delete_Note.java @@ -0,0 +1,34 @@ +package com.lemon.zkclient; + +import org.I0Itec.zkclient.ZkClient; + +public class Delete_Note { + + + /* + 借助zkclient完成会话的创建 + */ + public static void main(String[] args) { + + /* + 创建一个zkclient实例就可以完成连接,完成会话的创建 + serverString : 服务器连接地址 + + 注意:zkClient通过对zookeeperAPI内部封装,将这个异步创建会话的过程同步化了.. + */ + + ZkClient zkClient = new ZkClient("127.0.0.1:2181"); + System.out.println("会话被创建了.."); + + // 递归删除节点 + String path = "/lg-zkclient/c1"; + zkClient.createPersistent(path+"/c11"); + zkClient.deleteRecursive(path); + System.out.println("递归删除成功"); + + + + + } + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/zkclient/Get_NoteChildren.java b/zookeeper-demo/src/main/java/com/lemon/zkclient/Get_NoteChildren.java new file mode 100644 index 00000000..6306ce6c --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/zkclient/Get_NoteChildren.java @@ -0,0 +1,60 @@ +package com.lemon.zkclient; + +import org.I0Itec.zkclient.IZkChildListener; +import org.I0Itec.zkclient.ZkClient; + +import java.util.List; + +public class Get_NoteChildren { + + + /* + 借助zkclient完成会话的创建 + */ + public static void main(String[] args) throws InterruptedException { + + /* + 创建一个zkclient实例就可以完成连接,完成会话的创建 + serverString : 服务器连接地址 + + 注意:zkClient通过对zookeeperAPI内部封装,将这个异步创建会话的过程同步化了.. + */ + + ZkClient zkClient = new ZkClient("127.0.0.1:2181"); + System.out.println("会话被创建了.."); + + // 获取子节点列表 + List children = zkClient.getChildren("/lg-zkclient"); + System.out.println(children); + + // 注册监听事件 + + /* + 客户端可以对一个不存在的节点进行子节点变更的监听 + 只要该节点的子节点列表发生变化,或者该节点本身被创建或者删除,都会触发监听 + */ + zkClient.subscribeChildChanges("/lg-zkclient-get", new IZkChildListener() { + + /* + s : parentPath + list : 变化后子节点列表 + */ + @Override + public void handleChildChange(String parentPath, List list) throws Exception { + System.out.println(parentPath + "的子节点列表发生了变化,变化后的子节点列表为"+ list); + + } + }); + + //测试 + zkClient.createPersistent("/lg-zkclient-get"); + Thread.sleep(1000); + + zkClient.createPersistent("/lg-zkclient-get/c1"); + Thread.sleep(1000); + + + + } + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/zkclient/Note_API.java b/zookeeper-demo/src/main/java/com/lemon/zkclient/Note_API.java new file mode 100644 index 00000000..a9ebd562 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/zkclient/Note_API.java @@ -0,0 +1,71 @@ +package com.lemon.zkclient; + +import org.I0Itec.zkclient.IZkDataListener; +import org.I0Itec.zkclient.ZkClient; + +public class Note_API { + + + /* + 借助zkclient完成会话的创建 + */ + public static void main(String[] args) throws InterruptedException { + + /* + 创建一个zkclient实例就可以完成连接,完成会话的创建 + serverString : 服务器连接地址 + + 注意:zkClient通过对zookeeperAPI内部封装,将这个异步创建会话的过程同步化了.. + */ + + ZkClient zkClient = new ZkClient("127.0.0.1:2181"); + System.out.println("会话被创建了.."); + + // 判断节点是否存在 + String path = "/lg-zkClient-Ep"; + boolean exists = zkClient.exists(path); + + if(!exists){ + // 创建临时节点 + zkClient.createEphemeral(path,"123"); + } + + // 读取节点内容 + Object o = zkClient.readData(path); + System.out.println(o); + + + // 注册监听 + zkClient.subscribeDataChanges(path, new IZkDataListener() { + + /* + 当节点数据内容发生变化时,执行的回调方法 + s: path + o: 变化后的节点内容 + */ + public void handleDataChange(String s, Object o) throws Exception { + System.out.println(s+"该节点内容被更新,更新的内容"+o); + } + + /* + 当节点被删除时,会执行的回调方法 + s : path + */ + public void handleDataDeleted(String s) throws Exception { + System.out.println(s+"该节点被删除"); + } + }); + + + // 更新节点内容 + zkClient.writeData(path,"456"); + Thread.sleep(1000); + + // 删除节点 + zkClient.delete(path); + Thread.sleep(1000); + + + } + +} From 9820e0fa2392690b8b2a8e828dbd36d17e66a369 Mon Sep 17 00:00:00 2001 From: fangpeng <591930734@qq.com> Date: Wed, 9 Mar 2022 19:27:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8zookeeper=E7=9A=84?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AFcurator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/lemon/curator/CreateNote_curator.java | 46 ++++++++++++++ .../java/com/lemon/curator/CreateSession.java | 31 ++++++++- .../com/lemon/curator/DeleteNote_curator.java | 46 ++++++++++++++ .../com/lemon/curator/GetNote_curator.java | 56 +++++++++++++++++ .../com/lemon/curator/UpdateNote_curator.java | 63 +++++++++++++++++++ 5 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 zookeeper-demo/src/main/java/com/lemon/curator/CreateNote_curator.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/curator/DeleteNote_curator.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/curator/GetNote_curator.java create mode 100644 zookeeper-demo/src/main/java/com/lemon/curator/UpdateNote_curator.java diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/CreateNote_curator.java b/zookeeper-demo/src/main/java/com/lemon/curator/CreateNote_curator.java new file mode 100644 index 00000000..d4007261 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/curator/CreateNote_curator.java @@ -0,0 +1,46 @@ +package com.lemon.curator; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +/** + * @Description: TODO + * @Author : Lemon-CS + * @Date : 2022年03月09日 6:10 下午 + */ +public class CreateNote_curator { + + + // 创建会话 + public static void main(String[] args) throws Exception { + + + //不使用fluent编程风格 + + RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3); + + // 使用fluent编程风格 + CuratorFramework client = CuratorFrameworkFactory.builder() + .connectString("127.0.0.1:2181") + .sessionTimeoutMs(50000) + .connectionTimeoutMs(30000) + .retryPolicy(exponentialBackoffRetry) + .namespace("base") // 独立的命名空间 /base + .build(); + + client.start(); + + System.out.println("连接会话创建了"); + + // 创建节点 + String path = "/lemon-curator/c1"; + String s = client.create().creatingParentsIfNeeded() + .withMode(CreateMode.PERSISTENT).forPath(path, "test-curator".getBytes()); + + System.out.println("节点递归创建成功,该节点路径" + s); + } + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java b/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java index 0df4d76c..df560a60 100644 --- a/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java +++ b/zookeeper-demo/src/main/java/com/lemon/curator/CreateSession.java @@ -1,9 +1,38 @@ package com.lemon.curator; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; + /** - * @Description: TODO + * @Description: 创建会话 * @Author : Lemon-CS * @Date : 2022年03月09日 6:04 下午 */ public class CreateSession { + + // 创建会话 + public static void main(String[] args) { + + //不使用fluent编程风格 + RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3); + CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", exponentialBackoffRetry); + curatorFramework.start(); + System.out.println( "会话被建立了"); + + // 使用fluent编程风格 + CuratorFramework client = CuratorFrameworkFactory.builder() + .connectString("127.0.0.1:2181") + .sessionTimeoutMs(50000) + .connectionTimeoutMs(30000) + .retryPolicy(exponentialBackoffRetry) + .namespace("base") // 独立的命名空间 /base + .build(); + + client.start(); + System.out.println("会话2创建了"); + + } + } diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/DeleteNote_curator.java b/zookeeper-demo/src/main/java/com/lemon/curator/DeleteNote_curator.java new file mode 100644 index 00000000..a92bb945 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/curator/DeleteNote_curator.java @@ -0,0 +1,46 @@ +package com.lemon.curator; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; + +public class DeleteNote_curator { + + // 创建会话 + public static void main(String[] args) throws Exception { + + + //不使用fluent编程风格 + + RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3); + CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", exponentialBackoffRetry); + curatorFramework.start(); + System.out.println( "会话被建立了"); + + // 使用fluent编程风格 + CuratorFramework client = CuratorFrameworkFactory.builder() + .connectString("127.0.0.1:2181") + .sessionTimeoutMs(50000) + .connectionTimeoutMs(30000) + .retryPolicy(exponentialBackoffRetry) + .namespace("base") // 独立的命名空间 /base + .build(); + + client.start(); + + System.out.println("会话2创建了"); + + + // 删除节点 + String path = "/lg-curator"; + client.delete().deletingChildrenIfNeeded().withVersion(-1).forPath(path); + + System.out.println("删除成功,删除的节点" + path); + + + + } + + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/GetNote_curator.java b/zookeeper-demo/src/main/java/com/lemon/curator/GetNote_curator.java new file mode 100644 index 00000000..9a02bea7 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/curator/GetNote_curator.java @@ -0,0 +1,56 @@ +package com.lemon.curator; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.data.Stat; + +public class GetNote_curator { + + // 创建会话 + public static void main(String[] args) throws Exception { + + + //不使用fluent编程风格 + + RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3); + + // 使用fluent编程风格 + CuratorFramework client = CuratorFrameworkFactory.builder() + .connectString("127.0.0.1:2181") + .sessionTimeoutMs(50000) + .connectionTimeoutMs(30000) + .retryPolicy(exponentialBackoffRetry) + .namespace("base") // 独立的命名空间 /base + .build(); + + client.start(); + + System.out.println("会话2创建了"); + + // 创建节点 + String path = "/lg-curator/c1"; + String s = client.create().creatingParentsIfNeeded() + .withMode(CreateMode.PERSISTENT).forPath(path, "init".getBytes()); + + System.out.println("节点递归创建成功,该节点路径" + s); + + // 获取节点的数据内容及状态信息 + + // 数据内容 + byte[] bytes = client.getData().forPath(path); + System.out.println("获取到的节点数据内容:" + new String(bytes)); + + // 状态信息 + Stat stat = new Stat(); + client.getData().storingStatIn(stat).forPath(path); + + System.out.println("获取到的节点状态信息:" + stat ); + + + } + + +} diff --git a/zookeeper-demo/src/main/java/com/lemon/curator/UpdateNote_curator.java b/zookeeper-demo/src/main/java/com/lemon/curator/UpdateNote_curator.java new file mode 100644 index 00000000..104a4036 --- /dev/null +++ b/zookeeper-demo/src/main/java/com/lemon/curator/UpdateNote_curator.java @@ -0,0 +1,63 @@ +package com.lemon.curator; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.data.Stat; + +public class UpdateNote_curator { + + // 创建会话 + public static void main(String[] args) throws Exception { + + + //不使用fluent编程风格 + + RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3); + + // 使用fluent编程风格 + CuratorFramework client = CuratorFrameworkFactory.builder() + .connectString("127.0.0.1:2181") + .sessionTimeoutMs(50000) + .connectionTimeoutMs(30000) + .retryPolicy(exponentialBackoffRetry) + .namespace("base") // 独立的命名空间 /base + .build(); + + client.start(); + + System.out.println("会话2创建了"); + + // 创建节点 + String path = "/lg-curator/c1"; + + // 获取节点的数据内容及状态信息 + + // 数据内容 + byte[] bytes = client.getData().forPath(path); + System.out.println("获取到的节点数据内容:" + new String(bytes)); + + // 状态信息 + Stat stat = new Stat(); //0 + client.getData().storingStatIn(stat).forPath(path); + + System.out.println("获取到的节点状态信息:" + stat ); + + // 更新节点内容 //1 + int version = client.setData().withVersion(stat.getVersion()).forPath(path, "修改内容1".getBytes()).getVersion(); + System.out.println("当前的最新版本是" + version); + byte[] bytes2 = client.getData().forPath(path); + System.out.println("修改后的节点数据内容:" + new String(bytes2)); + + // BadVersionException + client.setData().withVersion(stat.getVersion()).forPath(path,"修改内容2".getBytes()); + + + + + + } + + +}