• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于java:Zookeeper的原生api操作

java 搞代码 4年前 (2022-01-28) 38次浏览 已收录 0个评论
文章目录[隐藏]

pom文件引入

<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.14</version>
    </dependency>
</dependencies>

zookeeper原生api创立会话操作

public class CreateSession implements Watcher {
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new CreateSession());
        System.out.println(zooKeeper.getState());
        countDownLatch.await();
        System.out.println("zk session  create success");

    }
    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
        }
    }
}

失常创立回话

zookeeper原生api获取信息操作

public class getNoetNode implements Watcher {

    private  static ZooKeeper zooKeeper;

    public static void main(String[] args) throws InterruptedException, IOException {
        zooKeeper = new ZooKeeper("119.45.52.68:2181",10000,new getNoetNode());
        //countDownLatch.await();
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Override
    public void process(WatchedEvent event) {
        if(event.getType() ==Event.Ev<p style="color:transparent">来源gao!%daima.com搞$代*!码网</p>entType.NodeChildrenChanged){
//再次获取节点数据
            try {
                List<String> children =
                        zooKeeper.getChildren(event.getPath(), true);
                System.out.println(children);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //当连贯创立了,服务端发送给客户端SyncConnected事件
        if(event.getState() == Event.KeeperState.SyncConnected){
            try {
//调⽤获取单个节点数据⽅法
                getNodeData();
                getChildren();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println();
    }

    private static void getNodeData() throws Exception {
        byte[] data = zooKeeper.getData("/persistent/children", true,
                null);
        System.out.println(new String(data,"utf-8"));
    }

    public static  void getChildren() throws Exception{
        List<String> childrens = zooKeeper.getChildren("persistent",true);
        System.out.println(childrens);

    }
}

欢送搜寻关注自己与敌人共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于java:Zookeeper的原生api操作
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址