問題描述
前段時間遇到個問題,自己內部系統調用出現重複請求導致數據混亂。
發生條件:接受到一個請求,該請求沒有執行完成又接受到相同請求,導致數據錯誤(如果是前一個請求執行完成,馬上又接受相同請求不會有問題)
問題分析:是由於數據庫的髒讀導致
問題解決思路
1.加一把大大的鎖(是最簡單的實現方式,但是性能堪憂,而且會阻塞請求)
2.實現請求攔截(可以共用,但是怎麼去實現卻是一個問題,怎麼用一個優雅的方式實現,並且方便復用)
3.修改實現(會對原有代碼做改動,存在風險,最主要的是不能共用)
最終實現方式
通過註解+spring AOP 的方式實現
使用
通過在任意方法上添加註解NotDuplicate
類1:
import static java.lang.annotation.ElementType.METHOD;import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface NotDuplicate {}類2:
import java.lang.reflect.Method;import java.util.Set;import java.util.concurrent.ConcurrentSkipListSet;import org.aspectj.lang.ProceedingJoinPoint;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.springframework.stereotype.Component;@Aspect@Componentpublic class NotDuplicateAop { private static final Set<String> KEY = new ConcurrentSkipListSet<>(); @Pointcut("@annotation(com.hhly.skeleton.base.filter.NotDuplicate)") public void duplicate() { } /** * 對方法攔截後進行參數驗證* @param pjp * @return * @throws Throwable */ @Around("duplicate()") public Object duplicate(ProceedingJoinPoint pjp) throws Throwable { MethodSignature msig = (MethodSignature) pjp.getSignature(); Method currentMethod = pjp.getTarget().getClass().getMethod(msig.getName(), msig.getParameterTypes()); //拼接簽名StringBuilder sb = new StringBuilder(currentMethod.toString()); Object[] args = pjp.getArgs(); for (Object object : args) { if(object != null){ sb.append(object.getClass().toString()); sb.append(object.toString()); } } String sign = sb.toString(); boolean success = KEY.add(sign); if(!success){ throw new ServiceRuntimeException("該方法正在執行,不能重複請求"); } try { return pjp.proceed(); } finally { KEY.remove(sign); } }}以上就是本次給大家講述的全部內容以及相關代碼,如果大家還有任何問題可以在下方的留言區討論,感謝大家對武林網的支持。