【弄nèng - Zookeeper】Zookeeper入门教程(九)—— ZKPaths工具类

本文介绍zk客户端curator的使用,本文主要介绍Curator recipes的使用
官方文档传送门
官方文档Curator recipes
参考:http://www.throwable.club/2018/12/16/zookeeper-curator-usage/
https://blog.csdn.net/hosaos/article/details/88233109
参考书籍:《从Paxos到ZooKeeper 分布式一致性原理与实践》

1. ZKPaths

ZKPaths提供了创建节点,查询节点,删除节点等API。是我们更加简单的操作ZK

pom

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

关键方法

 	/**
     * 将名称空间应用于给定的路径
     */
    public static String fixForNamespace(String namespace, String path);

    /**
     * 给定父路径和子节点,创建一个组合的完整路径
     */
    public static String makePath(String parent, String child);

    /**
     * 给定完整路径,返回节点名。即。"/cluster/sub2"会返回"sub2"
     */
    public static String getNodeFromPath(String path);

    /**
     * 给定完整路径,返回节点名及其路径。即。"/cluster/sub2"将返回{"/cluster","sub2"}
     */
   public static PathAndNode getPathAndNode(String path)
   
	/**
     * 得到子节点集合
     */
   public static List<String> getSortedChildren(ZooKeeper zookeeper, String path);
	/**
     * 递归地删除节点的子节点
     */
   public static void deleteChildren(ZooKeeper zookeeper, String path, boolean deleteSelf);

更多方法请参看源码

2. 事例

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.EnsurePath;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * ZKPaths事例
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class QueueTests {

    static String path = "/zkpath_path";

    private CuratorFramework client;

    /**
     * 使用Fluent风格API创建
     */
    @Before
    public void client() {
        // 重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString("localhost:2181")
                .sessionTimeoutMs(5000)  // 会话超时时间,默认60000ms
                .connectionTimeoutMs(10000) // 连接超时时间,默认15000ms
                .retryPolicy(retryPolicy) // 重试策略
                // .namespace("base") // 名称空间,如果这里设置了名称空间,那么所有路径都将预先指定名称空间
                .build();
        client.start();
    }


    @Test
    public void test() throws Exception {
        ZooKeeper zookeeper = client.getZookeeperClient().getZooKeeper();

        // 将名称空间应用于给定的路径
        System.out.println(ZKPaths.fixForNamespace("/zkpath_path", "/sub1"));
        // 给定父路径和子节点,创建一个组合的完整路径
        System.out.println(ZKPaths.makePath(path, "/sub1"));
        // 给定完整路径,返回节点名。即。"/cluster/sub2"会返回"sub2"
        System.out.println(ZKPaths.getNodeFromPath("/cluster/sub2"));
        // 给定完整路径,返回节点名及其路径。即。"/cluster/sub2"将返回{"/cluster","sub2"}
        ZKPaths.PathAndNode pn = ZKPaths.getPathAndNode("/cluster/sub2");
        System.out.println(pn.getPath());
        System.out.println(pn.getNode());

        String dir1 = path + "/child3";
        String dir2 = path + "/child4";
        // 创建节点,会创建所有包含节点
        ZKPaths.mkdirs(zookeeper, dir1);
        ZKPaths.mkdirs(zookeeper, dir2);
        // 得到子节点集合
        System.out.println(ZKPaths.getSortedChildren(zookeeper, path));
        // 递归地删除节点的子节点
        ZKPaths.deleteChildren(client.getZookeeperClient().getZooKeeper(), path, true);
        // 关闭工具类
        // CloseableUtils.closeQuietly(queue);
        // CloseableUtils.closeQuietly(client);
    }
}

控制台
在这里插入图片描述

源码地址

IT-CLOUD-ZOOKEEPER :zookeeper客户端Curator事例


项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
IT-CLOUD-ACTIVITI6 :Activiti教程源码。博文在本CSDN Activiti系列中。
IT-CLOUD-ELASTICSEARCH :elasticsearch教程源码。博文在本CSDN elasticsearch系列中。
IT-CLOUD-KAFKA :spring整合kafka教程源码。博文在本CSDN kafka系列中。
IT-CLOUD-KAFKA-CLIENT :kafka client教程源码。博文在本CSDN kafka系列中。
IT-CLOUD-ZOOKEEPER :zookeeper客户端Curator事例。博文在本CSDN zookeeper系列中。

开源项目,持续更新中,喜欢请 Star~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值