import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper; public class setConfig { public static String url = "172.18.32.159:2181"; private final static String root = "/myConf"; private final static String UrlNode = root + "/url"; private final static String userNameNode = root + "/username"; private final static String passWdNode = root + "/passwd"; private final static String auth_type = "digest"; private final static String auth_passwd = "password"; private final static String URLString = "10.11.1.1"; private final static String UserName = "username123"; private final static String Passwd = "password123"; public static void main(String[] args) throws Exception { ZooKeeper zk = new ZooKeeper(url,3000,new Watcher() { public void process(WatchedEvent event) { System.out.println("触发了事件:" + event.getType()); } }); while(ZooKeeper.States.CONNECTED != zk.getState()) { Thread.sleep(3000); } zk.addAuthInfo(auth_type, auth_passwd.getBytes());//增加密钥 下面如果文件夹添加了加密功能 就需要密钥才可以访问 if(zk.exists(root, true) == null) { zk.create(root, "root".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); } if(zk.exists(UrlNode, true) == null) { zk.create(UrlNode, URLString.getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); } if(zk.exists(userNameNode, true) == null) { zk.create(userNameNode, UserName.getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); } if(zk.exists(passWdNode, true) == null) { zk.create(passWdNode, Passwd.getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); } zk.close(); }}//代码跑完到目录下运行查询目录
[zk: localhost:2181(CONNECTED) 1] ls /myConf
Authentication is not valid : /myConf//此时会出现没权限查看
[zk: localhost:2181(CONNECTED) 5] addauth digest password /myConf
[zk: localhost:2181(CONNECTED) 6] ls /myConf//要用此方式添加密码帐号才能进入下一级别目录.命令忘记可以用help 查看那