一、案例需求
在一个SpringBoot项目,其中定时任务和web端代码耦合在一起,并部署在多个服务器节点上,但这样会存在一个问题,每个节点都会触发一次定时任务,这显然重复执行了。为了达到唯一控制效果,引入了分布式锁的功能。但是有个缺点是每写一个定时任务都得写一份与业务无关的分布式锁代码,这样造成大量的冗余代码。所以想着通过注解+切面的方式来实现代码的精简。
二、代码
1.注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableDistributedLock {
String key();
int expTime() default 30;
}
分布式锁用的是redis实现的,所以注解简单定义了key和过期时间。
2.AOP
@Aspect
@Component
public class DistributedLockAspect {
private final static Logger logger = LoggerFactory.getLogger(DistributedLockAspect.class);
@Resource(name = "exclusiveLock")
private DistributedLock exclusiveLock;
@Pointcut("@annotation(EnableDistributedLock)")
public void distributedLockPointCut() {
}
@Around("distributedLockPointCut()")
public void aroud(ProceedingJoinPoint joinPoint) throws Throwable {


6692

被折叠的 条评论
为什么被折叠?



