日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Ⅴ:zookeeper的相关Java Api

發布時間:2024/10/5 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ⅴ:zookeeper的相关Java Api 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2021最新zookeeper系列

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

Ⅰ:zookeeper的單機安裝 - 詳細教程:https://blog.csdn.net/Kevinnsm/article/details/116134397?spm=1001.2014.3001.5501

Ⅱ:zookeeper的相關shell命令:https://blog.csdn.net/Kevinnsm/article/details/116137602?spm=1001.2014.3001.5501

Ⅲ:zookeeper之查看節點的狀態信息:https://blog.csdn.net/Kevinnsm/article/details/116143218?spm=1001.2014.3001.5501

Ⅳ:zookeeper的acl權限控制:https://blog.csdn.net/Kevinnsm/article/details/116167394?spm=1001.2014.3001.5501

Ⅴ:zookeeper的相關Java Api:https://blog.csdn.net/Kevinnsm/article/details/116462557?spm=1001.2014.3001.5501

Ⅵ:zookeeper的Watcher事件監聽機制:https://blog.csdn.net/Kevinnsm/article/details/116501842?spm=1001.2014.3001.5501

Ⅶ:教你一招利用zookeeper作為服務的配置中心:https://blog.csdn.net/Kevinnsm/article/details/116542974?spm=1001.2014.3001.5501

?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

文章目錄

  • 前置準備
  • 一、API之創建節點
    • 1、API之word授權模式
    • 2、API之IP授權模式
    • 3、API之auth授權模式
    • 4、API之digest授權模式
  • 二、API之修改節點
    • 1、同步更新
    • 2、異步更新
  • 三、API之刪除節點
    • 1、同步刪除
    • 2、異步刪除
  • 四、API之查看節點
    • 1、同步查看
    • 1、異步查看
  • 五、API之查看所有子節點
    • 1、同步查看
    • 2、異步查看
  • 六、API之查看節點是否存在
    • 1、同步查看
    • 2、異步查看


# 前置:--》把握住Watcher流程《--

1、連接zookeeper服務器
2、連接時必須使當前線程等待(等待其他線程創建連接zookeeper服務成功,使用計數器實現)
3、執行回調函數process
4、釋放當前線程

前置準備

如果不知道zookeeper怎么安裝,請移步zookeeper的安裝步驟:https://blog.csdn.net/Kevinnsm/article/details/116134397?spm=1001.2014.3001.5501

一、API之創建節點

使用@Before和@After注解

該注解的作用是:當在有@Test注解的條件下,首先會執行@Before標注的方法,然后執行@Test注解標注的方法,最后執行@After標注的方法.

由于每次都要進行zookeeper服務的連接和關閉,所以使用@Before和@After注解進行有順序的連接和關閉。

