创建节点 create
持久节点
临时节点 -e
有序节点 -s
无序节点
持久有序: 分布式id
临时有序: 分布式锁
set /aa “aaa” -v 3 (要和版本号一致)
delete /aa -v 2 (也是考虑版本号)
deleteall 删除这个路径下的所有的节点
get 查看节点
返回数据+属性
查看状态
stat 只返回属性
读取子节点列表
ls
ls2 另加当前节点节点属性
监听器
应用 之一:zookeeper做配置中心,一旦有修改,通知应用程序去读取最新的信息。
zookeeper 监听器都是一次性的。
get path watch
监听节点内容
stat path watch
监听节点状态
ls/ls2 path watch
监听该节点下所有子节点的增加和删除操作
ACL权限控制
scheme: id : permission 来标识
-
scheme 授权模式
world
ip
auth
digest -
id: 授权对象
-
permission : 授予的权限
例如
setAcl /test ip:192.168.194.128:crwda
//将节点权限设置为ip为192.168.194.128的客户端可以进行增删改查和管理权限
getAcl
setAcl
addauth 添加授权用户
world 模式
setAcl /test world:anyone:crwda
ip模式
setAcl /test ip:192.168.194.128:crwda,ip:192.168.128.123:crwda
Auth 模式 (明文密码)
//先添加认证用户
addauth digest dong:123456
//设置
setAcl /node auth:dong:123456:crwda
Digest 授权模式(密文密码)
多种授权模式
setAcl /node ip:192.168.194.128:crwda,auth:dong:123456:crwda,digest:dong:密文:cdrwa
分布式锁
curator
public class CuratorConnection {
public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.194.128:2181")
//会话超时时间
.sessionTimeoutMs(5000)
//重连机制
.retryPolicy(new RetryOneTime(3000))
//命名空间
.namespace("create")
.build();
client.start();
System.out.println("lianjieshangle ");
//新增节点,
client.create()
//可以递归创建节点树
.creatingParentsIfNeeded()
//结点的类型
.withMode(CreateMode.PERSISTENT)
//结点的权限列表 worlany:one:cdrwa
.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
// 异步创建节点,异步回调接口
.inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
System.out.println(curatorEvent.getPath());
System.out.println(curatorEvent.getType());
}
})
//arg1:节点的路径
//arg2:节点的数据
.forPath("/node1","node1".getBytes());
System.out.println("结束");
List<ACL> list = new ArrayList<>();
//授权模式和授权对象
Id id = new Id("ip","192.168.194.128");
list.add(new ACL(ZooDefs.Perms.ALL,id));
client.close();
}
}
//修改节点
client.setData()
.withVersion(2)
.forPath("/node1","111".getBytes());
//删除节点
client.delete()
//删除包含子节点的节点
.deletingChildrenIfNeeded()
.withVersion(2)
.forPath("/node1");
//查看节点
byte[] bytes = client.getdata()
.forPath("/node");
//查看子节点
List<String> list = client.getChildren()
.forPath("/node");
//节点是否存在
Stat stat = client.checkExists()
.forPath("/node");
监视器
事务