分布式锁实现限流

1.限流锁的应用场景

同一时间接口访问量巨大,如秒杀,需要进行限流。

2.实现思路

用 CURRENT_LIMIT_+ 类名+方法名作为redis的key, value作为访问秒杀接口的人数。
用redis的计数器统计访问人数,每新增一个访问请求,计数器+1,当人数超过上限,提示服务忙,秒杀接口处理完之后,计数器-1。

3.主要组成

Dcl:限流注解,自定义锁注解,然后给需要限流的方法加上此注解
DistributedCurrentLimit:限流接口
RedisDistributedCurrentLimit:限流实现类
DistributedCurrentLimitAspect:切面类【核心】

自定义锁注解,利用切面给所有加注解的方法加分布式锁进行限流。

4.关键代码分析:

DistributedCurrentLimitAspect:用redis计时器算人头,人头达到上限就返回服务正忙

@Around("@annotation(com.nascent.ecrp.mall.common.distribute.Dcl)")
    public Object distributedLock(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        if (request == null) {
            return proceedingJoinPoint.proceed();
        }
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        // 用CURRENT_LIMIT_+ 类名+方法名作为redis的key,统计调用此方法的用户数
        String lockKey = "CURRENT_LIMIT_" + method.getDeclaringClass().getName() + "." + method.getName();

        Dcl distributedKey = method.getAnnotation(Dcl.class);

        // 解析缓存key的后缀
        int keyIndex = distributedKey.keyIndex();
        String suffixKey = "";
        if (keyIndex > -1) {
            Object[] args = proceedingJoinPoint.getArgs();
            suffixKey += "_" + args[keyIndex];
        }
        lockKey += suffixKey;

        try {
        	// 每进来一个,redis的计数器+1
            Long incr = currentLimit.incr(lockKey);
            // 计数器数 > MAX_LIMIT,抛出异常,用户就不能继续访问接口了
            if (incr > MAX_LIMIT) {
                // currentLimit.decr(lockKey);
                throw new WmDefinitelyRuntimeException("前方道路拥挤,请稍后再试");
            }
            return proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            throw new WmDefinitelyRuntimeException(e);
        } finally {
            currentLimit.decr(lockKey);
        }
    }

3.全部代码

Dcl

/**
 * 限流
 */
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dcl {

    /**
     * 缓存key后缀参数所在位置
     *
     * @return 位置
     */
    int keyIndex();
}

DistributedCurrentLimit

/**
 * 分布式限流
 */
public interface DistributedCurrentLimit{

    /**
     * 队列加1
     */
    Long incr(String lockKey);

    /**
     * 队列减一
     */
    Long decr(String lockKey);

RedisDistributedCurrentLimit

/**
 * redis 分布式锁
 */
@SuppressWarnings({"UnusedReturnValue", "NullableProblems", "unused", "RedundantThrows"})
@Component
public class RedisDistributedCurrentLimit implements DistributedCurrentLimit {


    /**
     * 缓存
     */
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Long incr(String lockKey) {
        return redisTemplate.opsForValue().increment(lockKey);
    }

    @Override
    public Long decr(String lockKey) {
        return redisTemplate.opsForValue().decrement(lockKey);
    }

}

DistributedCurrentLimitAspect

/**
 * 用户操作锁Aspect
 */
@Aspect
@Component
@Order(-1)
public class DistributedCurrentLimitAspect {

    /**
     * 默认最大队列,暂时写死
     */
    private static final Long MAX_LIMIT = 50L;

    /**
     * 分布式锁
     */
    @Autowired
    private RedisDistributedCurrentLimit currentLimit;

    @Autowired
    private HttpServletRequest request;

