Redis分布式锁

LockService

 

package com.das.service.lock;

import com.das.common.constant.EtcConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author liangmy
 * @Date 2019/10/23
 */
public interface LockService {

    /**
     * liangmy
     *
     * @param carOwnerUserId
     * @param strategy
     * @return
     */
    Object doSomething2(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy);

    /**
     * 分布式锁 一
     * 一般是使用 setnx(set if not exists) 指令,只允许被一个客户端占用资源。先来先占,用完了,再调用 del 指令释放资源
     * 但是有个问题,如果逻辑执行到中间出现异常了,可能会导致 del 指令没有被调用,这样 就会陷入死锁,锁永远得不到释放
     * 于是我们在拿到锁之后,再给锁加上一个过期时间,比如 5s,这样即使中间出现异常也 可以保证 5 秒之后锁会自动释放。
     * 但是以上逻辑还有问题。如果在 setnx 和 expire 之间服务器进程突然挂掉了,可能是因 为机器掉电或者是被人为杀掉的,就会导致 expire 得不到执行,也会造成死锁。
     * Redis 2.8 版本中作者加入了 set 指令的扩展参数,使得 setnx 和 expire 指令可以一起执行
     * set key value [EX seconds] [PX milliseconds] [NX|XX]
     *
     * @param carOwnerUserId
     * @param strategy
     * @return
     */
    Object doSomething3(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy);

    /**
     * 分布式锁 二 : 超时问题
     * 如果在加锁和释放锁之间的逻辑执行的太长,以至 于超出了锁的超时限制,就会出现问题。
     * 因为这时候锁过期了,第二个线程重新持有了这把锁, 但是紧接着第一个线程执行完了业务逻辑,就把锁给释放了,
     * 第三个线程就会在第二个线程逻 辑执行完之间拿到了锁。
     * 安全的方案是为 set 指令的 value 参数设置为一个随机数,释放锁时先匹配 随机数是否一致,然后再删除 key
     * 但是匹配 value 和删除 key 不是一个原子操作,这就需要使用 Lua 脚本来处理了,因为 Lua 脚本可 以保证连续多个指令的原子性执行。
     * if redis.call("get",KEYS[1]) == ARGV[1] then
     *     return redis.call("del",KEYS[1])
     * else
     *     return 0
     * end
     *
     * @param carOwnerUserId
     * @param strategy
     * @return
     */
    Object doSomething4(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy);
}
LockServiceImpl
package com.das.service.lock.impl;

import com.das.common.constant.EtcConstant;
import com.das.common.exception.DasException;
import com.das.common.exception.ExceptionCode;
import com.das.redis.DasJedis;
import com.das.service.lock.LockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author liangmy
 * @Date 2019/10/29
 */
@Service
@Slf4j
public class LockServiceImpl implements LockService {

