zookeeper 实现分布式锁(cpu占用低)

Posted tecnologycc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper 实现分布式锁(cpu占用低)相关的知识,希望对你有一定的参考价值。

原理:

1 zookeeper 可以建立临时节点文件 znode  结构如高度为2的树  在 basepath+lockname上建立  basepath+lockname---1 basepath+lockname---2 basepath+lockname---3 的顺序节点

2 watcher机制。可以建立某一个节点的watcher callback method A,当be watch的node change, 会call the A mehod

note: these code not 原创,摘自 https://dzone.com/articles/distributed-lock-using

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class DistributedLock {
    private final ZooKeeper zk;
    private final String lockBasePath;
    private final String lockName;
    private String lockPath;
    public DistributedLock(ZooKeeper zk, String lockBasePath, String lockName) {
        this.zk = zk;
        this.lockBasePath = lockBasePath;
        this.lockName = lockName;
    }
    public void lock() throws IOException {
        try {
// lockPath will be different than (lockBasePath + "/" + lockName) becuase of the sequence number ZooKeeper appends
            lockPath = zk.create(lockBasePath + "/" + lockName, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            final Object lock = new Object();
            synchronized(lock) {
                while(true) {
                    List<String> nodes = zk.getChildren(lockBasePath, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            synchronized (lock) {
                                lock.notifyAll();
                            }
                        }
                    });
                    Collections.sort(nodes); // ZooKeeper node names can be sorted lexographically
                    if (lockPath.endsWith(nodes.get(0))){
                        return;
                    } else {
                        lock.wait();
                    }
                }
            }
        } catch (KeeperException e) {
            throw new IOException (e);
        } catch (InterruptedException e) {
            throw new IOException (e);
        }
    }
    public void unlock() throws IOException {
        try {
            zk.delete(lockPath, -1);
            lockPath = null;
        } catch (KeeperException e) {
            throw new IOException (e);
        } catch (InterruptedException e) {
            throw new IOException (e);
        }
    }
}

 

以上是关于zookeeper 实现分布式锁(cpu占用低)的主要内容,如果未能解决你的问题,请参考以下文章

Zookeeper分布式锁

基于zookeeper实现分布式锁

分布式锁 Zookeeper分布式锁

redis分布式锁扣减库存弊端: 吞吐量低, 解决方法:使用 分段锁 分布式分段锁并发扣减库存--代码实现

Redis实现分布式锁与Zookeeper实现分布式锁区别

漫谈分布式锁之ZooKeeper实现