分布式鎖的三種實現方式
一、zookeeper
1、實現原理:
基於zookeeper瞬時有序節點實現的分布式鎖,其主要邏輯如下(該圖來自於IBM網站)。大致思想即為:每個客戶端對某個功能加鎖時,在zookeeper上的與該功能對應的指定節點的目錄下,生成一個唯一的瞬時有序節點。判斷是否獲取鎖的方式很簡單,只需要判斷有序節點中序號最小的一個。當釋放鎖的時候,只需將這個瞬時節點刪除即可。同時,其可以避免服務宕機導致的鎖無法釋放,而產生的死鎖問題。
2、優點
鎖安全性高,zk可持久化
3、缺點
性能開銷比較高。因為其需要動態產生、銷毀瞬時節點來實現鎖功能。
4、實現
可以直接采用zookeeper第三方庫curator即可方便地實現分布式鎖。以下為基於curator實現的zk分布式鎖核心代碼:
@Override
public boolean tryLock(LockInfo info) {
InterProcessMutex mutex = getMutex(info);
int tryTimes = info.getTryTimes();
long tryInterval = info.getTryInterval();
boolean flag = true;// 代表是否需要重試
while (flag && --tryTimes >= 0) {
try {
if (mutex.acquire(info.getWaitLockTime(), TimeUnit.MILLISECONDS)) {
LOGGER.info(LogConstant.DST_LOCK + "acquire lock successfully!");
flag = false;
break;
}
} catch (Exception e) {
LOGGER.error(LogConstant.DST_LOCK + "acquire lock error!", e);
} finally {
checkAndRetry(flag, tryInterval, tryTimes);
}
}
return !flag;// 最后還需要重試,說明沒拿到鎖
}
@Override
public boolean releaseLock(LockInfo info) {
InterProcessMutex mutex = getMutex(info);
int tryTimes = info.getTryTimes();
long tryInterval = info.getTryInterval();
boolean flag = true;// 代表是否需要重試
while (flag && --tryTimes >= 0) {
try {
mutex.release();
LOGGER.info(LogConstant.DST_LOCK + "release lock successfully!");
flag = false;
break;
} catch (Exception e) {
LOGGER.error(LogConstant.DST_LOCK + "release lock error!", e);
} finally {
checkAndRetry(flag, tryInterval, tryTimes);
}
}
return !flag;// 最后還需要重試,說明沒拿到鎖
}
/**
* 獲取鎖。此處需要加同步,concurrentHashmap無法避免此處的同步問題
* @param info 鎖信息
* @return 鎖實例
*/
private synchronized InterProcessMutex getMutex(LockInfo info) {
InterProcessReadWriteLock lock = null;
if (locksCache.get(info.getLock()) != null) {
lock = locksCache.get(info.getLock());
} else {
lock = new InterProcessReadWriteLock(client, BASE_DIR + info.getLock());
locksCache.put(info.getLock(), lock);
}
InterProcessMutex mutex = null;
switch (info.getIsolate()) {
case READ:
mutex = lock.readLock();
break;
case WRITE:
mutex = lock.writeLock();
break;
default:
throw new IllegalArgumentException();
}
return mutex;
}
/**
* 判斷是否需要重試
* @param flag 是否需要重試標志
* @param tryInterval 重試間隔
* @param tryTimes 重試次數
*/
private void checkAndRetry(boolean flag, long tryInterval, int tryTimes) {
try {
if (flag) {
Thread.sleep(tryInterval);
LOGGER.info(LogConstant.DST_LOCK + "retry getting lock! now retry time left: " + tryTimes);
}
} catch (InterruptedException e) {
LOGGER.error(LogConstant.DST_LOCK + "retry interval thread interruptted!", e);
}
}
二、memcached分布式鎖
1、實現原理:
memcached帶有add函數,利用add函數的特性即可實現分布式鎖。add和set的區別在於:如果多線程並發set,則每個set都會成功,但最后存儲的值以最后的set的線程為准。而add的話則相反,add會添加第一個到達的值,並返回true,后續的添加則都會返回false。利用該點即可很輕松地實現分布式鎖。
2、優點
並發高效。
3、缺點
- (1)memcached采用列入LRU置換策略,所以如果內存不夠,可能導致緩存中的鎖信息丟失。
- (2)memcached無法持久化,一旦重啟,將導致信息丟失。
三、redis分布式鎖
redis分布式鎖即可以結合zk分布式鎖鎖高度安全和memcached並發場景下效率很好的優點,可以利用jedis客戶端實現。參考http://blog.csdn.net/java2000_wl/article/details/8740911
/**
* @author http://blog.csdn.net/java2000_wl
* @version <b>1.0.0</b>
*/
public class RedisBillLockHandler implements IBatchBillLockHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisBillLockHandler.class);
private static final int DEFAULT_SINGLE_EXPIRE_TIME = 3;
private static final int DEFAULT_BATCH_EXPIRE_TIME = 6;
private final JedisPool jedisPool;
/**
* 構造
* @author http://blog.csdn.net/java2000_wl
*/
public RedisBillLockHandler(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* 獲取鎖 如果鎖可用 立即返回true, 否則返回false
* @author http://blog.csdn.net/java2000_wl
* @param billIdentify
* @return
*/
public boolean tryLock(IBillIdentify billIdentify) {
return tryLock(billIdentify, 0L, null);
}
/**
* 鎖在給定的等待時間內空閑,則獲取鎖成功 返回true, 否則返回false
* @author http://blog.csdn.net/java2000_wl
* @param billIdentify
* @param timeout
* @param unit
* @return
*/
public boolean tryLock(IBillIdentify billIdentify, long timeout, TimeUnit unit) {
String key = (String) billIdentify.uniqueIdentify();
Jedis jedis = null;
try {
jedis = getResource();
long nano = System.nanoTime();
do {
LOGGER.debug("try lock key: " + key);
Long i = jedis.setnx(key, key);
if (i == 1) {
jedis.expire(key, DEFAULT_SINGLE_EXPIRE_TIME);
LOGGER.debug("get lock, key: " + key + " , expire in " + DEFAULT_SINGLE_EXPIRE_TIME + " seconds.");
return Boolean.TRUE;
} else { // 存在鎖
if (LOGGER.isDebugEnabled()) {
String desc = jedis.get(key);
LOGGER.debug("key: " + key + " locked by another business:" + desc);
}
}
if (timeout == 0) {
break;
}
Thread.sleep(300);
} while ((System.nanoTime() - nano) < unit.toNanos(timeout));
return Boolean.FALSE;
} catch (JedisConnectionException je) {
LOGGER.error(je.getMessage(), je);
returnBrokenResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
returnResource(jedis);
}
return Boolean.FALSE;
}
/**
* 如果鎖空閑立即返回 獲取失敗 一直等待
* @author http://blog.csdn.net/java2000_wl
* @param billIdentify
*/
public void lock(IBillIdentify billIdentify) {
String key = (String) billIdentify.uniqueIdentify();
Jedis jedis = null;
try {
jedis = getResource();
do {
LOGGER.debug("lock key: " + key);
Long i = jedis.setnx(key, key);
if (i == 1) {
jedis.expire(key, DEFAULT_SINGLE_EXPIRE_TIME);
LOGGER.debug("get lock, key: " + key + " , expire in " + DEFAULT_SINGLE_EXPIRE_TIME + " seconds.");
return;
} else {
if (LOGGER.isDebugEnabled()) {
String desc = jedis.get(key);
LOGGER.debug("key: " + key + " locked by another business:" + desc);
}
}
Thread.sleep(300);
} while (true);
} catch (JedisConnectionException je) {
LOGGER.error(je.getMessage(), je);
returnBrokenResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
returnResource(jedis);
}
}
/**
* 釋放鎖
* @author http://blog.csdn.net/java2000_wl
* @param billIdentify
*/
public void unLock(IBillIdentify billIdentify) {
List<IBillIdentify> list = new ArrayList<IBillIdentify>();
list.add(billIdentify);
unLock(list);
}
/**
* 批量獲取鎖 如果全部獲取 立即返回true, 部分獲取失敗 返回false
* @author http://blog.csdn.net/java2000_wl
* @date 2013-7-22 下午10:27:44
* @param billIdentifyList
* @return
*/
public boolean tryLock(List<IBillIdentify> billIdentifyList) {
return tryLock(billIdentifyList, 0L, null);
}
/**
* 鎖在給定的等待時間內空閑,則獲取鎖成功 返回true, 否則返回false
* @author http://blog.csdn.net/java2000_wl
* @param billIdentifyList
* @param timeout
* @param unit
* @return
*/
public boolean tryLock(List<IBillIdentify> billIdentifyList, long timeout, TimeUnit unit) {
Jedis jedis = null;
try {
List<String> needLocking = new CopyOnWriteArrayList<String>();
List<String> locked = new CopyOnWriteArrayList<String>();
jedis = getResource();
long nano = System.nanoTime();
do {
// 構建pipeline,批量提交
Pipeline pipeline = jedis.pipelined();
for (IBillIdentify identify : billIdentifyList) {
String key = (String) identify.uniqueIdentify();
needLocking.add(key);
pipeline.setnx(key, key);
}
LOGGER.debug("try lock keys: " + needLocking);
// 提交redis執行計數
List<Object> results = pipeline.syncAndReturnAll();
for (int i = 0; i < results.size(); ++i) {
Long result = (Long) results.get(i);
String key = needLocking.get(i);
if (result == 1) { // setnx成功,獲得鎖
jedis.expire(key, DEFAULT_BATCH_EXPIRE_TIME);
locked.add(key);
}
}
needLocking.removeAll(locked); // 已鎖定資源去除
if (CollectionUtils.isEmpty(needLocking)) {
return true;
} else {
// 部分資源未能鎖住
LOGGER.debug("keys: " + needLocking + " locked by another business:");
}
if (timeout == 0) {
break;
}
Thread.sleep(500);
} while ((System.nanoTime() - nano) < unit.toNanos(timeout));
// 得不到鎖,釋放鎖定的部分對象,並返回失敗
if (!CollectionUtils.isEmpty(locked)) {
jedis.del(locked.toArray(new String[0]));
}
return false;
} catch (JedisConnectionException je) {
LOGGER.error(je.getMessage(), je);
returnBrokenResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
returnResource(jedis);
}
return true;
}
/**
* 批量釋放鎖
* @author http://blog.csdn.net/java2000_wl
* @param billIdentifyList
*/
public void unLock(List<IBillIdentify> billIdentifyList) {
List<String> keys = new CopyOnWriteArrayList<String>();
for (IBillIdentify identify : billIdentifyList) {
String key = (String) identify.uniqueIdentify();
keys.add(key);
}
Jedis jedis = null;
try {
jedis = getResource();
jedis.del(keys.toArray(new String[0]));
LOGGER.debug("release lock, keys :" + keys);
} catch (JedisConnectionException je) {
LOGGER.error(je.getMessage(), je);
returnBrokenResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
returnResource(jedis);
}
}
/**
* @author http://blog.csdn.net/java2000_wl
* @date 2013-7-22 下午9:33:45
* @return
*/
private Jedis getResource() {
return jedisPool.getResource();
}
/**
* 銷毀連接
* @author http://blog.csdn.net/java2000_wl
* @param jedis
*/
private void returnBrokenResource(Jedis jedis) {
if (jedis == null) {
return;
}
try {
//容錯
jedisPool.returnBrokenResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
/**
* @author http://blog.csdn.net/java2000_wl
* @param jedis
*/
private void returnResource(Jedis jedis) {
if (jedis == null) {
return;
}
try {
jedisPool.returnResource(jedis);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
轉自: