分布式锁-基于zookeeper客户端curator实现锁机制(一)锁基础
2018-02-01 22:05
267 查看
分布式锁基础:继承接口的锁实现,以统一的接口调用完成,封装错误处理
定义接口,实现此接口都拥有获取锁与释放锁
/* * Copyright 2016-2018. Individual ownership panw * * Tech blog at http://blog.csdn.net/hatsune_miku_. * * Contact Mailbox at <a href="mailto:mikuismywifu@gmail.com">panw</a>. * */ package com.miku.common.util.lock; import org.apache.curator.framework.recipes.locks.InterProcessLock; /** * @author:panw. * @date:2018/1/16. */ public interface Lock { /** * 获取锁 - 根据接口子类推断出当前锁类型 * * @see org.apache.curator.framework.recipes.locks.InterProcessMultiLock * @see org.apache.curator.framework.recipes.locks.InterProcessMutex * @see org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex * @param interProcessLock * @throws Exception */ void lock(InterProcessLock interProcessLock) throws Exception; /** * 获取锁 - 阻止直到可用或给定的时间到期 * * @param interProcessLock * @param milliseconds * @throws Exception */ boolean lock(InterProcessLock interProcessLock,long milliseconds) throws Exception; /** * 释放锁 - 根据锁接口子类推断出当前锁类型 * * @see org.apache.curator.framework.recipes.locks.InterProcessMultiLock * @see org.apache.curator.framework.recipes.locks.InterProcessMutex * @see org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex * @param interProcessLock * @throws Exception */ void unlock(InterProcessLock interProcessLock) throws Exception; }
锁抽象,获取zk上下文锁地址,zk链接地址,监听zk链接状态
/* * Copyright 2016-2018. Individual ownership panw * * Tech blog at http://blog.csdn.net/hatsune_miku_. * * Contact Mailbox at <a href="mailto:mikuismywifu@gmail.com">panw</a>. * */ package com.miku.common.util.lock.support; import com.miku.common.util.lock.Lock; import com.miku.common.utils.spring.SpringUtils; import com.miku.common.zookeeper.ZookeeperClient; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.locks.InterProcessLock; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** * zookeeper分布式锁实现,所有获取锁都是公平锁的实现 * * @see org.apache.curator.framework.recipes.locks * * @author:panw. * @date:2018/1/17. */ public abstract class AbstractZookeeperLock<E extends InterProcessLock> implements Lock{ public static CuratorFramework curatorFramework; public final ConcurrentHashMap<String,E> locks = new ConcurrentHashMap<>(); public AbstractZookeeperLock(){ ZookeeperClient zookeeperClient = SpringUtils.getBean(ZookeeperClient.class); curatorFramework = zookeeperClient.getClient(); } @Override public void lock(InterProcessLock interProcessLock) throws Exception{ try { interProcessLock.acquire(); lockConnectionStateListenable(); } catch (Exception e) { throw new IllegalStateException("锁丢失=>" + e); } } @Override public boolean lock(InterProcessLock interProcessLock,long time) throws Exception{ boolean state; try { state = interProcessLock.acquire(time, TimeUnit.MILLISECONDS); lockConnectionStateListenable(); } catch (Exception e) { throw new IllegalStateException("锁丢失=>" + e); } return state; } @Override public void unlock(InterProcessLock interProcessLock) throws Exception{ try { interProcessLock.release(); } catch (Exception e) { throw new IllegalStateException("锁丢失=>" + e); } } /** * 监听zk连接状态. */ public void lockConnectionStateListenable() throws Exception{ curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override // ConnectionStateListener并监视SUSPENDED和LOST状态更改。如果报告“挂起”状态,则不能确定是否仍然保持锁定状态, // 除非您随后收到“重新连接”状态。如果报告失去状态,则确定您不再持有该锁 public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { // TODO 考虑删除锁实例 throw new IllegalStateException("会话超时锁丢失=>"); } } }); } }
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator_version}</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>${curator_version}</version> </dependency>
博客原创:写作不易,转载请标明出处。文章地址:https://blog.csdn.net/hatsune_miku_/article/details/79233888(๑˃∀˂๑)♪阿里嘎多(๑˃∀˂๑)♪
分布式锁-基于zookeeper客户端curator实现锁机制(一)锁基础
分布式锁-zookeeper-SharedLock基于InterProcessSemaphoreMutex(二)分布式共享锁
分布式锁-zookeeper-SharedReentrantLock基于InterProcessMutex(三)分布式重入共享锁
分布式锁-zookeeper-SharedReentrantReadWriteLock基于InterProcessReadWriteLock(四)分布式重入读写共享锁
相关文章推荐
- 基于zookeeper简单实现分布式锁
- 基于select模型的udp客户端实现超时机制
- 基于zookeeper实现分布式锁
- 基于select模型的udp客户端实现超时机制
- 基于zookeeper的分布式锁实现
- 基于zookeeper的分布式锁实现
- curator实现zookeeper的分布式锁
- 基于zookeeper的分布式锁实现
- 基于zookeeper实现分布式锁
- 基于zookeeper简单实现分布式锁
- 基于zookeeper的分布式锁实现(监听父节点)
- 4000 基于zookeeper实现分布式锁
- 分布式锁实现方式一 基于zookeeper的分布式锁
- 基于zookeeper的分布式锁实现
- 基于zookeeper解决分布式锁问题与缓存技术实现分布式锁
- 基于Zookeeper的分布式锁实现
- 第5章:基于zookeeper的分布式锁实现
- 基于zookeeper的分布式锁实现
- 基于zookeeper简单实现分布式锁
- 分布式锁基于zookeeper实现