redis 判断存在性_springboot + redis + 注解 + 拦截器 实现接口幂等性校验
提醒:后面有些圖片模糊,請(qǐng)點(diǎn)擊原文查看清晰圖片
一、概念
冪等性, 通俗的說就是一個(gè)接口, 多次發(fā)起同一個(gè)請(qǐng)求, 必須保證操作只能執(zhí)行一次
比如:
訂單接口, 不能多次創(chuàng)建訂單
支付接口, 重復(fù)支付同一筆訂單只能扣一次錢
支付寶回調(diào)接口, 可能會(huì)多次回調(diào), 必須處理重復(fù)回調(diào)
普通表單提交接口, 因?yàn)榫W(wǎng)絡(luò)超時(shí)等原因多次點(diǎn)擊提交, 只能成功一次
等等
二、常見解決方案
唯一索引 -- 防止新增臟數(shù)據(jù)
token機(jī)制 -- 防止頁(yè)面重復(fù)提交
悲觀鎖 -- 獲取數(shù)據(jù)的時(shí)候加鎖(鎖表或鎖行)
樂觀鎖 -- 基于版本號(hào)version實(shí)現(xiàn), 在更新數(shù)據(jù)那一刻校驗(yàn)數(shù)據(jù)
分布式鎖 -- redis(jedis、redisson)或zookeeper實(shí)現(xiàn)
狀態(tài)機(jī) -- 狀態(tài)變更, 更新數(shù)據(jù)時(shí)判斷狀態(tài)
三、本文實(shí)現(xiàn)
本文采用第2種方式實(shí)現(xiàn), 即通過redis + token機(jī)制實(shí)現(xiàn)接口冪等性校驗(yàn)
四、實(shí)現(xiàn)思路
為需要保證冪等性的每一次請(qǐng)求創(chuàng)建一個(gè)唯一標(biāo)識(shí)token, 先獲取token, 并將此token存入redis, 請(qǐng)求接口時(shí), 將此token放到header或者作為請(qǐng)求參數(shù)請(qǐng)求接口, 后端接口判斷redis中是否存在此token:
如果存在, 正常處理業(yè)務(wù)邏輯, 并從redis中刪除此token, 那么, 如果是重復(fù)請(qǐng)求, 由于token已被刪除, 則不能通過校驗(yàn), 返回請(qǐng)勿重復(fù)操作提示
如果不存在, 說明參數(shù)不合法或者是重復(fù)請(qǐng)求, 返回提示即可
五、項(xiàng)目簡(jiǎn)介
springboot
redis
@ApiIdempotent注解 + 攔截器對(duì)請(qǐng)求進(jìn)行攔截
@ControllerAdvice全局異常處理
壓測(cè)工具: jmeter
說明:
本文重點(diǎn)介紹冪等性核心實(shí)現(xiàn), 關(guān)于springboot如何集成redis、ServerResponse、ResponseCode等細(xì)枝末節(jié)不在本文討論范圍之內(nèi), 有興趣的小伙伴可以查看我的Github項(xiàng)目: https://github.com/wangzaiplus/springboot/tree/wxw
六、代碼實(shí)現(xiàn)
pom
<dependency> <groupId>redis.clientsgroupId> <artifactId>jedisartifactId> <version>2.9.0version>dependency><dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.16.10version>dependency>JedisUtil
@Component@Slf4jpublic class JedisUtil { @Autowired private JedisPool jedisPool; private Jedis getJedis() { return jedisPool.getResource(); } /** * 設(shè)值 * * @param key * @param value * @return */ public String set(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); return jedis.set(key, value); } catch (Exception e) { log.error("set key:{} value:{} error", key, value, e); return null; } finally { close(jedis); } }? .....?}自定義注解@ApiIdempotent
/** * 在需要保證 接口冪等性 的Controller的方法上使用此注解 */@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface ApiIdempotent {}ApiIdempotentInterceptor攔截器
/** * 接口冪等性攔截器 */public class ApiIdempotentInterceptor implements HandlerInterceptor { @Autowired private TokenService tokenService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class); if (methodAnnotation != null) { check(request);// 冪等性校驗(yàn), 校驗(yàn)通過則放行, 校驗(yàn)失敗則拋出異常, 并通過統(tǒng)一異常處理返回友好提示 } return true; } private void check(HttpServletRequest request) { tokenService.checkToken(request); } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { }}TokenServiceImpl
@Servicepublic?class?TokenServiceImpl?implements?TokenService?{????private?static?final?String?TOKEN_NAME?=?"token"; @Autowired????private?JedisUtil?jedisUtil; @Override public ServerResponse createToken() { String str = RandomUtil.UUID32(); StrBuilder token = new StrBuilder();????????token.append(Constant.Redis.TOKEN_PREFIX).append(str);????????jedisUtil.set(token.toString(),?token.toString(),?Constant.Redis.EXPIRE_TIME_MINUTE); return ServerResponse.success(token.toString()); } @Override public void checkToken(HttpServletRequest request) { String token = request.getHeader(TOKEN_NAME); if (StringUtils.isBlank(token)) {// header中不存在token token = request.getParameter(TOKEN_NAME); if (StringUtils.isBlank(token)) {// parameter中也不存在token throw new ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg()); } } if (!jedisUtil.exists(token)) { throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());????????} Long del = jedisUtil.del(token); if (del <= 0) { throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg()); }????}}TestApplication
@SpringBootApplication@MapperScan("com.wangzaiplus.test.mapper")public?class?TestApplication??extends?WebMvcConfigurerAdapter?{ public static void main(String[] args) { SpringApplication.run(TestApplication.class, args);????} /** * 跨域 * @return */ @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource);????} @Override public void addInterceptors(InterceptorRegistry registry) { // 接口冪等性攔截器 registry.addInterceptor(apiIdempotentInterceptor()); super.addInterceptors(registry);????} @Bean public ApiIdempotentInterceptor apiIdempotentInterceptor() { return new ApiIdempotentInterceptor();????}}OK, 目前為止, 校驗(yàn)代碼準(zhǔn)備就緒, 接下來(lái)測(cè)試驗(yàn)證
七、測(cè)試驗(yàn)證
獲取token的控制器TokenController
@RestController@RequestMapping("/token")public class TokenController { @Autowired private TokenService tokenService; @GetMapping public ServerResponse token() { return tokenService.createToken();????}}TestController, 注意@ApiIdempotent注解, 在需要冪等性校驗(yàn)的方法上聲明此注解即可, 不需要校驗(yàn)的無(wú)影響
@RestController@RequestMapping("/test")@Slf4jpublic class TestController { @Autowired????private?TestService?testService; @ApiIdempotent @PostMapping("testIdempotence") public ServerResponse testIdempotence() { return testService.testIdempotence(); }}獲取token
查看redis
測(cè)試接口安全性: 利用jmeter測(cè)試工具模擬50個(gè)并發(fā)請(qǐng)求, 將上一步獲取到的token作為參數(shù)
header或參數(shù)均不傳token, 或者token值為空, 或者token值亂填, 均無(wú)法通過校驗(yàn), 如token值為"abcd"
八、注意點(diǎn)(非常重要)
上圖中, 不能單純的直接刪除token而不校驗(yàn)是否刪除成功, 會(huì)出現(xiàn)并發(fā)安全性問題, 因?yàn)? 有可能多個(gè)線程同時(shí)走到第46行, 此時(shí)token還未被刪除, 所以繼續(xù)往下執(zhí)行, 如果不校驗(yàn)jedisUtil.del(token)的刪除結(jié)果而直接放行, 那么還是會(huì)出現(xiàn)重復(fù)提交問題, 即使實(shí)際上只有一次真正的刪除操作, 下面重現(xiàn)一下
稍微修改一下代碼:
再次請(qǐng)求
再看看控制臺(tái)
雖然只有一個(gè)真正刪除掉token, 但由于沒有對(duì)刪除結(jié)果進(jìn)行校驗(yàn), 所以還是有并發(fā)問題, 因此, 必須校驗(yàn)
九、總結(jié)
其實(shí)思路很簡(jiǎn)單, 就是每次請(qǐng)求保證唯一性, 從而保證冪等性, 通過攔截器+注解, 就不用每次請(qǐng)求都寫重復(fù)代碼, 其實(shí)也可以利用spring aop實(shí)現(xiàn), 無(wú)所謂
如果小伙伴有什么疑問或者建議歡迎提出
Github
https://github.com/wangzaiplus/springboot/tree/wxw
作者:wangzaiplus
鏈接:https://www.jianshu.com/p/6189275403ed
【推薦閱讀】
數(shù)據(jù)庫(kù)之架構(gòu):主備+分庫(kù)?主從+讀寫分離?
(完)
(java思維導(dǎo)圖)
長(zhǎng)按關(guān)注,每天java一下,成就架構(gòu)師
總結(jié)
以上是生活随笔為你收集整理的redis 判断存在性_springboot + redis + 注解 + 拦截器 实现接口幂等性校验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php5.5升级到php5.6,从php
- 下一篇: node mysql 增删改查_Node