java 使用 zookeeper 的 API 操作 zookeeper

依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.3</version>
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>
public class TestService {

    private ZooKeeper zooKeeper;

    @Before
    public void init() throws Exception {
    	// 用来吧异步阻塞
        CountDownLatch countDownLatch = new CountDownLatch(1);
        String connectString = "127.0.0.1:2181";
        int sessionTimeout = 1000;
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, 
		// 连接成功后监听
		new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                //执行完毕在countDown
                countDownLatch.countDown();
            }
        });
        //执行完毕在往下走
        countDownLatch.await();
    }

    @Test
    public void dataIsExists() throws Exception {
        zooKeeper.exists("/test", 
        // 不进行监听
        false, 
        // 异步回调
        new AsyncCallback.StatCallback() {
            @Override
            public void processResult(int i, String s, Object o, Stat stat) {
                System.out.println("异步回调");
            }
        }, "传给回调的参数");
    }

    @Test
    public void createPath() throws Exception {
        zooKeeper.create("/test/aaaa", "数据内容".getBytes(), 
        // 设置权限
        ZooDefs.Ids.OPEN_ACL_UNSAFE, 
        // path 是临时存储或永久存储
        CreateMode.EPHEMERAL);
    }

    @Test
    public void deletePath() throws Exception {
        zooKeeper.delete("/test/aaaa", 0);
    }

    @Test
    public void getPath() throws Exception {
        HashSet<String> strings = new HashSet<>();

        zooKeeper.getChildren("/test", 
        // 监听回调
        new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    List<String> children1 = zooKeeper.getChildren("/test", false);
                    for (String s : children1) {
                        if (!strings.contains(s)) {
                            strings.add(s);
                            System.out.println(s);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 
        // 异步回调
        new AsyncCallback.ChildrenCallback() {
            @Override
            public void processResult(int i, String s, Object o, List<String> list) {
                System.out.println(list);
            }
        }, "自定义参数");

        while (true) ;
    }

    @Test
    public void updateData() throws Exception {
        String path = "/test";
        zooKeeper.setData(path, "数据内容".getBytes(),-1);
    }

    @Test
    public void getData() throws Exception {
        String path = "/test";
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper.getData(path, 
        // 不进行监听
        false, 
        // 异步回调
        new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
                System.out.println(new String(bytes));
                countDownLatch.countDown();
            }
        }, "自定义参数");
        countDownLatch.await();
    }

}
posted @ 2022-03-07 18:03  叕叕666  阅读(57)  评论(0)    收藏  举报