前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >zookeeper API操作,基本命令及报错

zookeeper API操作,基本命令及报错

作者头像
chenchenchen
发布2019-09-02 17:36:42
1.3K1
发布2019-09-02 17:36:42
举报
文章被收录于专栏:chenchenchenchenchenchen

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_38004638/article/details/96590483

API

ZooKeeper API 中,有两个包是我们经常打交道的,分别是 org.apache.zookeeper, org.apache.zookeeper.data 。前一个包提供了一些API操作zk,例如对节点node增删改查,后一个包定义了一些实体类,例如对zk 节点进行权限控制的ACL类、Id类等。zk常用的API如下。

创建会话

ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly)

connectString: zk的url sessionTimeout: session的超时时间 watcher: zookeeper实例的状态监视器,由于zk的创建过程是异步的,这里可以通过注册一个监视器,监听zk的状态,当状态变为 SyncConnected 后,即表示已经连接成功。

创建节点Node

String create(final String path, byte data[], List<ACL> acl, CreateMode createMode) //同步方式 create(final String path, byte data[], List<ACL> acl, CreateMode createMode, StringCallback cb, Object ctx) //异步方式创建节点

删除节点

delete(final String path, int version) //同步方式 delete(final String path, int version, VoidCallback cb, Object ctx) //异步方式 path:路径 version:期望版本号,相当于数据库的乐观锁,在node里面存放着状态信息stat,里面存放有版本号version,如果设置的期待版本号与节点里的不相同,会操作节点失败。当版本号设置为-1时,忽略节点的版本号。

获取节点:int version =zooKeeper.exists(path, null).getVersion();

获取子节点

public List<String> getChildren(String path, boolean watch) //同步 List<String> getChildren(final String path, Watcher watcher) //同步 public void getChildren(String path, boolean watch, Children2Callback cb,Object ctx) //异步

watcher:观察节点的变化,包括节点的删除,子节点的创建、删除。需要注意的是,watcher是个一次性的东西,当它被回调的时候,将会从节点中删除,所以如果要一致监控该节点,需要反复注册watcher。 getChildren返回的子节点路径是路径名,不是全路径,例如节点路径是/node,子节点的路径是/node/child,通过节点 getChildren 获取的路径是 child。

更新节点数据

public Stat setData(final String path, byte data[], int version) //同步 public void setData(final String path, byte data[], int version, StatCallback cb, Object ctx) //异步 version:与文章 删除节点 里的version一致,获取节点:int version =zooKeeper.exists(path, null).getVersion(); cb:异步回调接口

获取节点数据

public byte[] getData(final String path, Watcher watcher, Stat stat) //同步 public void getData(String path, boolean watch, DataCallback cb, Object ctx) // 异步

watcher:当节点的数据发生变化时回调,也是一次性的 stat:如果需要获取节点的状态信息,传一个对象

判断节点是否存在

public Stat exists(final String path, Watcher watcher) public void exists(final String path, Watcher watcher, StatCallback cb, Object ctx) watcher:可以监听子节点的创建、删除,和节点的数据更新

Zookeeper基础命令操作

https://my.oschina.net/merryyou/blog/1823428

ZooKeeper四字命令

https://www.cnblogs.com/kuku0223/p/8428341.html

conf is not executed because it is not in the whitelist 使用四字命令报错

进入zookeeper的zoo.cfg,修改

#开启四字命令 4lw.commands.whitelist=*

报错

连接报错

KeeperErrorCode = ConnectionLoss for /节点路径 连接失败,一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的

增加连接时长 ZooKeeper zk = new ZooKeeper("10.0.0.11:2181", 15000, null);

增加maxWaitTime,maxSleepTime

关闭防火墙

KeeperErrorCode = Unimplemented for /节点路径 使用Curator时报错,原因是因为版本问题

Curator官网的声明:http://curator.apache.org/ The are currently two released versions of Curator, 2.x.x and 3.x.x:

Curator 2.x.x - compatible with both ZooKeeper 3.4.x and ZooKeeper 3.5.x Curator 3.x.x - compatible only with ZooKeeper 3.5.x and includes support for new features such as dynamic reconfiguration, etc.

ZooKeeper 3.5.x

使用Curator4.0.0

Zookeeper3.4.X

使用Curator2.X,如2.12.0

Curator4.0在软兼容模式下支持Zookeeper3.4.X,但是需要依赖排除zookeeper。同时必须加入Zookeeper3.4.X的依赖,并且呢,因为是软兼容模式,一些3.4.X不具备的新特性是不能使用的。(不建议)

<dependency>

<groupId>org.apache.curator</groupId>

<artifactId>curator-recipes</artifactId>

<version>${curator-version}</version>

<exclusions>

<exclusion>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

</exclusion>

</exclusions>

</dependency>

Connection refused: no further information

查询节点报错

Authentication is not valid KeeperErrorCode = NoAuth for

创建节点报错

KeeperErrorCode = NodeExists for /节点路径 节点路径已存在

KeeperErrorCode = NoNode for /节点路径 节点路径不存在

在每次新建一个节点时,一定要判断该节点(路径)是否存在,因为在ZooKeeper中路径使唯一的,所以当在该路径下已有节点时,继续往当前路径上新建节点就会报这个错

更新报错

KeeperErrorCode = BadVersion for /节点路径 更新操作中版本号错误

更新操作前,先获取当前需要更新的版本号

Stat stat = zooKeeper.exists(path, null);

int version = stat.getVersion();

删除节点报错

Node not empty

The command 'rmr' has been deprecated. Please use 'deleteall' instead.

Authentication is not valid : / 没有权限操作

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • API
    • 创建会话
      • 创建节点Node
        • 删除节点
          • 获取子节点
            • 更新节点数据
              • 获取节点数据
                • 判断节点是否存在
                • Zookeeper基础命令操作
                  • ZooKeeper四字命令
                  • 报错
                    • 连接报错
                      • 查询节点报错
                        • 创建节点报错
                          • 更新报错
                            • 删除节点报错
                            领券
                            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档