Fork me on GitHub

分布式锁的三种实现方式

分布式锁的三种实现方式

一、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);  
        }  
    }  

转自:

  • http://surlymo.iteye.com/blog/2082684
  • http://blog.csdn.net/ugg/article/details/41894947
posted @ 2016-11-09 10:38  秋楓  阅读(...)  评论(...编辑  收藏