    /**
     * @param proceedingJoinPoint proceedingJoinPoint
     */
    @Around("@annotation(com.nascent.ecrp.mall.common.distribute.Dcl)")
    public Object distributedLock(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        if (request == null) {
            return proceedingJoinPoint.proceed();
        }
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        String lockKey = "CURRENT_LIMIT_" + method.getDeclaringClass().getName() + "." + method.getName();

        Dcl distributedKey = method.getAnnotation(Dcl.class);

        // 解析缓存key的后缀
        int keyIndex = distributedKey.keyIndex();
        String suffixKey = "";
        if (keyIndex > -1) {
            Object[] args = proceedingJoinPoint.getArgs();
            suffixKey += "_" + args[keyIndex];
        }
        lockKey += suffixKey;

        try {
            Long incr = currentLimit.incr(lockKey);
            if (incr > MAX_LIMIT) {
                // currentLimit.decr(lockKey);
                throw new WmDefinitelyRuntimeException("前方道路拥挤,请稍后再试");
            }
            return proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            throw new WmDefinitelyRuntimeException(e);
        } finally {
            currentLimit.decr(lockKey);
        }
    }
}

秒杀接口添加锁注解

/**
     * 参与秒杀
     *
     * @return CommonResult
     */
    @Dul
    @Dcl(keyIndex = 0)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResult doSeckill(WmMarketingSeckillPostVo seckillPostVo) {
        String checkResult = checkGoodsSeckillInfo(seckillPostVo.getSeckillOrderInfoList(),seckillPostVo.getShopId());
		if(checkResult.equals(ErrorCode.SECKILL_ACTIVITY_UPDATE.getCode())){
			return new CommonResult().setFailed().setCode(ErrorCode.SECKILL_ACTIVITY_UPDATE.getCode()).setMsg("活动状态已变更,请重新下单");
		}
        List<JSONObject> seckillOrderList = new ArrayList<>();
        //下单校验
        List<SecKillBuyInfo> buyInfos = new ArrayList<>();
        SeckillOrderCheckRequest seckillOrderCheckRequest = new SeckillOrderCheckRequest();
        seckillOrderCheckRequest.setCustomerId(getCustomerId());
        seckillOrderCheckRequest.setShopId(seckillPostVo.getShopId());
        for(WmMarketingSeckillPostVo.SeckillOrderInfo seckillOrderInfo : seckillPostVo.getSeckillOrderInfoList()){
            SecKillBuyInfo secKillBuyInfo = new SecKillBuyInfo();
            secKillBuyInfo.setActivityId(seckillOrderInfo.getMarketingGuid());
            List<SeckillBuyGoodsInfo> seckillBuyGoodsInfos = new ArrayList<>();
            SeckillBuyGoodsInfo seckillBuyGoodsInfo = new SeckillBuyGoodsInfo();
            seckillBuyGoodsInfo.setGoodsId(seckillOrderInfo.getGoodsId());
            seckillBuyGoodsInfo.setGoodsLibId(seckillOrderInfo.getGoodsLibId());
            List<SeckillBuySkuInfo> seckillBuySkuInfos = new ArrayList<>();
            SeckillBuySkuInfo seckillBuySkuInfo = new SeckillBuySkuInfo();
            seckillBuySkuInfo.setGoodsSkuId(StringUtil.isBlank(seckillOrderInfo.getGoodsSkuId()) ? "0" : seckillOrderInfo.getGoodsSkuId());
            seckillBuySkuInfo.setBuyCount(seckillOrderInfo.getGoodsNum());
            seckillBuySkuInfos.add(seckillBuySkuInfo);
            seckillBuyGoodsInfo.setSkuInfos(seckillBuySkuInfos);
            seckillBuyGoodsInfos.add(seckillBuyGoodsInfo);
            secKillBuyInfo.setGoodsList(seckillBuyGoodsInfos);
            buyInfos.add(secKillBuyInfo);
        }
        seckillOrderCheckRequest.setBuyInfos(buyInfos);
        seckillOrderCheckRequest.setGroupId(getGroupId());
        SeckillOrderCheckResponse seckillOrderCheckResponse = OpenPlatformClient.exec(getGroupId(), seckillOrderCheckRequest);
        log.info("【调用中台下单校验接口响应结果】seckillOrderCheckResponse="+JSON.toJSONString(seckillOrderCheckResponse));
        if(!seckillOrderCheckResponse.success&&seckillOrderCheckResponse.getCode().equals("50402")){
            log.info("【秒杀下单已达限购上限】");
            return new CommonResult().setFailed().setCode("50402").setMsg("已达限购上限");
        }
        if(!seckillOrderCheckResponse.success&&seckillOrderCheckResponse.getCode().equals("50404")){
            log.info("【秒杀剩余库存不足】");
            return new CommonResult().setFailed().setCode("50404").setMsg("剩余库存不足");
        }
        if(!seckillOrderCheckResponse.success&&seckillOrderCheckResponse.getCode().equals("50005")){
            log.info("【秒杀活动已结束】");
            return new CommonResult().setFailed().setCode("50005").setMsg("活动已结束");
        }
        AssertUtil.assertTrue(seckillOrderCheckResponse.getSuccess()&&seckillOrderCheckResponse.getResult().isSeckillSuccess(),"秒杀活动校验失败");
        Map<String,ActivitySeckillCheckInfo> activityCheckInfoMap = new HashMap<>();
        for(ActivitySeckillCheckInfo activitySeckillCheckInfo : seckillOrderCheckResponse.getResult().getCheckInfos()){
            Long goodsId = activitySeckillCheckInfo.getItemInfos().get(0).getGoodsId();
            Long goodsLibId = activitySeckillCheckInfo.getItemInfos().get(0).getGoodsLibId();
            String skuId = activitySeckillCheckInfo.getItemInfos().get(0).getSkuInfos().get(0).getSkuId();//无sku也会返回到sku级,skuid为0
            activityCheckInfoMap.put(activitySeckillCheckInfo.getActivityId()+"_"+goodsLibId+"_"+goodsId+"_"+skuId,activitySeckillCheckInfo);
        }
        Map<String, ItemDetailsInfo> itemInfoMap = getGoodsInfos(seckillPostVo.getShopId(),seckillPostVo.getSeckillOrderInfoList());
        Integer expireMinute = seckillOrderCheckResponse.getResult().getExpireMinute();
        for(WmMarketingSeckillPostVo.SeckillOrderInfo seckillOrderInfo :seckillPostVo.getSeckillOrderInfoList()){
            ItemDetailsInfo itemInfo = itemInfoMap.get(seckillOrderInfo.getGoodsId()+"_"+seckillOrderInfo.getGoodsLibId());
            Long marketGuid = seckillOrderInfo.getMarketingGuid();
            Long goodsId = seckillOrderInfo.getGoodsId();
            Long goodsLibId = seckillOrderInfo.getGoodsLibId();
            String goodsSkuId = seckillOrderInfo.getGoodsSkuId()==null?"0":seckillOrderInfo.getGoodsSkuId();//无sku也会返回到sku级,skuid为0
            ActivitySeckillCheckInfo activitySeckillCheckInfo = activityCheckInfoMap.get(marketGuid+"_"+goodsLibId+"_"+goodsId+"_"+goodsSkuId);
            String activityName = activitySeckillCheckInfo.getActivityName();
            //秒杀价
            BigDecimal price = itemInfo.getPrice();
            for(ActivitySeckillCheckItemInfo activitySeckillCheckItemInfo : activitySeckillCheckInfo.getItemInfos()){
                if(activitySeckillCheckItemInfo.getGoodsId().longValue() == seckillOrderInfo.getGoodsId().longValue() && activitySeckillCheckItemInfo.getGoodsLibId().longValue() == seckillOrderInfo.getGoodsLibId().longValue()){
                    for(ActivitySeckillCheckSkuInfo seckillCheckSkuInfo : activitySeckillCheckItemInfo.getSkuInfos()){
                        if(seckillCheckSkuInfo.getSkuId().equals("0")||seckillCheckSkuInfo.getSkuId().equals(seckillOrderInfo.getGoodsSkuId())){
                            price = seckillCheckSkuInfo.getPrice();
                        }
                    }
                }
            }
            JSONObject orderJson = createOrderDetail(marketGuid,activityName,expireMinute,seckillOrderInfo.getGoodsNum(), goodsId, goodsSkuId, price, itemInfo);
            seckillOrderList.add(orderJson);
        }
        return new CommonResult(seckillOrderList);
    }
  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis是一个开源的高性能键值对存储数据库,它支持多种数据结构和功能,其中包括分布式锁。所以,如果需要在分布式环境中使用来保证数据的一致性和并发操作的正确性,那么学习和掌握Redis的分布式锁是很有必要的。 分布式锁是用来解决分布式系统中资源竞争、数据一致性和并发控制问题的一种机制。在分布式环境中,不同节点之间可能同时对同一个资源进行读写操作,这就需要使用分布式锁来保证资源的原子性和独占性,避免多个节点同时操作导致的数据不一致问题。 Redis的分布式锁通过使用SETNX命令来实现,即当某个节点获取到时,将一个特定的key设置为1,其他节点会发现该key已经存在而无法获取。当获取到后,节点需要在执行完操作后手动释放,以供其他节点获取。通过使用Redis的分布式锁,可以有效地实现资源的并发控制和数据的一致性。 学习和掌握Redis的分布式锁可以帮助我们解决分布式环境下的并发访问问题,确保系统的可靠性和性能。同时,Redis的分布式锁还可以用于实现一些常见的分布式算法,如分布式任务分配、分布式队列、分布式限流等,对于构建高可用、高性能的分布式系统具有重要作用。 因此,为了能够更好地应对分布式环境下的并发控制和数据一致性问题,以及利用Redis的分布式锁实现其他分布式算法,学习和掌握Redis的分布式锁是非常有必要的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值