以前写过一个原生zookeeper Api对zookeeper的操作使用。如果有想学习原生api的可以看这里:
https://blog.csdn.net/RenJianLeLe/article/details/108034937
直接上demo,如果项目用使用到了直接拿来稍微改动就能用。
环境是版本说明:
zookeeper 版本 3.4.14
pom依赖(注意:我们使用zookeeper的Java客户端工具最好跟zookeeper版本保持一致)
<properties>
<zookeeper.version>3.4.14zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>2.6.0version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.8.6version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
dependencies>
public class CuratorConnectionTest {
private static final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
public static void main(String[] args) {
CuratorFramework client = CuratorFrameworkFactory.builder()
//连接地址
.connectString(HOST)
//连接超时时间
.connectionTimeoutMs(3000)
//会话超时时间
.sessionTimeoutMs(5000)
//重试机制 重试一次
//RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
//命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
.namespace("create")
//构建连接对象
.build();
//打开连接
client.start();
System.out.println(client.getState());
client.close();
}
}
public class CuratorCreate {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(150000)
// 会话超时时间
// .sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("create")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 创建一个持久节点
*/
@Test
public void create1() {
try {
String path = client.create()
.withMode(CreateMode.PERSISTENT)
.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
.forPath("/node1","node1".getBytes());
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建 一个ip权限节点
*/
@Test
public void create2() {
try {
List<ACL> list = new ArrayList<ACL>();
Id id = new Id("ip","192.168.56.101");
list.add(new ACL(ZooDefs.Perms.ALL,id));
list.add(new ACL(ZooDefs.Perms.ALL,new Id("ip","192.168.199.242")));
String path = client.create()
.withMode(CreateMode.PERSISTENT)
.withACL(list)
.forPath("/node3","node3".getBytes());
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 递归创建节点
*/
@Test
public void create3() {
String path;
try {
path = client.create()
/**
* 这个方法就是去支持递归创建节点
*/
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
.forPath("/node3/node1","node1".getBytes());
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步创建节点
*/
@Test
public void test4() {
CountDownLatch count = new CountDownLatch(1);
try {
client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getPath());
System.out.println(event.getType());
if(event.getType() == CuratorEventType.CREATE) {
count.countDown();
}
}
})
.forPath("/node4","node4".getBytes());
count.await();
System.out.println("结束");
} catch (Exception e) {
count.countDown();
e.printStackTrace();
}
}
}
public class CuratorDelete {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("delete")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 删除节点
*/
@Test
public void delete1() {
try {
client.delete().forPath("/node3");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 指定版本号去删除
*/
@Test
public void delete2() {
try {
client.delete()
.withVersion(1)
.forPath("/node3");
} catch (Exception e) {
//BadVersion for /node3
e.printStackTrace();
}
}
/**
* 递归删除,节点包含子节点
*/
@Test
public void delete3() {
try {
client.delete()
//这个方法可以递归删除
.deletingChildrenIfNeeded()
.withVersion(-1)
.forPath("/node3");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步删除
*/
@Test
public void delete4() {
CountDownLatch count = new CountDownLatch(1);
try {
client.delete()
//这个方法可以递归删除
.deletingChildrenIfNeeded()
.withVersion(-1)
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getContext()); //context
System.out.println(event.getPath()); ///node3
System.out.println(event.getName()); //null
System.out.println(event.getResultCode()); //0
System.out.println(event.getStat()); //null
if(event.getType()==CuratorEventType.SET_DATA) {
count.countDown();
}
}
}, "context")
.forPath("/node3");
count.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorExists {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("get")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 判断节点是否存在,
* 如果不存在返回 null
* 如果存在 返回节点的属性
*/
@Test
public void exists1() {
try {
Stat stat = client.checkExists()
.forPath("/get");
System.out.println(gson.toJson(stat));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步判断节点是否存在
*/
@Test
public void exists2() {
CountDownLatch countDown = new CountDownLatch(1);
try {
client.checkExists()
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getType());
System.out.println(gson.toJson(event.getStat()));
if(event.getType() == CuratorEventType.EXISTS) {
countDown.countDown();
}
}
})
.forPath("/get");
countDown.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorGet {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
.namespace("get")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 获取节点数据
*/
@Test
public void get1() {
try {
byte[] data = client.getData().forPath("node1");
System.out.println(new String(data,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取节点权限
*/
@Test
public void get2() {
try {
List<ACL> data = client.getACL().forPath("node1");
System.out.println(gson.toJson(data));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取节点属性
*/
@Test
public void get3() {
try {
Stat stat = new Stat();
byte[] data = client.getData().storingStatIn(stat).forPath("node1");
System.out.println(gson.toJson(stat));
System.out.println(new String(data,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步方式读取
*/
@Test
public void get4() {
CountDownLatch countDown = new CountDownLatch(1);
try {
client.getData()
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
/**
* 结果分别如下
* GET_DATA
context
null
/node1
{"czxid":78,"mzxid":78,"ctime":1598276691205,"mtime":1598276691205,"version":0,"cversion":0,"aversion":0,"ephemeralOwner":0,"dataLength":5,"numChildren":0,"pzxid":78}
node1
*/
System.out.println(event.getType());
System.out.println(event.getContext());
System.out.println(event.getName());
System.out.println(event.getPath());
System.out.println(gson.toJson(event.getStat()));
System.out.println(new String(event.getData(),"utf-8"));
if(event.getType() == CuratorEventType.GET_DATA) {
countDown.countDown();
}
}
}, "context")
.forPath("node1");
countDown.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorGetChild {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("get")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 获取子节点数据
*/
@Test
public void getChild1() {
try {
List<String> list = client.getChildren()
.forPath("/get");
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步获取子节点数据
*/
@Test
public void getChild2() {
CountDownLatch countDown = new CountDownLatch(1);
try {
client.getChildren()
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getPath());
System.out.println(event.getType());
System.out.println(event.getChildren());
if(event.getType() == CuratorEventType.CHILDREN) {
countDown.countDown();
}
}
})
.forPath("/get");
countDown.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorSet {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("create")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 更新节点数据
*/
@Test
public void set1() {
try {
Stat node = client.setData()
.forPath("/node2","node".getBytes());
System.out.println(gson.toJson(node));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 指定版本更新
* 如果版本号是-1 表示不校验版本号
*/
@Test
public void set2() {
try {
Stat node = client.setData()
//如果版本号是-1 表示不校验版本号
.withVersion(2)
.forPath("/node2","node222".getBytes());
System.out.println(gson.toJson(node));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 异步方式去修改
*/
@Test
public void set3() {
CountDownLatch count = new CountDownLatch(1);
try {
Stat node = client.setData()
//如果版本号是-1 表示不校验版本号
.withVersion(-1)
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println(event.getPath());
System.out.println(event.getType());
System.out.println(gson.toJson(event.getStat()));
//上下文对象
System.out.println(event.getContext());
if(event.getType()==CuratorEventType.SET_DATA) {
count.countDown();
}
}
},"context")
.forPath("/node2","node".getBytes());
count.await();
System.out.println(gson.toJson(node));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改权限
*/
@Test
public void set4() {
try {
List<ACL> list = new ArrayList<ACL>();
// Id id = new Id("ip","192.168.56.101");
// list.add(new ACL(ZooDefs.Perms.ALL,id));
list.add(new ACL(ZooDefs.Perms.ALL,new Id("world","anyone")));
list.add(new ACL(ZooDefs.Perms.ALL,new Id("ip","192.168.199.242")));
Stat node = client.setACL().withACL(list)
.forPath("/node3");
System.out.println(gson.toJson(node));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorLock {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
.namespace("tra")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 排他锁
*/
@Test
public void lock1() {
try {
InterProcessLock interProcessLock = new InterProcessMutex(client, "/lock1");
interProcessLock.acquire();
System.out.println("获取锁对象");
ThreadLocalRandom random = ThreadLocalRandom.current();
for(int i=0;i<5;i++) {
Thread.sleep(random.nextInt(2000));
System.out.println(i);
}
interProcessLock.release();
System.out.println("释放锁对象");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读锁
* 1、两个读锁 可以共享资源,不会锁住
* 2、当加上了读锁 写操作锁来了,读操作没有完成之前, 写操作会锁住
* 3、当加上了写锁 读操作来了,写操作没有完成之前,不能进行读
*/
@Test
public void lock2() {
try {
InterProcessReadWriteLock interProcessReadWriteLock = new InterProcessReadWriteLock(client, "/lock1");
InterProcessLock interProcessLock = interProcessReadWriteLock.readLock();
System.out.println("获取锁对象");
ThreadLocalRandom random = ThreadLocalRandom.current();
for(int i=0;i<5;i++) {
Thread.sleep(random.nextInt(2000));
System.out.println(i);
}
interProcessLock.release();
System.out.println("释放锁对象");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写锁
* 两个写操作 是排他的,第一个写没有完成之前 第二个写操作不能操作
*
*/
@Test
public void lock3() {
try {
InterProcessReadWriteLock interProcessReadWriteLock = new InterProcessReadWriteLock(client, "/lock1");
InterProcessLock interProcessLock = interProcessReadWriteLock.writeLock();
System.out.println("获取锁对象");
ThreadLocalRandom random = ThreadLocalRandom.current();
for(int i=0;i<5;i++) {
Thread.sleep(random.nextInt(2000));
System.out.println(i);
}
interProcessLock.release();
System.out.println("释放锁对象");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorTransaction {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
.namespace("tra")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* 开启事务 批量执行命令
* 如果 其中一个命令执行失败,所有命令都不会成功
*/
@Test
public void transaction1() {
try {
client.inTransaction()//开启事务
.create().forPath("node1","node1".getBytes())
.and()
.setData().forPath("node2","node2".getBytes())
.and()
.commit();//提交事务
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class CuratorWatcher {
// private final String HOST = "192.168.56.101:2181,192.168.56.101:2182,192.168.56.101:2183,192.168.56.101:2184";
private final String HOST = "192.168.199.242:2181";
CuratorFramework client = null;
Gson gson = new Gson();
@Before
public void before() {
client = CuratorFrameworkFactory.builder()
// .authorization("digest", "root:123456".getBytes()) //创建一个授权用户
// 连接地址
.connectString(HOST)
// // 连接超时时间
.connectionTimeoutMs(3000)
// 会话超时时间
.sessionTimeoutMs(5000)
// 重试机制 重试一次
// RetryNTimes -- 可以指定重试几次
.retryPolicy(new RetryOneTime(5000))
// 命名空间 -- 如果指定这个数据,所有存储节点的前缀都是它
// .namespace("get")
// 构建连接对象
.build();
client.start();
}
@After
public void after() {
client.close();
}
/**
* curator 的watcher 用的 NodeCache对象
* 使用它 需要 start 用完了 然后close
*
* 可以捕获 (创建、修改事件) 如果监控前该节点已经存在会取出值
* 删除有异常 捕获异常后,再创建还能监听到事件
*/
@Test
public void watcher1() {
try {
NodeCache nodeCache = new NodeCache(client, "/watcher");
nodeCache.start();
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println(nodeCache.getCurrentData().getPath());
System.out.println(new String(nodeCache.getCurrentData().getData()));
}
});
Thread.sleep(100000);
System.out.println("over");
nodeCache.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 监控子节点
*/
@Test
public void watcher2() {
try {
//如果第三个参数 为false 那么 子节点的数据就不能获取到了
PathChildrenCache cnode = new PathChildrenCache(client, "/get", true);
cnode.start();
cnode.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
System.out.println(event.getType());//事件类型
System.out.println(new String(event.getData().getData()));//子节点数据
System.out.println(event.getData().getPath());//节点路径
}
});
Thread.sleep(100000);
System.out.println("over");
cnode.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}