    @Autowired
    private DasJedis dasJedis;
    @Autowired
    private MessageSource messageSource;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object doSomething2(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy) {
        String key = EtcConstant.getKey(EtcConstant.UserMoneyConstant._modifyMoneyRedisKey, carOwnerUserId);
        for (int i = 0; i < 3; i++) {
            Boolean setnxResult = dasJedis.setnx(key, String.valueOf(System.currentTimeMillis() + EtcConstant.UserMoneyConstant.timeout));
            if (setnxResult) {
                // 1.1获取锁成功
                // 2. 重置 分布锁 的过期时间
                dasJedis.expire(key, EtcConstant.UserMoneyConstant.timeout);
                // 3. 执行业务逻辑
                if (null != strategy) {
                    try {
                        Object obj = strategy.progress();
                        return obj;
                    } catch (Exception e) {
                        throw DasException.internalError(null, messageSource.getMessage("internal_error", Arrays.asList(ExceptionCode.INTERNAL_ERROR_THIRD_PARTY_ERROR ).toArray(), LocaleContextHolder.getLocale()));
                    } finally {
                        // 4. 释放锁
                        dasJedis.del(key);
                    }
                }
            } else {
                // 获取锁失败,尝试重新获取分布锁
                String lockValueA = dasJedis.get(key);
                if (StringUtils.isNotBlank(lockValueA) && System.currentTimeMillis() > Long.valueOf(lockValueA)) {
                    // 当前时间大于分布锁设置的超时时间,说明可以获取分布锁
                    String lockValueB = dasJedis.getset(key, String.valueOf(System.currentTimeMillis() + EtcConstant.UserMoneyConstant.timeout));
                    if (StringUtils.isBlank(lockValueB) || StringUtils.equals(lockValueA, lockValueB)) {
                        // B is null 说明在 get 和 getset 的过程中,没有进程修改closeOrderScheduledRedisKey已经过期, lock 是可以获取的。
                        // A == B 说明在get 和 getset 的过程中,没有进程修改closeOrderScheduledRedisKey 对应的 值 ,即 lock 是可以获取的。
                        dasJedis.expire(key, EtcConstant.UserMoneyConstant.timeout);
                        if (null != strategy) {
                            try {
                                Object obj = strategy.progress();
                                return obj;
                            } catch (Exception e) {
                                throw DasException.internalError(null, messageSource.getMessage("internal_error", Arrays.asList(ExceptionCode.INTERNAL_ERROR_THIRD_PARTY_ERROR ).toArray(), LocaleContextHolder.getLocale()));
                            } finally {
                                // 4. 释放锁
                                dasJedis.del(key);
                            }
                        }
                    }
                }
            }
            try {
                int nextInt = new Random().nextInt(1000 * 2);
                log.info("获取独占锁失败, 尝试{}毫秒后重试!", nextInt);
                TimeUnit.MILLISECONDS.sleep(nextInt);
            } catch (InterruptedException e) {
                throw DasException.getLockFail(e, e.getMessage());
            }
        }
        throw DasException.getLockFail(null, messageSource.getMessage(EtcConstant.MessageCode.update_failed, null, LocaleContextHolder.getLocale()));
    }


    @Override
    public Object doSomething3(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy) {
        throw new UnsupportedOperationException("doSomething3 connot operation");
    }

    @Override
    public Object doSomething4(Long carOwnerUserId, EtcConstant.UserMoneyConstant.ModifyMoneyProgress strategy) {
        // set key value [EX seconds] [PX milliseconds] [NX|XX]
        String key = EtcConstant.getRedisKey(EtcConstant.UserMoneyConstant._modifyMoneyRedisKey, carOwnerUserId);
        String tag_value = EtcConstant.getKey(System.currentTimeMillis(), carOwnerUserId);
        boolean setnxex = dasJedis.lock(key, tag_value, EtcConstant.UserMoneyConstant.expireTimeout);
        if (setnxex) {
            if (null != strategy) {
                try {
                    Object obj = strategy.progress();
                    return obj;
                } catch (Exception e) {
                    throw DasException.internalError(null, messageSource.getMessage("internal_error", Arrays.asList(ExceptionCode.INTERNAL_ERROR_THIRD_PARTY_ERROR ).toArray(), LocaleContextHolder.getLocale()));
                } finally {
                    // 4. 释放锁
                    dasJedis.unlock(key, tag_value);
                }
            }

        }
        throw DasException.getLockFail(null, messageSource.getMessage(EtcConstant.MessageCode.failed_to_get_lock_please_try_again_later, null, LocaleContextHolder.getLocale()));
    }
}

lock


    public boolean lock(String key, String value, int expiretime) {
        Jedis jedis = jedisPool.getResource();
        Long start = System.currentTimeMillis(), timeout = 1000 * 2L;
        try {
            for (; ; ) {
                //SET命令返回OK ,则证明获取锁成功
                String lock = jedis.set(key, value, "NX", "EX", expiretime);
                if ("OK".equals(lock)) {
                    return true;
                }
                //否则循环等待,在timeout时间内仍未获取到锁,则获取失败
                long l = System.currentTimeMillis() - start;
                if (l >= timeout) {
                    return false;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            jedis.close();
        }
    }

unlock

    public boolean unlock(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String script =
                "if redis.call('get',KEYS[1]) == ARGV[1] then" +
                        "   return redis.call('del',KEYS[1]) " +
                        "else" +
                        "   return 0 " +
                        "end";
        try {
            Object result = jedis.eval(script, Collections.singletonList(key),
                    Collections.singletonList(value));
            if ("1".equals(result.toString())) {
                return true;
            }
            return false;
        } finally {
            jedis.close();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值