自定义redis锁注解

项目中经常使用的redis锁写起来十分繁琐, 同样的代码需要编写多次. 于是想到使用注解方式来解决

0.1. 构思

  1. 由于使用注解不能确定返回值, 所以采用自定义异常的方式来解决
  2. redis锁一般需要指定过期时间, 考虑到特殊情况设置默认过期时间为永久

0.2. 启动类

1
2
3
4
5
6
7
8
9
// 开启AOP代理自动配置 
// exposeProxy = true 表示通过aop框架暴露该代理对象,aopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
public class SalesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SalesApplication.class, args);
    }
}

0.3. 自定义注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Classname:RedisLockException
 * Description:自定义redis异常
 * date:2019-12-09 15:49
 * author:yuyu
 */
public class RedisLockException extends RuntimeException {

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param message the detail message. The detail message is saved for
     *                later retrieval by the {@link #getMessage()} method.
     */
    public RedisLockException(String message) {
        super(message);
    }
}

``` 

## redis锁注解

/**

  • 警示: key 与 enumKey 二选一, 必须设置一个!!!

  • 警示: 如果 key 与 enumKey 都设置, 则使用key加锁

  • 警示: 如果redis加锁失败会抛出 RedisLockException 异常

  • 警示: 类内部方法调用不生效, 如果想生效请重新从redis中获取该对象再进行调用

  • 例: InvTransactionsSendServiceImpl thisBean = SpringContextUtil.getBean(InvTransactionsSendServiceImpl.class);

  • thisBean.sendINVTransactionLock(beginDate, endDate);

  • 例:

  • String msg;

  • try {

  • // testService.test(a) 该方法添加了 @RedisLock

  • msg = testService.test(a);

  • } catch (RedisLockException e) {

  • msg = e.getMessage();

  • } catch (Exception e) {

  • log.error(“方法异常”, e);

  • msg = “方法异常”;

  • }

  • return msg;

  • /
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface RedisLock {

    /**

    • redis 中的key

    • /
      String key() default SalesConstant.REDIS_LOCK_DEFAULT_KEY;

      /**

    • 枚举类redis key

    • /
      SalesConstant.RedisLockKey enumKey() default SalesConstant.RedisLockKey.DEFAULT_VALUE;

      /**

    • 秒,key过期时间,如果小于0则永久, 默认永久

    • /
      long expired() default -1;

}

1
2

## Aspect解析注解

package com.secoo.sales.common.annotation.redis;

import com.secoo.sales.common.constant.SalesConstant;
import com.secoo.sales.common.exception.RedisLockException;
import com.secooframework.redis.SecooRedisTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**

  • Classname:RedisLockAspect

  • Description:redis锁切面

  • date:2019-12-09 15:03

  • author:yuyu

  • /
    @Aspect
    public class RedisLockAspect {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SecooRedisTemplate secooRedisTemplate;

@Pointcut("@annotation(com.secoo.sales.common.annotation.redis.RedisLock)")
private void pointcut() {
}


/**
 * 环绕通知
 *
 * @param joinPoint 可用于执行切点的类
 * @throws Throwable
 */
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    RedisLock annotation = getMethod(joinPoint).getAnnotation(RedisLock.class);
    // 获取不是默认实现的key, 优先选择 key()
    String key = annotation.key();
    if (key.equals(SalesConstant.RedisLockKey.DEFAULT_VALUE.getCode())) {
        key = annotation.enumKey().getCode();
        if (key.equals(SalesConstant.RedisLockKey.DEFAULT_VALUE.getCode())) {
            // key() 与 enumKey() 必须实现一个, 如果两个都是默认实现则直接抛出异常
            logger.info("Redis加锁未指定key");
            throw new RedisLockException("Redis加锁未指定key!");
        }
    }

    // 加锁
    Boolean flag = secooRedisTemplate.setnx(key, "1");

    // 过期时间
    long expiredTime = annotation.expired();

    if (flag) {
        logger.info("redis加锁成功-key: " + key);
        // 如果过期时间大于0 , 则设置过期时间
        if (expiredTime > 0) {
            secooRedisTemplate.expire(key, expiredTime);
        }

        try {
            return (Object) joinPoint.proceed();
        } catch (Exception e) {
            throw e;
        } finally {
            // 删除锁
            secooRedisTemplate.del(key);
            logger.info("redis删除锁成功-key: " + key);
        }
    } else {
        logger.info("redis加锁失败-key: " + key);
        throw new RedisLockException("Redis加锁失败");
    }
}

private Method getMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
    Signature signature = joinPoint.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Method agentMethod = methodSignature.getMethod();
    return joinPoint.getTarget().getClass().getMethod(agentMethod.getName(), agentMethod.getParameterTypes());
}

}

1
2
3

## 注意事项
类内部调用无法触发切面代理, 会导致redis锁无效, 如果想使用需从spring中再次获取该对象并调用

@Service
public class Demo {
private Logger log = LoggerFactory.getLogger(this.getClass());

public void method1(){
    Demo thisBean = SpringContextUtil.getBean(Demo.class);
    try {
        thisBean.method2();
    } catch (RedisLockException e) {
        log.warn("redis加锁失败,返回值" + e.getMessage());
    } catch (Exception e) {
        log.error("操作异常", e);
    }
}

@RedisLock(enumKey = SalesConstant.RedisLockKey.MSG_INTEGRATION)
public void method2(){

}

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Redis自定义分布式,你可以使用注解来简化的使用。下面是一个示例: 首先,你可以创建一个自定义注解,例如`@DistributedLock`。这个注解可以用来修饰需要加的方法。 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface DistributedLock { String value() default ""; // 的key,默认为空 long expire() default 30000; // 的过期时间,默认为30秒 } ``` 接下来,你可以创建一个切面类,用于处理`@DistributedLock`注解。在该切面类中,使用Redis来实现分布式的功能。 ```java @Aspect @Component public class DistributedLockAspect { private final RedisTemplate<String, String> redisTemplate; public DistributedLockAspect(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @Around("@annotation(distributedLock)") public Object distributedLock(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable { String lockKey = distributedLock.value(); // 获取的key long expire = distributedLock.expire(); // 获取的过期时间 // 加操作 Boolean locked = redisTemplate.opsForValue().setIfAbsent(lockKey, "locked", expire, TimeUnit.MILLISECONDS); if (locked != null && locked) { try { // 执行方法体 return joinPoint.proceed(); } finally { // 释放 redisTemplate.delete(lockKey); } } else { throw new RuntimeException("Failed to acquire distributed lock."); } } } ``` 在上述代码中,我们使用`@Around`注解将切面绑定到所有使用`@DistributedLock`注解的方法上。在切面方法内部,首先获取的key和过期时间,然后尝试加。如果成功获取到,则执行方法体,并在方法执行完毕后释放。如果获取失败,则抛出异常。 最后,你可以在需要加的方法上使用`@DistributedLock`注解。 ```java @Service public class ExampleService { @Autowired private RedisTemplate<String, String> redisTemplate; @DistributedLock(value = "myLockKey", expire = 5000) public void exampleMethod() { // 需要加的方法体 } } ``` 上述示例中,`exampleMethod()`方法使用了`@DistributedLock`注解,并指定了的key为"myLockKey",过期时间为5秒。 这样,你就可以通过注解方式来简化Redis自定义分布式的使用了。请注意,在实际使用中,你需要配置好Redis连接和注入RedisTemplate实例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值