@Test
public void testAclServer() {
List<ACL> acls = new ArrayList<ACL>(2);
try {
Id id1 = new Id("digest", DigestAuthenticationProvider.generateDigest("fish:fishpw"));
ACL acl1 = new ACL(ZooDefs.Perms.WRITE, id1);
Id id2 = new Id("digest", DigestAuthenticationProvider.generateDigest("qsd:qsdpw"));
ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);
acls.add(acl1);
acls.add(acl2);
// 所有用户都有权限
// Id world = new Id("world", "anyone");
// ACL worldAcl = new ACL(ZooDefs.Perms.READ, world);
// acls.add(worldAcl);
// 10.0.2.76是本机IP
// Id id3 = new Id("ip", "10.0.2.76");
// ACL acl3 = new ACL(ZooDefs.Perms.WRITE, id3);
// acls.add(acl3);
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
ZooKeeper zk = null;
try {
zk = new ZooKeeper("10.0.1.75:2181,10.0.1.76:2181,10.0.1.77:2181", 300000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!");
}
});
if (zk.exists("/test", true) == null) {
System.out.println(zk.create("/test", "ACL测试".getBytes(), acls, CreateMode.PERSISTENT));
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
@Test
public void testAclClient() {
try {
ZooKeeper zk = new ZooKeeper("10.0.1.75:2181,10.0.1.76:2181,10.0.1.77:2181", 300000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!");
}
});
// 只有写权限
zk.addAuthInfo("digest", "fish:fishpw".getBytes());
// 只有读权限
zk.addAuthInfo("digest", "qsd:qsdpw".getBytes());
System.out.println(new String(zk.getData("/test", null, null)));
zk.setData("/test", "I change!".getBytes(), -1);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有