pom文件引入
<dependencies><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.14</version></dependency></dependencies>几个状态和概念
节点创建的权限信息ACL 参数的类型
- ANYONE_ID_UNSAFE : 表示任何?
- AUTH_IDS :此ID仅可?于设置ACL 。它将被客户机验证的ID替换 。
- OPEN_ACL_UNSAFE :这是?个完全开放的ACL(常?)-->world:anyone
- CREATOR_ALL_ACL :此ACL授予创建者身份验证ID的所有权限
节点的几种类型的枚举 - PERSISTENT:持久节点
- PERSISTENT_SEQUENTIAL:持久顺序节点
- EPHEMERAL:临时节点
- EPHEMERAL_SEQUENTIAL:临时顺序节点
public class CreateSession implements Watcher {private static CountDownLatch countDownLatch = new CountDownLatch(1);public static void main(String[] args) throws IOException, InterruptedException {ZooKeeper zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new CreateSession());System.out.println(zooKeeper.getState());countDownLatch.await();System.out.println("zk sessioncreate success");}@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getState()==Event.KeeperState.SyncConnected){countDownLatch.countDown();}}}正常创建回话
文章插图
zookeeper原生api获取信息操作
public class getNoetNode implements Watcher {privatestatic ZooKeeper zooKeeper;public static void main(String[] args) throws InterruptedException, IOException {zooKeeper = new ZooKeeper("119.45.52.68:2181",10000,new getNoetNode());//countDownLatch.await();Thread.sleep(Integer.MAX_VALUE);}@Overridepublic void process(WatchedEvent event) {if(event.getType() ==Event.EventType.NodeChildrenChanged){//再次获取子节点数据 , 监听子节点变化try {List<String> children =zooKeeper.getChildren(event.getPath(), true);System.out.println(children);} catch (KeeperException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}//当连接创建了 , 服务端发送给客户端SyncConnected事件if(event.getState() == Event.KeeperState.SyncConnected){try {//调?获取单个节点数据?法getNodeData();getChildren();} catch (Exception e) {e.printStackTrace();}}System.out.println();}private static void getNodeData() throws Exception {byte[] data = https://tazarkount.com/read/zooKeeper.getData("/persistent/children", true,null);System.out.println(new String(data,"utf-8"));}public staticvoid getChildren() throws Exception{List<String> childrens = zooKeeper.getChildren("persistent",true);System.out.println(childrens);}}zookeeper原生api更新节点信息public class UpdateNode implements Watcher {private static ZooKeeper zooKeeper;public static void main(String[] args) throws Exception{zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());Thread.sleep(Integer.MAX_VALUE);}@Overridepublic void process(WatchedEvent event) {try {byte[] before = zooKeeper.getData("/persistent/children",false,null);System.out.println("修改前的值"+new String(before));Stat stat = zooKeeper.setData("/persistent","客户端修改的内容".getBytes(),-1);System.out.println(stat);byte[] after = zooKeeper.getData("/persistent/children",false,null);System.out.println("修改后的值"+new String(after));}catch (Exception e){e.printStackTrace();}}}zookeeper原生api删除节点信息public class DeleteNode implements Watcher {private staticZooKeeper zooKeeper;public static void main(String[] args) throwsException{zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());Thread.sleep(Integer.MAX_VALUE);}@Overridepublic void process(WatchedEvent event) {try {Stat exists = zooKeeper.exists("/persistent/children", false);System.out.println(exists == null ? "该节点不存在":"该节点存在");zooKeeper.delete("/persistent/children",-1);Stat exists2 = zooKeeper.exists("/persistent/children", false);System.out.println(exists2 == null ? "该节点不存在":"该节点存在");}catch (Exception e){e.printStackTrace();}}}代码地址为https://github.com/zhendiao/deme-code/tree/main/zk欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】 , 以及总结的分类面试题https://github.com/zhendiao/JavaInterview
【zookeeper和eureka的区别 Zookeeper的原生api操作】

文章插图

文章插图
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
