1.zookeeper常用shell命令
1.新增节点
create [-s] [-e] path data #其中-s为有序节点, -e为临时节点
#当前会话过期,临时节点消失
2.创建持久化节点并写入数据
create /hadoop "123456"
3.更新,删除节点
set /hadoop "345" 1 #delete /hadoop 1
也可以基于版本号进行修改,类似于乐观锁机制,当你传入的数据版本号(dataVersion)和当前节点的版本号不符合时,zookeeper会拒绝本次修改,修改完数据之后dataVersion 自增
3.创建节点,子节点
create /hadoop "123456"
create /hadoop/node1 "node1"
这个时候不能直接delete /hadoop ,rmr /hadoop是全部删除
4.得到节点的状态信息和数据
get /hadoop #stat /hadoop 得到节点的状态信息,不返回节点数据
5.监听器watch
get /hadoop watch # 当其他的客户端改变hadoop节点的值的
#时候当前客户端可以监听到节点数据已经发生改变
2.权限模式
添加用户
addauth digest itcast:12345
world , ip ,auth ,digest 授权模式
setAcl /test ip:192.168.30.130:crwda #将节点权限设置为Ip:
#192.168.30.130的客户端可以对节点进行增删改查,管理权限
setAcl /test world:anyone:rwda
acl超级管理员
echo -n super:admin | openssl dgst -binary -shal | openssl base64
用户名为super,密码为admin,生成一段密文
3.新增节点,javaApi
private CuratorFramework curator;
public void createNode(String node, CreateMode createMode) throws Exception {
// 最后一个参数表示是否进行压缩
Stat stat = curator.checkExists().forPath(node);
if (stat == null) {
curator.create().creatingParentsIfNeeded().withMode(createMode).forPath(node);
} else {
curator.setData().forPath(node);
}
}
public void createAclNode(String node, CreateMode createMode, List<ACL> acl) throws Exception {
Stat stat = curator.checkExists().forPath(node);
if (stat == null) {
curator.create().creatingParentsIfNeeded().withMode(createMode).withACL(acl, true).forPath(node);
}
curator.setData().forPath(node);
}
public void alterAclNode(String node, int perms, String acount) throws Exception {
//权限列表
List<ACL> acl = new ArrayList<ACL>();
//perms为crwda等操作所对应的数字
//ID对象,授权模式喝授权对象
acl.add(new ACL