redis分布式秒杀锁

-- 获取锁标识,是否与当前线程一致?
if(redis.call('get', KEYS[1]) == ARGV[1]) then
    -- 一致,删除
    return redis.call('del', KEYS[1])
end
-- 不一致,直接返回
return 0

redis分布式秒杀锁_第1张图片

package com.platform.lock;

public interface ILock {

    /**
     * 获取锁
     * @param timeoutSec
     * @return
     */
    public boolean tryLock(long timeoutSec);

    /**
     * 锁标识、释放锁
     */
    public void unlock();
}

redis分布式秒杀锁_第2张图片

package com.platform.lock;

import cn.hutool.core.lang.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * redis的分布式锁
 * 实现ILock接口
 */
public class SimpleRedisLock implements ILock {

    // 不同的业务有不同的锁名称
    private String name;

    private StringRedisTemplate stringRedisTemplate;
    private static final String KEY_PREFIX = "tryLock:";
    private static final String ID_PREFIX = UUID.randomUUID().toString(true) + "-";
    // DefaultRedisScript,
    private static final DefaultRedisScript UNLOCK_SCRIPT;

    public SimpleRedisLock(String name, StringRedisTemplate stringRedisTemplate) {
        this.name = name;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    // 初始化 UNLOCK_SCRIPT,用静态代码块的方式,一加载SimpleRedisLock有会加载unlock.lua
    // 避免每次调unLock() 才去加载,提升性能!!!
    static {
        UNLOCK_SCRIPT = new DefaultRedisScript<>();
        // setLocation() 设置脚本位置
        UNLOCK_SCRIPT.setLocation(new ClassPathResource("unlock.lua"));
        // 返回值类型
        UNLOCK_SCRIPT.setResultType(Long.class);
    }

    /**
     * 获取锁
     */
    @Override
    public boolean tryLock(long timeoutSec) {
        // 获取线程标示
        String threadId = ID_PREFIX + Thread.currentThread().getId();
        // 获取锁
        // set lock thread1 nx ex 10
        // nx : setIfAbsent(如果不存在) , ex : timeoutSec(秒)
        Boolean success = stringRedisTemplate.opsForValue()
                .setIfAbsent(KEY_PREFIX + name, threadId, timeoutSec, TimeUnit.SECONDS);
        // 自动拆箱(Boolean -> boolean)!!!可能有风险
        return Boolean.TRUE.equals(success);
    }

    /**
     * 解决判断(锁标识、释放锁)这两个动作,之间产生阻塞!!!
     * JVM的 FULL GC
     * 要让这两个动作具有原子性
     */
    @Override
    public void unlock() {
        // 调用lua脚本
        stringRedisTemplate.execute(
                UNLOCK_SCRIPT,
                Collections.singletonList(KEY_PREFIX + name),
                ID_PREFIX + Thread.currentThread().getId());
    }
}

redis分布式秒杀锁_第3张图片

    @PostMapping("/cancelPublicBenefit")
    @CheckRepeatCommit
    public RestResponse cancelPublicBenefit(@LoginUser MallUserEntity loginUser, @RequestBody MallPublicBenefitEntity publicBenefitEntity) {

        String key = PUBLIC_BENEFIT_TEAM_LOCK_FLAG + publicBenefitEntity.getId();
        RestResponse restResponse = null;

        SimpleRedisLock simpleRedisLock = new SimpleRedisLock(key,stringRedisTemplate);
        try {
            if (simpleRedisLock.tryLock(15)) {
                // 成功获取锁,执行业务逻辑
                restResponse = mallPublicBenefitService.cancelPublicBenefit(loginUser, publicBenefitEntity);
            } else {
                // 获取锁失败,处理失败逻辑
                throw new BusinessException("服务器繁忙!");
            }
        } finally {
            simpleRedisLock.unlock();
        }

        return restResponse;
    }

你可能感兴趣的:(redis,分布式,数据库)