ZooKeeper支持的权限:
CREATE: you can create a child node
READ: you can get data from a node and list its children.
WRITE: you can set data for a node
DELETE: you can delete a child node
ADMIN: you can set permissions
ZooKeeper内建的sheme:(scheme是ID的其中一个属性)
world has a single id, anyone, that represents anyone.
auth doesn't use any id, represents any authenticated user.
digest uses a username:password string to generate MD5 hash which is then used as an ACL ID identity. Authentication is done by sending theusername:password in clear text. When used in the ACL the expression will be the username:base64 encoded SHA1 password digest.
ip uses the client host IP as an ACL ID identity. The ACL expression is of the form addr/bits where the most significant bits of addr are matched against the most significant bits of the client host IP.
ZK内建的ID:
ANYONE_ID_UNSAFE //任意用户
AUTH_IDS //通过Auth认证过的用户
内建的权限控制集合:
OPEN_ACL_UNSAFE: 创建任何人都可以操作的节点
READ_ACL_UNSAFE: 创建任何人都可以读的节点
CREATOR_ALL_ACL: 设置了Auth的用户可以使用该ACL集合创建节点,该节点也只能被同样Auth授权的用户操作
示例代码如下:
- @Test
- public void testACL_with_ip_scheme() {
- try {
- Id id = new Id();
- id.setScheme("ip");
- id.setId(InetAddress.getLocalHost().getHostAddress());
- ACL acl = new ACL();
- acl.setId(id); //对ID所指定的目标设置权限
- acl.setPerms(Perms.ALL);
- List<ACL> acls = new ArrayList<ACL>();
- acls.add(acl); //可以添加多个运行的IP地址
- String p = "/ip";
- zk.create(p, p.getBytes(), acls, CreateMode.PERSISTENT);
- zk.delete(p, -1); //仅IP相同的用户可以对该进行进行操作
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
Watcher
可以设置Watcher的方式:
1) 在ZooKeeper的构造函数中可以设置Watcher
2) 使用ZooKeeper.register(Watcher)显示的更改在构造函数中设置的默认Watcher
3) 通过某些方法的调用可以更改某个path对应节点的Watcher?
具体可以设置Watcher的方法如下所示:
1) 构造函数: state changes or node events
2) Register: 修改构造函数中指定的默认Watcher.
3) getData: triggered by sets data on the node, or deletes the node.
4) getChildren: triggered by deletes the node or creates/delete a child under the node.
5) exists: triggered by creates/delete the node or sets the data on the node.
其中构造函数阶段指定的Watcher一直有效(register方式属于该类),其余方法设置的Watcher仅有效一次。在方法调用时,如果指定开启watcher,如果该节点通过getData、getChildren和exists设置了Watcher,就触发该Watcher,然后使得该Watcher失效(但默认的Watcher还一直生效),否则触发构造函数中设定的默认Watcher。
示例代码如下:
- class ExistsWatcher implements Watcher {
- @Override
- public void process(WatchedEvent event) {
- System.out.println("---------------------------");
- System.out.println("setting by exist watcher");
- System.out.println("path is : " + event.getPath());
- System.out.println("type is : " + event.getType());
- System.out.println("state is : " + event.getState());
- System.out.println("---------------------------");
- }
- }
- class DefaultWatcher implements Watcher {
- @Override
- public void process(WatchedEvent event) {
- System.out.println("=====>Default Watch Event: " + event.getType());
- }
- }
- @Test
- public void testWatcher() {
- try {
- DefaultWatcher defaultWatcher = new DefaultWatcher();
- ExistsWatcher existsWatcher = new ExistsWatcher();
- String p = "/watcher";
- ZooKeeper zk = new ZooKeeper(hostport, 300000, null);
- zk.register(defaultWatcher);
- Stat stat = zk.exists(p, existsWatcher);
- zk.create(p, p.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- stat = zk.exists(p, true);
- byte[] b = zk.getData(p, true, stat);
- System.out.println(new String(b));
- stat = zk.exists(p, true);
- zk.setData(p, "Iloveyou".getBytes(), stat.getVersion());
- stat = zk.exists(p, existsWatcher);
- zk.delete(p, stat.getVersion());
- zk.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
运行结果如下:
- =====>Default Watch Event: None
- ---------------------------
- setting by exist watcher
- path is : /watcher
- type is : NodeCreated
- state is : SyncConnected
- ---------------------------
- /watcher
- =====>Default Watch Event: NodeDataChanged
- ---------------------------
- setting by exist watcher
- path is : /watcher
- type is : NodeDeleted
- state is : SyncConnected
- ---------------------------
异步调用
顾名思义,异步调用是指在调用某个方法后不等待其返回,而是接着处理下面的任务,当方法调用完成时触发某个回调函数,回调函数需要在方法调用时指定,然后在回调函数中处理方法调用的结果。
在ZK中,几乎为每个方法都提供了异步调用的版本,如getData方法,其函数原型如下所示:
void getData(String path, boolean watch, DataCallback cb, Object ctx);
其中:
DataCallback为提供回调函数的类,
ctx为回调函数需要的参数
示例代码如下:
- Children2Callback cb = new Children2Callback() {
- @Override
- public void processResult(int rc, String path, Object ctx,
- List<String> children, Stat stat) {
- for (String s : children) {
- System.out.println("----->" + s);
- }
- System.out.println(ctx); //输出:helloworld
- }
- };
- zk.getChildren(path, true, cb, "helloworld");
相信通过上面的学习一定能够熟悉ZK客户端的编程思路。
