zoukankan      html  css  js  c++  java
  • zookeeper java调用及权限控制


    import java.io.IOException;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.KeeperException;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.ZooDefs;
    import org.apache.zookeeper.ZooDefs.Ids;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.data.ACL;
    import org.apache.zookeeper.data.Id;
    import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

    /**
     * @author Keeny (2013-12-31)
     **/
    public class Test {
        public static void main(String[] args) throws IOException, KeeperException, InterruptedException, NoSuchAlgorithmException {
            ZooKeeper zk = new ZooKeeper("192.168.192.142:2181", 500000, new Watcher() {
                // 监控全部被触发的事件
                public void process(WatchedEvent event) {
                    System.out.println("WatchedEvent=" + event.toString());
                    // dosomething
                }
            });
            // 创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即clientshutdown了也不会消失)

            
            List<ACL> acls = new ArrayList<ACL>(2);     
            Id id1 = new Id("digest", DigestAuthenticationProvider.generateDigest("admin:admin123"));  
            ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);  
              
            Id id2 = new Id("digest", DigestAuthenticationProvider.generateDigest("guest:guest123"));  
            ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);  
              
            acls.add(acl1);  
            acls.add(acl2);  //设置权限
            
    //        String root = zk.create("/root/test1", "test".getBytes(), acls, CreateMode.PERSISTENT);
    //        System.out.println("root=" + root);

            // 在root以下创建一个childone znode,数据为childone,不进行ACL权限控制。节点为永久性的
            // String childone = zk.create("/root/childone", "childone".getBytes(),
            // Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            // System.out.println("childone=" + childone);

            // 取得/root节点下的子节点名称,返回List<String>
            zk.addAuthInfo("digest", "admin:chinatetadmin1234".getBytes());   //设置权限
            
            List<String> lsit = zk.getChildren("/root", true);
            for (String string : lsit) {
                System.out.println("节点:" + string);
            }

            // 取得/root/childone节点下的数据,返回byte[]
             byte[] childoneData = zk.getData("/root/msg", true, null);
             System.out.println("childoneData=" + new String(childoneData));

            // 改动节点/root/childone下的数据,第三个參数为版本号。假设是-1,那会无视被改动的数据版本号,直接改掉
            // zk.setData("/root/childone", "childonemodify".getBytes(), -1);

    //         删除/root/childone这个节点,第二个參数为版本号,-1的话直接删除,无视版本号
    //         zk.delete("/root/msg", -1);
            //
            // zk.delete("/root/msg", -1);
            // 关闭session
            zk.close();
        }
    }


  • 相关阅读:
    递归
    数据结构与算法-复杂度分析
    'latin-1' codec can't encode characters in position解决字符问题
    redis理解
    spring 嵌套事务问题
    Parameterized testing with any Python test framework
    分布式事务测试考虑点
    python orm / 表与model相互转换
    Python多线程、多进程
    JS运动
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10084476.html
Copyright © 2011-2022 走看看