版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38004638/article/details/96590483
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 后,即表示已经连接成功。
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:可以监听子节点的创建、删除,和节点的数据更新
https://my.oschina.net/merryyou/blog/1823428
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 : / 没有权限操作
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有