1. Api

坐标

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-core</artifactId>
  10. <version>2.8.2</version>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
  13. <dependency>
  14. <groupId>org.apache.zookeeper</groupId>
  15. <artifactId>zookeeper</artifactId>
  16. <version>3.5.7</version>
  17. </dependency>
  18. </dependencies>

log4j.properties

  1. log4j.rootLogger=INFO, stdout
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
  5. log4j.appender.logfile=org.apache.log4j.FileAppender
  6. log4j.appender.logfile.File=target/spring.log
  7. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

test

package com.atguigu.zkclient;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ZkClient {
    private ZooKeeper zooKeeper = null;

    @Before
    public void before() throws IOException {
        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        int sessionTimeout = 2000;
        //创建zk对象
        zooKeeper = new ZooKeeper(connectString,  //连接地址
                sessionTimeout,  //超时时间
                new Watcher() {
                    //Zookeeper监听的回调函数
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("回调函数");
                    }
                }


        );
    }

    @After
    public void after() throws InterruptedException {
        zooKeeper.close();
    }

    //创建新节点
    @Test
    public void create() throws IOException, InterruptedException, KeeperException {

        zooKeeper.create(
                "/testApi", //节点名
                "haha".getBytes(), //值
                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //访问控制列表 相对应权限
                CreateMode.PERSISTENT  //永久节点
        );
    }

    //查询子节点
    @Test
    public void ls() throws InterruptedException, KeeperException {
        List<String> children = zooKeeper.getChildren(
                "/",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("自定义回调函数");
                    }
                }
        );
        for (String child : children) {
            System.out.println(child);
        }

        Thread.sleep(Long.MAX_VALUE);
    }

    //查询节点的值
    @Test
    public void get() throws InterruptedException, KeeperException, IOException {
        Stat stat = new Stat();  //节点状态(Stat结构体)
        byte[] data = zooKeeper.getData("/testApi", false, stat);
        System.out.write(data);
        System.out.println();
        System.out.println(stat.getMzxid());
    }

    //查询一个节点的状态
    @Test
    public void stat() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.exists("/testApi", false);
        if (stat == null) {
            System.out.println("节点不存在");
        } else {
            System.out.println(stat);
        }
    }

    //修改节点内容
    @Test
    public void set() throws InterruptedException, KeeperException {
        String node = "/testApi";
        int version = 0;
        Stat stat = zooKeeper.exists(node, false);

        if (stat == null) {
            System.out.println("节点不存在");
        } else {
            version = stat.getVersion();
        }
        //乐观锁 修改前先比较版本号 不一致则报错
        zooKeeper.setData(node,
                "123".getBytes(StandardCharsets.UTF_8),
                version
        );
    }

    //删除节点
    @Test
    public void delete() throws InterruptedException, KeeperException {
        zooKeeper.delete("/testApi",1);
    }



}