Zookeeper系列文章-Curator

之前我们讲解了Zookeeper安装与Zookeeper常用命令讲解

linux安装Zookeeper3.5.7详解_兜兜转转m的博客-CSDN博客

Zookeeper系列文章—入门_兜兜转转m的博客-CSDN博客 

由于我们需要利用Java进行开发,因此我们使用Curator对Zk进行操控

导入依赖,由于使用的是3.5.7版本,你的curator要使用4.0以上的新版本。

<!--curator-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.0</version>
        </dependency>

连接Zk

/**
 * @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();
       }
    }
}

 创建节点

  •         创建单个持久化节点,使用create
  •         创建临时节点,使用withMode(枚举变量)
  •         创建多级节点,creatingParentsIfNeeded()
// **********************测试节点创建********************
    @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);
    }

 获取节点

  • 获取某一个节点的数据--getData()
  • 获取多级节点---getChildren()
  • 获取某一个节点的详细信息--storingStatIn(stat)
//********************测试获取数据*************************
    @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<String> 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);
    }

修改数据

  • 修改某个节点的数据-- setData
  • 根据版本信息修改数据,相当CAS乐观锁。--withVersion
 // ***************修改数据****************
    @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));

    }

删除节点

  • 删除单个节点 -- delete
  • 删除多个节点 -- deletingChildernIfNeeded
  • 删除节点并回调--inBackground
// ***************删除节点*****************
    @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");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜转转m

一毛钱助力博主实现愿望

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值