spring boot 分布式锁 注解

DistributedLock.class
/**
 * @author zst
 * @description   分布式锁注解
 * @date 2023/6/3 0003 11:41
 * @mail 804633234@qq.com
 */
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {

    /**
     * 锁的名称,必填
     */
    String name();

    /**
     * 锁的持有时间,单位秒,默认 10 秒
     */
    long timeout() default 10000;




DistributedLockAspect.class
package com.chainlan.base.common.annotation.achieve;

import com.chainlan.base.common.annotation.DistributedLock;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zst
 * @description
 * @date 2023/6/3 0003 11:45
 * @mail 804633234@qq.com
 */

@Aspect
@Component
public class DistributedLockAspect {
    private static final Logger log = LoggerFactory.getLogger(DistributedLockAspect.class);
    @Resource
    private RedisTemplate redisTemplate;

    @Around("@annotation(distributedLock)")
    public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable {
        //锁名
        String lockName = distributedLock.name();
        //超时
        long timeout = distributedLock.timeout();
        log.info("************分布式注解锁***********************{},{}",lockName,timeout);
        Boolean reentrant= this.acquireLock(lockName,lockName,timeout);
        if (!reentrant) {
               throw new RuntimeException("稍等一会");
        }

        // 执行方法
        Object result = null;
        try {
            result = joinPoint.proceed();
        } finally {
            log.info("************分布式注解锁finally***********************{},{}",lockName,timeout);
            this.releaseLock(lockName,lockName);
        }
        return result;
    }
    //加锁
    public boolean acquireLock(String lockKey, String requestId, long expireTime) {
        Boolean result = redisTemplate.opsForValue().setIfAbsent("lock:"+lockKey, requestId, expireTime, TimeUnit.MILLISECONDS);
        return result != null && result;
    }
    //解锁
    public void releaseLock(String lockKey, String requestId) {
        Object currentValue = redisTemplate.opsForValue().get("lock:"+lockKey);
        if (currentValue != null && currentValue.toString().equals(requestId)) {
            redisTemplate.delete("lock:"+lockKey);
        }
    }
}

使用

    @DistributedLock(name="test")
    public AjaxResult test(@RequestBody  test)
    {

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值