zookeeper分布式锁代码实例
生活随笔
收集整理的這篇文章主要介紹了
zookeeper分布式锁代码实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
redis分布式鎖
模板:
/*** 分布式鎖模板類* Created by sunyujia@aliyun.com on 2016/2/23.*/ public interface DistributedLockTemplate {/**** @param lockId 鎖id(對應業務唯一ID)* @param timeout 單位毫秒* @param callback 回調函數* @return*/public Object execute(String lockId,int timeout,Callback callback); } public interface DistributedReentrantLock {public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException;public void unlock(); }zk鎖:
public class ZkDistributedLockTemplate implements DistributedLockTemplate {private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZkDistributedLockTemplate.class);private CuratorFramework client;public ZkDistributedLockTemplate(CuratorFramework client) {this.client = client;}private boolean tryLock(ZkReentrantLock distributedReentrantLock,Long timeout) throws Exception {return distributedReentrantLock.tryLock(timeout, TimeUnit.MILLISECONDS);}@Overridepublic Object execute(String lockId, int timeout, Callback callback) {ZkReentrantLock distributedReentrantLock = null;boolean getLock=false;try {distributedReentrantLock = new ZkReentrantLock(client,lockId);if(tryLock(distributedReentrantLock,new Long(timeout))){getLock=true;return callback.onGetLock();}else{return callback.onTimeout();}}catch(InterruptedException ex){log.error(ex.getMessage(), ex);Thread.currentThread().interrupt();}catch (Exception e) {log.error(e.getMessage(), e);}finally {if(getLock){distributedReentrantLock.unlock();}}return null;} } /*** 基于Zookeeper的可重入互斥鎖(關于重入:僅限于持有zk鎖的jvm內重入)* Created by sunyujia@aliyun.com on 2016/2/24.*/ public class ZkReentrantLock implements DistributedReentrantLock {private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZkReentrantLock.class);/*** 線程池*/private static final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);/*** 所有PERSISTENT鎖節點的根位置*/public static final String ROOT_PATH = "/ROOT_LOCK/";/*** 每次延遲清理PERSISTENT節點的時間 Unit:MILLISECONDS*/private long delayTimeForClean = 1000;/*** zk 共享鎖實現*/private InterProcessMutex interProcessMutex = null;/*** 鎖的ID,對應zk一個PERSISTENT節點,下掛EPHEMERAL節點.*/private String path;/*** zk的客戶端*/private CuratorFramework client;public ZkReentrantLock(CuratorFramework client, String lockId) {init(client, lockId);}public void init(CuratorFramework client, String lockId) {this.client = client;this.path = ROOT_PATH + lockId;interProcessMutex = new InterProcessMutex(client, this.path);}@Overridepublic boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {try {return interProcessMutex.acquire(timeout, unit);} catch (InterruptedException e) {throw e;} catch (Exception e) {log.error(e.getMessage(),e);throw new RuntimeException(e.getMessage(),e);}}@Overridepublic void unlock() {try {interProcessMutex.release();} catch (Throwable e) {log.error(e.getMessage(), e);} finally {executorService.schedule(new Cleaner(client, path), delayTimeForClean, TimeUnit.MILLISECONDS);}}static class Cleaner implements Runnable {CuratorFramework client;String path;public Cleaner(CuratorFramework client, String path) {this.client = client;this.path = path;}@Overridepublic void run() {try {List list = client.getChildren().forPath(path);if (list == null || list.isEmpty()) {client.delete().forPath(path);}} catch (KeeperException.NoNodeException e1) {//nothing} catch (KeeperException.NotEmptyException e2) {//nothing} catch (Exception e) {log.error(e.getMessage(), e);//準備刪除時,正好有線程創建鎖}}} }總結
以上是生活随笔為你收集整理的zookeeper分布式锁代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用nio多线程下载网络文件实例
- 下一篇: canal介绍和使用docker安装ca