package com.zookeeper;import com.sun.xml.internal.bind.v2.model.core.ID; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch;/*** @author:抱著魚睡覺的喵喵* @date:2021/4/28* @description:*/ public class CreateNode {private String IP = "123.57.252.59:2181";private ZooKeeper zookeeper;@Beforepublic void connection() throws IOException, InterruptedException {//CountDownLatch :使一個線程等待其他線程各自執行完畢后再執行final CountDownLatch downLatch = new CountDownLatch(1);//Watcher是一個回調函數zookeeper = new ZooKeeper("123.57.252.59:2181", 10000, new Watcher() {public void process(WatchedEvent watchedEvent) {if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {System.out.println("連接成功!");downLatch.countDown();}}});downLatch.await();}//關閉zookeeper連接@Afterpublic void close() {try {zookeeper.close();} catch (InterruptedException e) {e.printStackTrace();}} }

1、API之word授權模式

/*** world授權模式* @throws KeeperException* @throws InterruptedException*/@Testpublic void create3() throws KeeperException, InterruptedException {//權限列表List<ACL> acls = new ArrayList<ACL>();//授權模式和授權對象Id id = new Id("world", "anyone");//授權設置 (讀和創建的權限)acls.add(new ACL(ZooDefs.Perms.READ, id));acls.add(new ACL(ZooDefs.Perms.CREATE, id));//PERSISTENT:持久化節點zookeeper.create("/unity/node/3", "node3".getBytes(), acls, CreateMode.PERSISTENT);}

2、API之IP授權模式

/*** ip授權模式* @throws KeeperException* @throws InterruptedException*/@Testpublic void create4() throws KeeperException, InterruptedException {List<ACL> acls = new ArrayList<ACL>();Id id = new Id("ip", "8.140.37.103");acls.add(new ACL(ZooDefs.Perms.READ, id));zookeeper.create("/unity/nod4", "node4".getBytes(), acls, CreateMode.PERSISTENT);}

3、API之auth授權模式

one:

/*** auth授權模式* @throws KeeperException* @throws InterruptedException*/@Testpublic void create5() throws KeeperException, InterruptedException {zookeeper.addAuthInfo("digest", "user:user".getBytes());zookeeper.create("/unity/node5", "node5".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);}

two:

/*** auth授權模式* @throws KeeperException* @throws InterruptedException*/@Testpublic void create6() throws KeeperException, InterruptedException {zookeeper.addAuthInfo("digest", "user:user".getBytes());List<ACL> acls = new ArrayList<ACL>();Id id = new Id("auth", "user");acls.add(new ACL(ZooDefs.Perms.ALL, id));zookeeper.create("/unity/node6", "node6".getBytes(), acls, CreateMode.PERSISTENT);}

4、API之digest授權模式

/*** digest授權模式* @throws KeeperException* @throws InterruptedException*/@Testpublic void create7() throws KeeperException, InterruptedException {//權限列表List<ACL> acls = new ArrayList<ACL>();//授權模式和授權對象Id id = new Id("digest", "GR9f4mKrV2reacyCyiukMpZl5qc=");//授權設置acls.add(new ACL(ZooDefs.Perms.ALL, id));zookeeper.create("/node7","node7".getBytes(), acls, CreateMode.PERSISTENT);}

二、API之修改節點

setData(String path, bytes[] data, int dataVersion)
setData(String path, bytes[] data, int dataVersion, AsyncCallback.StatCallback)

連接和關閉

package com.zookeeper;import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.util.concurrent.CountDownLatch;/*** @author:抱著魚睡覺的喵喵* @date:2021/4/28* @description:*/ public class UpdateNode {private String IP = "123.57.252.59:2181";private ZooKeeper zookeeper;@Beforepublic void connection() throws IOException, InterruptedException {final CountDownLatch downLatch = new CountDownLatch(1);zookeeper = new ZooKeeper("123.57.252.59:2181", 10000, new Watcher() {public void process(WatchedEvent watchedEvent) {if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {System.out.println("連接成功!");downLatch.countDown();}}});downLatch.await();}@Afterpublic void close() {try {zookeeper.close();} catch (InterruptedException e) {e.printStackTrace();}} }

1、同步更新

/*** -1 代表不做修改,不做修改不是版本號不變(只是不需要對應刪除節點的版本號了;因為刪除節點需要版本號對應,否者會刪除失敗)* 更新節點* @throws KeeperException* @throws InterruptedException*/@Testpublic void update() throws KeeperException, InterruptedException {zookeeper.setData("/update/node1", "node-one".getBytes(), 0);} @Testpublic void update2() throws KeeperException, InterruptedException {Stat stat = zookeeper.setData("/update/node2", "node-two".getBytes(), 0);System.out.println(stat.getCversion());System.out.println(stat.getVersion());}

2、異步更新

@Testpublic void update3() {zookeeper.setData("/update/node3", "node3-three".getBytes(), 1, new AsyncCallback.StatCallback() {//回調函數public void processResult(int i, String s, Object o, Stat stat) {// 0代表修改成功System.out.println(i);//s代碼pathSystem.out.println(s);//o代表上下文對象,也就是下面的context字符串System.out.println(o);//stat代表屬性描述對象System.out.println(stat.getVersion());}}, "context");}

三、API之刪除節點

delete(String path,int dataVersion)
delete(String path,int dataVersion,AsyncCallback.VoidCallback)

1、同步刪除

@Testpublic void delete() throws KeeperException, InterruptedException {zookeeper.delete("/delete/node1", -1);}

2、異步刪除

@Testpublic void delete2() throws InterruptedException {zookeeper.delete("/delete/node2", 0, new AsyncCallback.VoidCallback() {public void processResult(int i, String s, Object o) {System.out.println(i);System.out.println(s);System.out.println(o);}},"context");Thread.sleep(10000);System.out.println("結束!");}

四、API之查看節點

getData(String path,boolean watcher,Stat stat)
getData(String path,boolean watcher, AsyncCallback.DataCallback)

1、同步查看

@Testpublic void get() throws KeeperException, InterruptedException {Stat stat = new Stat();byte[] data = zookeeper.getData("/get/node1", false, stat);System.out.println(new String(data));System.out.println(stat.getVersion());}

1、異步查看

@Testpublic void get2() {zookeeper.getData("/get/node1", false, new AsyncCallback.DataCallback() {public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {System.out.println(i);System.out.println(s);System.out.println(o);System.out.println(new String(bytes));System.out.println(stat.getVersion());}}, "context");}

五、API之查看所有子節點

getChildren(String path,bookean watcher)
getChildren(String path,boolean watcher,AsyncCallback.ChildrenCallback)

1、同步查看

@Testpublic void getChild() throws KeeperException, InterruptedException {List<String> list = zookeeper.getChildren("/get", false);for (String temp : list) {System.out.println(temp);}}

2、異步查看

@Testpublic void getChild2() throws InterruptedException {zookeeper.getChildren("/get", false, new AsyncCallback.ChildrenCallback() {public void processResult(int i, String s, Object o, List<String> list) {System.out.println(i);System.out.println(s);System.out.println(o);for (String cur : list)System.out.println(cur);}}, "context");Thread.sleep(1000);}

六、API之查看節點是否存在

exists(String path,boolean watcher)
exists(String path,boolean watcher,AsyncCallback.StatCallback)

1、同步查看

@Testpublic void exists() throws KeeperException, InterruptedException {Stat exists = zookeeper.exists("/get/node1", false);System.out.println(exists);}

2、異步查看

@Testpublic void exists2() throws InterruptedException {zookeeper.exists("/get/node1", false, new AsyncCallback.StatCallback() {public void processResult(int i, String s, Object o, Stat stat) {System.out.println(i);System.out.println(s);System.out.println(o);System.out.println(stat.getVersion());}}, "context");Thread.sleep(1000);}

總結

以上是生活随笔為你收集整理的Ⅴ:zookeeper的相关Java Api的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。