ZooKeeper zooKeeper;
CountDownLatch countDownLatch=new CountDownLatch(1);
public void createConn() throws IOException, InterruptedException {
//1.connectString: 服务器ip、port
//2.sessionTimeout: 客户端与服务端的会话超时时间,单位:ms
//3.watcher: 对象
zooKeeper = new ZooKeeper("127.0.0.1:2181", 10000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
//创建连接成功
if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
System.out.println("zookeeper连接创建成功!");
//countDownLatch.countDown();
}
}
});
//countDownLatch.await();
}
public void test1() throws KeeperException, InterruptedException, IOException {
//1.path: 节点路径
//2.data: 节点数据
//3.acl: acl权限列表:OPEN_ACL_UNSAFE、CREATOR_ALL_ACL、READ_ACL_UNSAFE
//4.createMode: 节点类型:PERSISTENT、PERSISTENT_SEQUENTIAL、EPHEMERAL、EPHEMERAL_SEQUENTIAL
zooKeeper.create("/test/node1","abc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public void test1() throws KeeperException, InterruptedException, IOException {
zooKeeper.create("/test/node1", "abc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
@Override
public void processResult(int i, String path, Object ctx, String name) {
//返回状态:0--成功
System.out.println(i);
//节点路径
System.out.println(path);
//上下文参数
System.out.println(ctx);
//节点路径
System.out.println(name);
}
},"Object ctx");
}
public void test2() throws KeeperException, InterruptedException {
//1.ZooDefs.Ids.READ_ACL_UNSAFE world:anyone:r
zooKeeper.create("/test/node2","abc".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public void test3() throws KeeperException, InterruptedException {
//world授权模式
//1.权限列表
List<ACL> aclList=new ArrayList<>();
//2.权限模式(scheme)、授权对象(id)
Id id=new Id("world","anyone");
//3.权限设置:
// int READ = 1;
// int WRITE = 2;
// int CREATE = 4;
// int DELETE = 8;
// int ADMIN = 16;
// int ALL = 31;
aclList.add(new ACL(ZooDefs.Perms.READ,id));
aclList.add(new ACL(ZooDefs.Perms.WRITE,id));
aclList.add(new ACL(ZooDefs.Perms.CREATE,id));
aclList.add(new ACL(ZooDefs.Perms.DELETE,id));
aclList.add(new ACL(ZooDefs.Perms.ADMIN,id));
zooKeeper.create("/test/node3","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
public void test4() throws KeeperException, InterruptedException {
//ip授权模式
//1.权限列表
List<ACL> aclList=new ArrayList<>();
//2.权限模式(scheme)、授权对象(id)
Id id=new Id("ip","192.168.31.220");
//3.权限设置:
// int ALL = 31;
aclList.add(new ACL(ZooDefs.Perms.ALL,id));
zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
public void test5() throws KeeperException, InterruptedException {
//auth授权模式
//1.添加授权用户
zooKeeper.addAuthInfo("digest","wzb:123456".getBytes());
zooKeeper.create("/test/node5","abc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
}
public void test6() throws KeeperException, InterruptedException {
//auth授权模式
//1.添加授权用户
zooKeeper.addAuthInfo("digest","wzb:123456".getBytes());
//2.权限列表
List<ACL> aclList=new ArrayList<>();
//3.权限模式(scheme)、授权对象(id)
Id id=new Id("auth","wzb");
//4.权限设置:
// int ALL = 31;
aclList.add(new ACL(ZooDefs.Perms.ALL,id));
zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
public void test7() throws KeeperException, InterruptedException {
//digest授权模式
//1.权限列表
List<ACL> aclList=new ArrayList<>();
//2.权限模式(scheme)、授权对象(id)
Id id=new Id("digest","wzb:/jkGdS5slDCObV2mWEt7Yf2Xb98=");
//3.权限设置:
// int ALL = 31;
aclList.add(new ACL(ZooDefs.Perms.ALL,id));
zooKeeper.create("/test/node4","abc".getBytes(), aclList, CreateMode.PERSISTENT);
}
public void test8() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.data: 节点数据
//3.version: 版本号 -1:不比较版本号
//4.StatCallback: 回调接口
//5.ctx: 上下文参数
zooKeeper.setData("/test/node1", "abc".getBytes(), -1);
}
public void test8() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.data: 节点数据
//3.version: 版本号 -1:不比较版本号
//4.StatCallback: 回调接口
//5.ctx: 上下文参数
zooKeeper.setData("/test/node1", "abc".getBytes(), -1, new AsyncCallback.StatCallback() {
@Override
public void processResult(int i, String path, Object ctx, Stat stat) {
//stat:节点属性对象
System.out.println(stat.toString());
}
},"Object ctx");
}
public void test9() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.version: 版本号 -1:不比较版本号
zooKeeper.delete("/test/node1", -1);
}
public void test9() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.version: 版本号 -1:不比较版本号
//3.VoidCallback: 回调接口
//4.ctx: 上下文参数
zooKeeper.delete("/test/node1", -1, new AsyncCallback.VoidCallback() {
@Override
public void processResult(int i, String path, Object ctx) {
}
},"Object ctx");
}
public void test10() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//3.stat: 节点属性对象
Stat stat=new Stat();
byte[] data = zooKeeper.getData("/test/node1", false, stat);
}
public void test10() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//3.DataCallback: 回调接口
//4.ctx: 上下文参数
zooKeeper.getData("/test/node1", false, new AsyncCallback.DataCallback() {
@Override
public void processResult(int i, String path, Object ctx, byte[] data, Stat stat) {
//data:数据
System.out.println(new String(data));
}
},"Object ctx");
}
public void test11() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//注意:返回的是节点名称而非路径
List<String> children = zooKeeper.getChildren("/test", false);
}
public void test11() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//3.ChildrenCallback: 回调接口
//4.ctx: 上下文参数
zooKeeper.getChildren("/test", false, new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int i, String path, Object ctx, List<String> children) {
}
}, "Object ctx");
}
public void test12() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//注意:stat为null代表节点不存在
Stat stat = zooKeeper.exists("/test", false);
}
public void test12() throws KeeperException, InterruptedException {
//1.path: 节点路径
//2.watch: 是否使用创建连接中注册的
//3.StatCallback: 回调接口
//4.ctx: 上下文参数
zooKeeper.exists("/test/node1", false, new AsyncCallback.StatCallback() {
@Override
public void processResult(int i, String path, Object ctx, Stat stat) {
}
}, "Object ctx");
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务