前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >zookeeper原生api操作

zookeeper原生api操作

作者头像
zhaozhen
发布2021-07-15 10:51:34
3140
发布2021-07-15 10:51:34
举报
文章被收录于专栏:微瞰Java后端开发

pom文件引入

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.14</version>
    </dependency>
</dependencies>

几个状态和概念 节点创建的权限信息ACL 参数的类型

ANYONE_ID_UNSAFE : 表示任何⼈

AUTH_IDS :此ID仅可⽤于设置ACL。它将被客户机验证的ID替换。

OPEN_ACL_UNSAFE :这是⼀个完全开放的ACL(常⽤)-->world:anyone

CREATOR_ALL_ACL :此ACL授予创建者身份验证ID的所有权限 节点的几种类型的枚举

PERSISTENT:持久节点

PERSISTENT_SEQUENTIAL:持久顺序节点

EPHEMERAL:临时节点

EPHEMERAL_SEQUENTIAL:临时顺序节点

代码语言:javascript
复制
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获取信息操作

代码语言:javascript
复制
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.EventType.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);

    }
}

zookeeper原生api更新节点信息

代码语言:javascript
复制
public class UpdateNode implements Watcher {

    private static ZooKeeper zooKeeper;

    public static void main(String[] args) throws Exception{
        zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());
        Thread.sleep(Integer.MAX_VALUE);

    }
    @Override
    public void process(WatchedEvent event) {
        try {
            byte[] before = zooKeeper.getData("/persistent/children",false,null);
            System.out.println("修改前的值"+new String(before));
           Stat stat = zooKeeper.setData("/persistent","客户端修改的内容".getBytes(),-1);
            System.out.println(stat);
            byte[] after = zooKeeper.getData("/persistent/children",false,null);
            System.out.println("修改后的值"+new String(after));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

zookeeper原生api删除节点信息

代码语言:javascript
复制
public class DeleteNode implements Watcher {
    private static  ZooKeeper zooKeeper;
    public static void main(String[] args) throws  Exception{
        zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());
        Thread.sleep(Integer.MAX_VALUE);
    }
    @Override
    public void process(WatchedEvent event) {
        try {
            Stat exists = zooKeeper.exists("/persistent/children", false);
            System.out.println(exists == null ? "该节点不存在":"该节点存在");
            zooKeeper.delete("/persistent/children",-1);
            Stat exists2 = zooKeeper.exists("/persistent/children", false);
            System.out.println(exists2 == null ? "该节点不存在":"该节点存在");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

代码地址为https://github.com/zhendiao/deme-code/tree/main/zk

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微瞰技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • zookeeper原生api获取信息操作
  • zookeeper原生api更新节点信息
  • zookeeper原生api删除节点信息
相关产品与服务
数字身份管控平台
数字身份管控平台(Identity and Access Management)为您提供集中式的数字身份管控服务。在企业 IT 应用开发时,数字身份管控平台可为您集中管理用户账号、分配访问权限以及配置身份认证规则,避免因员工账号、授权分配不当导致的安全事故。在互联网应用开发时,数字身份管控平台可为您打通应用的身份数据,更好地实现用户画像,也可为用户提供便捷的身份认证体验,提升用户留存。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档