美文网首页
Zookeeper学习笔记(5)—— 使用Zookeeper开源

Zookeeper学习笔记(5)—— 使用Zookeeper开源

作者: 曾泽浩 | 来源:发表于2021-05-05 18:56 被阅读0次

首先创建一个Maven项目,引入pom,

    <dependencies>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.3.0</version>
        </dependency>

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

    </dependencies>
  1. 创建会话
    public static void main(String[] args) throws Exception{
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
        client.start();

        Thread.sleep(Integer.MAX_VALUE);

    }

输出结果:

2021-05-04 11:19:38.354 [main] INFO  o.a.curator.framework.imps.CuratorFrameworkImpl - Starting
2021-05-04 11:19:38.358 [main] INFO  org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@76a3e297
2021-05-04 11:19:38.362 [main] INFO  org.apache.zookeeper.common.X509Util - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-05-04 11:19:38.374 [main] INFO  org.apache.zookeeper.ClientCnxnSocket - jute.maxbuffer value is 4194304 Bytes
2021-05-04 11:19:38.380 [main] INFO  org.apache.zookeeper.ClientCnxn - zookeeper.request.timeout value is 0. feature enabled=
2021-05-04 11:19:38.390 [main-SendThread(127.0.0.1:2181)] INFO  org.apache.zookeeper.ClientCnxn - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2021-05-04 11:19:38.393 [main] INFO  o.a.curator.framework.imps.CuratorFrameworkImpl - Default schema
2021-05-04 11:19:38.407 [main-SendThread(127.0.0.1:2181)] INFO  org.apache.zookeeper.ClientCnxn - Socket connection established, initiating session, client: /127.0.0.1:61954, server: localhost/127.0.0.1:2181
2021-05-04 11:19:38.446 [main-SendThread(127.0.0.1:2181)] INFO  org.apache.zookeeper.ClientCnxn - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x1003dddef680000, negotiated timeout = 40000
2021-05-04 11:19:38.451 [main-EventThread] INFO  o.a.curator.framework.state.ConnectionStateManager - State change: CONNECTED
  1. 创建节点
    private static String path = "/zk-test/create-node";

    private static CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(1000,3))
            .build();

    public static void main(String[] args) throws Exception{
        client.start();
        client.create()
                .creatingParentsIfNeeded()  // 如果父节点不存在,会递归的创建
//                .withMode(CreateMode.EPHEMERAL)  // 节点类型
                .forPath(path, "init".getBytes()); // 内容

        Thread.sleep(30000);

    }
  1. 删除节点
      private static String path = "/zk-test/delete-node";

    private static CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(1000,3))
            .build();

    public static void main(String[] args) throws Exception{
        client.start();
        client.create()
                .creatingParentsIfNeeded()  // 如果父节点不存在,会递归的创建
                .withMode(CreateMode.EPHEMERAL)  // 节点类型
                .forPath(path, "delete".getBytes()); // 内容

        Stat stat = new Stat();

        byte[] bytes = client.getData().storingStatIn(stat).forPath(path);

        System.out.println("============" + stat);

        System.out.println("content = " + new String(bytes));

        client.delete().deletingChildrenIfNeeded() // 递归删除子节点
                .withVersion(stat.getVersion())
                .forPath(path); 

        byte[] bytes2 = client.getData().storingStatIn(stat).forPath(path);

        System.out.println("after delete content = " + new String(bytes2));

        Thread.sleep(30000);
    }
  1. 读取数据
    private static String path = "/zk-test/get-node";

    private static CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(1000,3))
            .build();

    public static void main(String[] args) throws Exception{
        client.start();
        client.create()
                .creatingParentsIfNeeded()  // 如果父节点不存在,会递归的创建
                .withMode(CreateMode.EPHEMERAL)  // 节点类型
                .forPath(path, "get".getBytes()); // 内容

        Stat stat = new Stat();

        byte[] bytes = client.getData()
          .storingStatIn(stat)   // 把信息属性赋值给stat对象
          .forPath(path);

        System.out.println("============" + stat);

        System.out.println("content = " + new String(bytes));

        Thread.sleep(30000);
    }

  1. 更新数据
    private static String path = "/zk-test/set-node";

    private static CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(1000,3))
            .build();

    public static void main(String[] args) throws Exception{
        client.start();
        client.create()
                .creatingParentsIfNeeded()  // 如果父节点不存在,会递归的创建
                .withMode(CreateMode.EPHEMERAL)  // 节点类型
                .forPath(path, "set".getBytes()); // 内容

        Stat stat = new Stat();

        byte[] bytes = client.getData().storingStatIn(stat).forPath(path);

        System.out.println("============" + stat);

        System.out.println("content = " + new String(bytes));



        client.setData().forPath(path, "newContent".getBytes());

        byte[] bytes2 = client.getData().storingStatIn(stat).forPath(path);

        System.out.println("============" + stat);

        System.out.println("new content = " + new String(bytes2));

        Thread.sleep(30000);
    }

相关文章

网友评论

      本文标题:Zookeeper学习笔记(5)—— 使用Zookeeper开源

      本文链接:https://www.haomeiwen.com/subject/brgcdltx.html