之前我们讲解了Zookeeper安装与Zookeeper常用命令讲解
linux安装Zookeeper3.5.7详解_兜兜转转m的博客-CSDN博客
Zookeeper系列文章—入门_兜兜转转m的博客-CSDN博客
由于我们需要利用Java进行开发,因此我们使用Curator对Zk进行操控

导入依赖,由于使用的是3.5.7版本,你的curator要使用4.0以上的新版本。
-
-
org.apache.curator -
curator-framework -
4.0.0 -
-
-
-
org.apache.curator -
curator-recipes -
4.0.0 -
- /**
- * @author: msf
- * @date: 2022/11/22
- */
-
- public class ZkTest {
-
- // 加载连接
- private
- CuratorFramework client;
-
- /**
- * 第一个参数表示连接你的zk地址
- * 第二个参数表示会话连接时长
- * 第三参数表示你连接zk的时长
- * 第四个参数表示你的重试机制
- * 第五个参数是名称空间,将下面创建的节点都放在该节点下。
- */
- @Before
- public void connect() {
- RetryPolicy retryPolicy =
- new ExponentialBackoffRetry(3000,10);
- client = CuratorFrameworkFactory.builder()
- .connectString("192.168.3.4:2181")
- .sessionTimeoutMs(60 * 1000)
- .connectionTimeoutMs(15 * 1000)
- .retryPolicy(retryPolicy)
- .namespace("test")
- .build();
-
- client.start();
- }
-
-
- @After
- public void close() {
- if (client != null) {
- client.close();
- }
- }
- }

- // **********************测试节点创建********************
- @Test
- public void create() throws Exception {
- // 创建单个节点
- String path = client.create().forPath("/app1", "hello".getBytes(StandardCharsets.UTF_8));
- System.out.println("path = " + path);
- }
-
- @Test
- public void create2() throws Exception {
- // 测试建立的数据类型,持久化,持久化顺序的,临时的,临时顺序的
- // 本次建立是临时的--当客户端断开连接节点消失
- String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app2");
- System.out.println("path = " + path);
-
- Thread.sleep(3*1000);
-
- }
- @Test
- public void create3() throws Exception {
- // 创建多级节点
- String path = client.create().creatingParentsIfNeeded().forPath("/app2/p1/t1");
- System.out.println("path = " + path);
- }
- //********************测试获取数据*************************
- @Test
- public void get() throws Exception {
- // 获取节点的数据信息
- byte[] bytes = client.getData().forPath("/app1");
- System.out.println("data = " + new String(bytes));
- }
-
- @Test
- public void get2() throws Exception {
- // 获取多级节点
- List
strings = client.getChildren().forPath("/"); - System.out.println("strings = " + strings);
-
- }
-
- @Test
- public void get3() throws Exception {
- // 获取某一节点的详细新
- Stat stat = new Stat();
- System.out.println("stat = " + stat);
- byte[] bytes = client.getData().storingStatIn(stat).forPath("/app1");
- System.out.println("bytes = " + new String(bytes));
- System.out.println("stat = " + stat);
- }
- // ***************修改数据****************
- @Test
- public void updata() throws Exception {
- client.setData().forPath("/app1","Hello Zk".getBytes(StandardCharsets.UTF_8));
- }
-
- @Test
- public void updata2() throws Exception {
- // 根据详细信息中的版本进行修改数据
- Stat stat = new Stat();
- client.getData().storingStatIn(stat).forPath("/app1");
- client.setData().withVersion(stat.getVersion()).forPath("/app1","Hello Zk".getBytes(StandardCharsets.UTF_8));
-
- }
- // ***************删除节点*****************
- @Test
- public void delete() throws Exception {
- // 单个节点
- client.delete().forPath("/app2/p1/t1");
- }
- @Test
- public void delete2() throws Exception {
- // 删除多个节点
- client.delete().deletingChildrenIfNeeded().forPath("/app2");
- }
- @Test
- public void delete3() throws Exception {
- // 删除节点
- client.delete().inBackground(new BackgroundCallback() {
- @Override
- public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
- System.out.println("app1已经删除");
- System.out.println("event = " + event);
- }
- }).forPath("/app1");
- }