Java API访问ZK的权限控制

  • 无权限访问结点
/**
 * 对于ZK的授权访问
 * Created by liuhuichao on 2017/7/27.
 */
public class AutoSample {

    private static  String path="/test-1";
    public static void main(String[] args)throws Exception {
      //  ZooKeeper zk=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
       // zk.addAuthInfo("digest","lhc:true".getBytes());
       // zk.create(path,"lhc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
        //zk.create(path+"/node1","lhc".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);

        /**无授权信息访问**/
       // ZooKeeper zk2=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
       // byte[] result=zk2.getData(path,false,null);

        /**使用错误信息访问结点**/
        //ZooKeeper zk3=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
        //zk3.addAuthInfo("digest","lhc:false".getBytes());
       // zk3.getData(path,false,null);// KeeperErrorCode = NoAuth for /test-1

        /**测试删除节点**/
        ZooKeeper zk=new ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
        zk.addAuthInfo("digest","lhc:true".getBytes());
        zk.delete(path+"/node1",-1);

        ZooKeeper zk1=new  ZooKeeper("rc-zkp-datn-rse-nmg-ooz-woasis:2181",5000,null);
        zk1.delete(path,-1);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于 ZooKeeper 实现的分布式锁的 Java 代码示例: ```java import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; public class DistributedLock { private static final String LOCK_ROOT_PATH = "/locks"; private static final String LOCK_NODE_PREFIX = "lock-"; private final String lockPath; private final ZooKeeper zk; private String lockNode; private CountDownLatch lockAcquiredSignal; public DistributedLock(String zookeeperUrl, String lockPath) throws Exception { this.lockPath = LOCK_ROOT_PATH + "/" + lockPath; this.zk = new ZooKeeper(zookeeperUrl, 3000, null); ensureLockPathExists(); } public void lock() throws Exception { while (true) { try { lockNode = zk.create(lockPath + "/" + LOCK_NODE_PREFIX, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); lockAcquiredSignal = new CountDownLatch(1); waitForLock(); return; } catch (KeeperException.NodeExistsException e) { // node already exists, wait for the previous holder to release the lock waitForPreviousLock(); } } } public void unlock() throws Exception { zk.delete(lockNode, -1); lockNode = null; lockAcquiredSignal.countDown(); } private void ensureLockPathExists() throws Exception { if (zk.exists(lockPath, false) == null) { zk.create(lockPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } private void waitForPreviousLock() throws Exception { List<String> lockNodes = zk.getChildren(lockPath, false); Collections.sort(lockNodes); int currentLockIndex = lockNodes.indexOf(lockNode.substring(lockPath.length() + 1)); if (currentLockIndex <= 0) { throw new IllegalStateException("Lock node not found: " + lockNode); } String previousLockNode = lockPath + "/" + lockNodes.get(currentLockIndex - 1); Stat previousLockStat = zk.exists(previousLockNode, new LockWatcher()); if (previousLockStat != null) { lockAcquiredSignal.await(); } } private void waitForLock() throws Exception { List<String> lockNodes = zk.getChildren(lockPath, false); Collections.sort(lockNodes); int currentLockIndex = lockNodes.indexOf(lockNode.substring(lockPath.length() + 1)); if (currentLockIndex == 0) { // this is the first lock node, lock acquired return; } String previousLockNode = lockPath + "/" + lockNodes.get(currentLockIndex - 1); Stat previousLockStat = zk.exists(previousLockNode, new LockWatcher()); if (previousLockStat == null) { // previous lock node has been deleted, lock acquired return; } lockAcquiredSignal.await(); } private class LockWatcher implements Watcher { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeDeleted) { lockAcquiredSignal.countDown(); } } } } ``` 使用示例: ```java DistributedLock lock = new DistributedLock("localhost:2181", "my-lock"); lock.lock(); try { // critical section } finally { lock.unlock(); } ``` 该示例代码使用了 ZooKeeper 的临时顺序节点来实现分布式锁。当多个节点同时请求锁时,它们会在 ZooKeeper 中创建一个临时顺序节点,并按照节点名称的顺序等待前面的节点释放锁。当锁被释放时,它会通知等待的节点,让它们尝试重新获得锁。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值