在上周發(fā)布的 TienChin 項(xiàng)目視頻中,我和大家一共梳理了六種冪等性解決方案,接口冪等性處理算是一個(gè)非常常見(jiàn)的需求了,我們?cè)诤芏囗?xiàng)目中其實(shí)都會(huì)遇到。今天我們來(lái)看看兩種比較簡(jiǎn)單的實(shí)現(xiàn)思路。
1. 接口冪等性實(shí)現(xiàn)方案梳理
其實(shí)接口冪等性的實(shí)現(xiàn)方案還是蠻多的,我這里和小伙伴們分享兩種比較常見(jiàn)的方案。
1.1 基于 Token
基于 Token 這種方案的實(shí)現(xiàn)思路很簡(jiǎn)單,整個(gè)流程分兩步:
大致的思路就是上面這樣,當(dāng)然具體的實(shí)現(xiàn)則會(huì)復(fù)雜很多,有很多細(xì)節(jié)需要注意,松哥之前也專門(mén)錄過(guò)這種方案的視頻,小伙伴們可以參考下,錄了兩個(gè)視頻,一個(gè)是基于攔截器處理的,還有一個(gè)是基于 AOP 切面處理的:
基于攔截器處理(視頻一):
基于 AOP 切面處理(視頻二):
1.2 基于請(qǐng)求參數(shù)校驗(yàn)
最近在 TienChin 項(xiàng)目中使用的是另外一種方案,這種方案是基于請(qǐng)求參數(shù)來(lái)判斷的,如果在短時(shí)間內(nèi),同一個(gè)接口接收到的請(qǐng)求參數(shù)相同,那么就認(rèn)為這是重復(fù)的請(qǐng)求,拒絕處理,大致上就是這么個(gè)思路。
相比于第一種方案,第二種方案相對(duì)來(lái)說(shuō)省事一些,因?yàn)橹挥幸淮握?qǐng)求,不需要專門(mén)去服務(wù)端拿令牌。在高并發(fā)環(huán)境下這種方案優(yōu)勢(shì)比較明顯。
所以今天我就來(lái)和大家聊聊第二種方案的實(shí)現(xiàn),后面在 TienChin 項(xiàng)目視頻中也會(huì)和大家細(xì)講。
2. 基于請(qǐng)求參數(shù)的校驗(yàn)
首先我們新建一個(gè) Spring Boot 項(xiàng)目,引入 Web 和 Redis 依賴,新建完成后,先來(lái)配置一下 Redis 的基本信息,如下:
spring.redis.host=localhostspring.redis.port=6379spring.redis.password=123
為了后續(xù) Redis 操作方便,我們?cè)賮?lái)對(duì) Redis 進(jìn)行一個(gè)簡(jiǎn)單封裝,如下:
@Componentpublic class RedisCache { @Autowired public RedisTemplate redisTemplate; public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { redisTemplate.opsForValue().set(key, value, timeout, timeUnit); } public T getCacheObject(final String key) { ValueOperations operation = redisTemplate.opsForValue(); return operation.get(key); }}
這個(gè)比較簡(jiǎn)單,一個(gè)存數(shù)據(jù),一個(gè)讀數(shù)據(jù)。
接下來(lái)我們自定義一個(gè)注解,在需要進(jìn)行冪等性處理的接口上,添加該注解即可,將來(lái)這個(gè)接口就會(huì)自動(dòng)的進(jìn)行冪等性處理。
@Inherited@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RepeatSubmit { /** * 間隔時(shí)間(ms),小于此時(shí)間視為重復(fù)提交 */ public int interval() default 5000; /** * 提示消息 */ public String message() default “不允許重復(fù)提交,請(qǐng)稍候再試”;}
這個(gè)注解我們通過(guò)攔截器來(lái)進(jìn)行解析,解析代碼如下:
public abstract class RepeatSubmitInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { Map map = new HashMap(); map.put(“status”, 500); map.put(“msg”, annotation.message()); response.setContentType(“application/json;charset=utf-8”); response.getWriter().write(new ObjectMapper().writeValueAsString(map)); return false; } } return true; } else { return true; } } /** * 驗(yàn)證是否重復(fù)提交由子類(lèi)實(shí)現(xiàn)具體的防重復(fù)提交的規(guī)則 * * @param request * @return * @throws Exception */ public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation);}
這個(gè)攔截器是一個(gè)抽象類(lèi),將接口方法攔截下來(lái),然后找到接口上的 @RepeatSubmit 注解,調(diào)用 isRepeatSubmit 方法去判斷是否是重復(fù)提交的數(shù)據(jù),該方法在這里是一個(gè)抽象方法,我們需要再定義一個(gè)類(lèi)繼承自這個(gè)抽象類(lèi),在新的子類(lèi)中,可以有不同的冪等性判斷邏輯,這里我們就是根據(jù) URL 地址+參數(shù) 來(lái)判斷冪等性條件是否滿足:
@Componentpublic class SameUrlDataInterceptor extends RepeatSubmitInterceptor { public final String REPEAT_PARAMS = “repeatParams”; public final String REPEAT_TIME = “repeatTime”; public final static String REPEAT_SUBMIT_KEY = “REPEAT_SUBMIT_KEY”; private String header = “Authorization”; @Autowired private RedisCache redisCache; @SuppressWarnings(“unchecked”) @Override public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation) { String nowParams = “”; if (request instanceof RepeatedlyRequestWrapper) { RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request; try { nowParams = repeatedlyRequest.getReader().readLine(); } catch (IOException e) { e.printStackTrace(); } } // body參數(shù)為空,獲取Parameter的數(shù)據(jù) if (StringUtils.isEmpty(nowParams)) { try { nowParams = new ObjectMapper().writeValueAsString(request.getParameterMap()); } catch (JsonProcessingException e) { e.printStackTrace(); } } Map nowDataMap = new HashMap(); nowDataMap.put(REPEAT_PARAMS, nowParams); nowDataMap.put(REPEAT_TIME, System.currentTimeMillis()); // 請(qǐng)求地址(作為存放cache的key值) String url = request.getRequestURI(); // 唯一值(沒(méi)有消息頭則使用請(qǐng)求地址) String submitKey = request.getHeader(header); // 唯一標(biāo)識(shí)(指定key + url + 消息頭) String cacheRepeatKey = REPEAT_SUBMIT_KEY + url + submitKey; Object sessionObj = redisCache.getCacheObject(cacheRepeatKey); if (sessionObj != null) { Map sessionMap = (Map) sessionObj; if (compareParams(nowDataMap, sessionMap) && compareTime(nowDataMap, sessionMap, annotation.interval())) { return true; } } redisCache.setCacheObject(cacheRepeatKey, nowDataMap, annotation.interval(), TimeUnit.MILLISECONDS); return false; } /** * 判斷參數(shù)是否相同 */ private boolean compareParams(Map nowMap, Map preMap) { String nowParams = (String) nowMap.get(REPEAT_PARAMS); String preParams = (String) preMap.get(REPEAT_PARAMS); return nowParams.equals(preParams); } /** * 判斷兩次間隔時(shí)間 */ private boolean compareTime(Map nowMap, Map preMap, int interval) { long time1 = (Long) nowMap.get(REPEAT_TIME); long time2 = (Long) preMap.get(REPEAT_TIME); if ((time1 – time2) < interval) { return true; } return false; }}
我們來(lái)看下具體的實(shí)現(xiàn)邏輯:
好啦,做完這一切,最后我們?cè)賮?lái)配置一下攔截器即可:
@Configurationpublic class WebConfig implements WebMvcConfigurer { @Autowired RepeatSubmitInterceptor repeatSubmitInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(repeatSubmitInterceptor) .addPathPatterns(“/**”); }}
如此,我們的接口冪等性就處理好啦 在需要的時(shí)候,就可以直接在接口上使用啦:
@RestControllerpublic class HelloController { @PostMapping(“/hello”) @RepeatSubmit(interval = 100000) public String hello(@RequestBody String msg) { System.out.println(“msg = ” + msg); return “hello”; }}
好啦,公眾號(hào)后臺(tái)回復(fù) RepeatSubmit 可以下載本文源碼哦。