• Zookeeper系列文章-Curator


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

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

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

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

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

    1. org.apache.curator
    2. curator-framework
    3. 4.0.0
    4. org.apache.curator
    5. curator-recipes
    6. 4.0.0

    连接Zk

    1. /**
    2. * @author: msf
    3. * @date: 2022/11/22
    4. */
    5. public class ZkTest {
    6. // 加载连接
    7. private
    8. CuratorFramework client;
    9. /**
    10. * 第一个参数表示连接你的zk地址
    11. * 第二个参数表示会话连接时长
    12. * 第三参数表示你连接zk的时长
    13. * 第四个参数表示你的重试机制
    14. * 第五个参数是名称空间,将下面创建的节点都放在该节点下。
    15. */
    16. @Before
    17. public void connect() {
    18. RetryPolicy retryPolicy =
    19. new ExponentialBackoffRetry(3000,10);
    20. client = CuratorFrameworkFactory.builder()
    21. .connectString("192.168.3.4:2181")
    22. .sessionTimeoutMs(60 * 1000)
    23. .connectionTimeoutMs(15 * 1000)
    24. .retryPolicy(retryPolicy)
    25. .namespace("test")
    26. .build();
    27. client.start();
    28. }
    29. @After
    30. public void close() {
    31. if (client != null) {
    32. client.close();
    33. }
    34. }
    35. }

     创建节点

    •         创建单个持久化节点,使用create
    •         创建临时节点,使用withMode(枚举变量)
    •         创建多级节点,creatingParentsIfNeeded()
    1. // **********************测试节点创建********************
    2. @Test
    3. public void create() throws Exception {
    4. // 创建单个节点
    5. String path = client.create().forPath("/app1", "hello".getBytes(StandardCharsets.UTF_8));
    6. System.out.println("path = " + path);
    7. }
    8. @Test
    9. public void create2() throws Exception {
    10. // 测试建立的数据类型,持久化,持久化顺序的,临时的,临时顺序的
    11. // 本次建立是临时的--当客户端断开连接节点消失
    12. String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app2");
    13. System.out.println("path = " + path);
    14. Thread.sleep(3*1000);
    15. }
    16. @Test
    17. public void create3() throws Exception {
    18. // 创建多级节点
    19. String path = client.create().creatingParentsIfNeeded().forPath("/app2/p1/t1");
    20. System.out.println("path = " + path);
    21. }

     获取节点

    • 获取某一个节点的数据--getData()
    • 获取多级节点---getChildren()
    • 获取某一个节点的详细信息--storingStatIn(stat)
    1. //********************测试获取数据*************************
    2. @Test
    3. public void get() throws Exception {
    4. // 获取节点的数据信息
    5. byte[] bytes = client.getData().forPath("/app1");
    6. System.out.println("data = " + new String(bytes));
    7. }
    8. @Test
    9. public void get2() throws Exception {
    10. // 获取多级节点
    11. List strings = client.getChildren().forPath("/");
    12. System.out.println("strings = " + strings);
    13. }
    14. @Test
    15. public void get3() throws Exception {
    16. // 获取某一节点的详细新
    17. Stat stat = new Stat();
    18. System.out.println("stat = " + stat);
    19. byte[] bytes = client.getData().storingStatIn(stat).forPath("/app1");
    20. System.out.println("bytes = " + new String(bytes));
    21. System.out.println("stat = " + stat);
    22. }

    修改数据

    • 修改某个节点的数据-- setData
    • 根据版本信息修改数据,相当CAS乐观锁。--withVersion
    1. // ***************修改数据****************
    2. @Test
    3. public void updata() throws Exception {
    4. client.setData().forPath("/app1","Hello Zk".getBytes(StandardCharsets.UTF_8));
    5. }
    6. @Test
    7. public void updata2() throws Exception {
    8. // 根据详细信息中的版本进行修改数据
    9. Stat stat = new Stat();
    10. client.getData().storingStatIn(stat).forPath("/app1");
    11. client.setData().withVersion(stat.getVersion()).forPath("/app1","Hello Zk".getBytes(StandardCharsets.UTF_8));
    12. }

    删除节点

    • 删除单个节点 -- delete
    • 删除多个节点 -- deletingChildernIfNeeded
    • 删除节点并回调--inBackground
    1. // ***************删除节点*****************
    2. @Test
    3. public void delete() throws Exception {
    4. // 单个节点
    5. client.delete().forPath("/app2/p1/t1");
    6. }
    7. @Test
    8. public void delete2() throws Exception {
    9. // 删除多个节点
    10. client.delete().deletingChildrenIfNeeded().forPath("/app2");
    11. }
    12. @Test
    13. public void delete3() throws Exception {
    14. // 删除节点
    15. client.delete().inBackground(new BackgroundCallback() {
    16. @Override
    17. public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
    18. System.out.println("app1已经删除");
    19. System.out.println("event = " + event);
    20. }
    21. }).forPath("/app1");
    22. }

  • 相关阅读:
    糖友不能接触葡萄糖?
    vue面试如何准备,这几道面试题助力你拿到理想offer
    WPS 换行后缩进、加粗等自定义样式的清除
    jsp小结-三种语法
    Java版 招投标系统简介 招投标系统源码 java招投标系统 招投标系统功能设计
    ssm+vue的4S店预约保养管理系统(有报告)。Javaee项目,ssm vue前后端分离项目。
    第59篇 QML 之 JS类型转换为 Number 类型
    torch.multiprocesssing
    ssm+mysql实现进销存系统
    Slurm基本使用
  • 原文地址:https://blog.csdn.net/abc123mma/article/details/127980534