提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.idempotent.core.aop; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.exception.ServiceException; |
|
4 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
5 |
import com.iailab.framework.common.util.collection.CollectionUtils; |
|
6 |
import com.iailab.framework.idempotent.core.annotation.Idempotent; |
|
7 |
import com.iailab.framework.idempotent.core.keyresolver.IdempotentKeyResolver; |
|
8 |
import com.iailab.framework.idempotent.core.redis.IdempotentRedisDAO; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
import org.aspectj.lang.ProceedingJoinPoint; |
|
11 |
import org.aspectj.lang.annotation.Around; |
|
12 |
import org.aspectj.lang.annotation.Aspect; |
|
13 |
import org.springframework.util.Assert; |
|
14 |
|
|
15 |
import java.util.List; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
/** |
|
19 |
* 拦截声明了 {@link Idempotent} 注解的方法,实现幂等操作 |
|
20 |
* |
|
21 |
* @author iailab |
|
22 |
*/ |
|
23 |
@Aspect |
|
24 |
@Slf4j |
|
25 |
public class IdempotentAspect { |
|
26 |
|
|
27 |
/** |
|
28 |
* IdempotentKeyResolver 集合 |
|
29 |
*/ |
|
30 |
private final Map<Class<? extends IdempotentKeyResolver>, IdempotentKeyResolver> keyResolvers; |
|
31 |
|
|
32 |
private final IdempotentRedisDAO idempotentRedisDAO; |
|
33 |
|
|
34 |
public IdempotentAspect(List<IdempotentKeyResolver> keyResolvers, IdempotentRedisDAO idempotentRedisDAO) { |
|
35 |
this.keyResolvers = CollectionUtils.convertMap(keyResolvers, IdempotentKeyResolver::getClass); |
|
36 |
this.idempotentRedisDAO = idempotentRedisDAO; |
|
37 |
} |
|
38 |
|
|
39 |
@Around(value = "@annotation(idempotent)") |
|
40 |
public Object aroundPointCut(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable { |
|
41 |
// 获得 IdempotentKeyResolver |
|
42 |
IdempotentKeyResolver keyResolver = keyResolvers.get(idempotent.keyResolver()); |
|
43 |
Assert.notNull(keyResolver, "找不到对应的 IdempotentKeyResolver"); |
|
44 |
// 解析 Key |
|
45 |
String key = keyResolver.resolver(joinPoint, idempotent); |
|
46 |
|
|
47 |
// 1. 锁定 Key |
|
48 |
boolean success = idempotentRedisDAO.setIfAbsent(key, idempotent.timeout(), idempotent.timeUnit()); |
|
49 |
// 锁定失败,抛出异常 |
|
50 |
if (!success) { |
|
51 |
log.info("[aroundPointCut][方法({}) 参数({}) 存在重复请求]", joinPoint.getSignature().toString(), joinPoint.getArgs()); |
|
52 |
throw new ServiceException(GlobalErrorCodeConstants.REPEATED_REQUESTS.getCode(), idempotent.message()); |
|
53 |
} |
|
54 |
|
|
55 |
// 2. 执行逻辑 |
|
56 |
try { |
|
57 |
return joinPoint.proceed(); |
|
58 |
} catch (Throwable throwable) { |
|
59 |
// 3. 异常时,删除 Key |
|
60 |
// 参考美团 GTIS 思路:https://tech.meituan.com/2016/09/29/distributed-system-mutually-exclusive-idempotence-cerberus-gtis.html |
|
61 |
if (idempotent.deleteKeyWhenException()) { |
|
62 |
idempotentRedisDAO.delete(key); |
|
63 |
} |
|
64 |
throw throwable; |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
} |