❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️
文章目录
- 前置准备
- 一、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的安装步骤
一、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;
@Before
public 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连接
@After
public void close() {
try {
zookeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
1、API之word授权模式
/**
* world授权模式
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public 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
*/
@Test
public 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
*/
@Test
public 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
*/
@Test
public 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
*/
@Test
public 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;
@Before
public 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();
}
@After
public void close() {
try {
zookeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
1、同步更新
/**
* -1 代表不做修改,不做修改不是版本号不变(只是不需要对应删除节点的版本号了;因为删除节点需要版本号对应,否者会删除失败)
* 更新节点
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void update() throws KeeperException, InterruptedException {
zookeeper.setData("/update/node1", "node-one".getBytes(), 0);
}
@Test
public 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、异步更新
@Test
public 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代码path
System.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、同步删除
@Test
public void delete() throws KeeperException, InterruptedException {
zookeeper.delete("/delete/node1", -1);
}
2、异步删除
@Test
public 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、同步查看
@Test
public 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、异步查看
@Test
public 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、同步查看
@Test
public void getChild() throws KeeperException, InterruptedException {
List<String> list = zookeeper.getChildren("/get", false);
for (String temp : list) {
System.out.println(temp);
}
}
2、异步查看
@Test
public 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、同步查看
@Test
public void exists() throws KeeperException, InterruptedException {
Stat exists = zookeeper.exists("/get/node1", false);
System.out.println(exists);
}
2、异步查看
@Test
public 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);
}