Welcome everyone

Zookeeper Java API

java 汪明鑫 121浏览 0评论

添加maven依赖

<!-- zk version要和服务器zk版本一样 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>

 

ZK配置类

package pers.wmx.springbootfreemarkerdemo.zk;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wangmingxin03
 * Created on 2021-12-03
 */
@Slf4j
@Configuration
public class ZookeeperConfig {
    private static final String connectString = "127.0.0.1:2182";

    private static final int timeout = 4000;

    @Bean(name = "zkClient")
    public ZooKeeper zkClient() {
        ZooKeeper zooKeeper = null;

        try {
            final CountDownLatch countDownLatch = new CountDownLatch(1);

            zooKeeper = new ZooKeeper(connectString, timeout, event -> {
                if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
                    countDownLatch.countDown();
                }
            });

            countDownLatch.await();
            log.info("zk start success ... ");

        } catch (Exception e) {
            log.error("zk start exception ... ", e);
        }

        return zooKeeper;
    }
}

 

package pers.wmx.springbootfreemarkerdemo.zk;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wangmingxin03
 * Created on 2021-12-03
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ZkTest {
    @Autowired
    private ZooKeeper zkClient;

    // 创建节点
    @Test
    public void testInsert() {
        try {
            zkClient.create("/hello/hello1", "zk".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (Exception e) {
            log.error("create node fail ... ", e);
        }
    }

    @Test
    public void testQuery() {
        try {
            Stat stat = new Stat();
            byte[] bytes = zkClient.getData("/hello", false, stat);
            log.info("query result:{}", new String(bytes));
        } catch (Exception e) {
            log.error("query fail ... ", e);
        }
    }

    @Test
    public void testWatcher() {
        try {
            CountDownLatch countDownLatch = new CountDownLatch(1);
            Stat exists = zkClient.exists("/hello", watchedEvent -> {

                // 注册一个监听器
                log.info("watch event | path:{}, type:{}",
                        watchedEvent.getPath(), watchedEvent.getType());
                countDownLatch.countDown();

            });

            log.info("exists :{}", exists != null);
            countDownLatch.await();
        } catch (Exception e) {
            log.error("fail ... ", e);
        }
    }

}

 

跑几个简单的单测,结束罪恶的一天,哈哈~

 

 

转载请注明:汪明鑫的个人博客 » Zookeeper Java API

喜欢 (1)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz