Curator连zookeeper3.4.14

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dtg</groupId>
    <artifactId>TestZk</artifactId>
    <version>1.0-SNAPSHOT</version>



    <dependencies>
        <!--zookeeper-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>


</project>

  

TestMain 
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class TestMain {


    //会话超时时间
    private static final int SESSION_TIMEOUT = 30 * 1000;

    //连接超时时间
    private static final int CONNECTION_TIMEOUT = 15 * 1000;

    //ZooKeeper服务地址
    private static final String CONNECT_ADDR = "192.168.83.21:2181,192.168.83.22:2181,192.168.83.23:2181";


    public static void main(String[] args) {

        //1 重试策略:重试间隔时间为1s 重试1次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        //2 通过工厂创建连接
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(CONNECT_ADDR).connectionTimeoutMs(SESSION_TIMEOUT)
                .sessionTimeoutMs(SESSION_TIMEOUT)
                .retryPolicy(retryPolicy)
                //命名空间
                .namespace("super")
                .build();
        //3 开启连接
        client.start();

        System.out.println("clientstate:"+client.getState());
        try {

            //测试检查某个节点是否存在
            Stat stat1 = client.checkExists().forPath("/curator");
            Stat stat2 = client.checkExists().forPath("/curator2");

            System.out.println("'/curator'是否存在: " + (stat1 != null ? true : false));
            System.out.println("'/curator2'是否存在: " + (stat2 != null ? true : false));
            //创建永久节点
            client.create().forPath("/curator", "/curator data".getBytes());

            //创建永久有序节点
            client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/curator_sequential", "/curator_sequential data".getBytes());

            //创建临时节点
            client.create().withMode(CreateMode.EPHEMERAL)
                    .forPath("/curator/ephemeral", "/curator/ephemeral data".getBytes());

            //创建临时有序节点
            client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator/ephemeral_path1", "/curator/ephemeral_path1 data".getBytes());

            client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator/ephemeral_path2", "/curator/ephemeral_path2 data".getBytes());

            //测试检查某个节点是否存在
            Stat stat3 = client.checkExists().forPath("/curator");
            Stat stat4 = client.checkExists().forPath("/curator2");

            System.out.println("'/curator'是否存在: " + (stat3 != null ? true : false));
            System.out.println("'/curator2'是否存在: " + (stat4 != null ? true : false));

            //获取某个节点的所有子节点
            System.out.println(client.getChildren().forPath("/"));

            //获取某个节点数据
            System.out.println(new String(client.getData().forPath("/curator")));

            //设置某个节点数据
            client.setData().forPath("/curator", "/curator modified data".getBytes());

            //创建测试节点
            client.create().orSetData().creatingParentContainersIfNeeded()
                    .forPath("/curator/del_key1", "/curator/del_key1 data".getBytes());

            client.create().orSetData().creatingParentContainersIfNeeded()
                    .forPath("/curator/del_key2", "/curator/del_key2 data".getBytes());

            client.create().forPath("/curator/del_key2/test_key", "test_key data".getBytes());

            //删除该节点
            client.delete().forPath("/curator/del_key1");

            //级联删除子节点
            client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/curator/del_key2");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            System.out.println("clientstate:"+client.getState());
            client.close();
            System.out.println("finally...");
        }
    }

}

  

 

posted @ 2020-09-19 17:28  大糖果tt  阅读(515)  评论(0编辑  收藏  举报