点击蓝色“程序猿DD”关注我

回复“资源”获取独家整理的学习资料!

来源:http://tinyurl.com/y5k2sx5t

>>阿里云8月最新优惠,点击查看<<

一、概念

幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如:

  • 订单接口, 不能多次创建订单

  • 支付接口, 重复支付同一笔订单只能扣一次钱

  • 支付宝回调接口, 可能会多次回调, 必须处理重复回调

  • 普通表单提交接口, 因为网络超时等原因多次点击提交, 只能成功一次 等等

二、常见解决方案

  1. 唯一索引 -- 防止新增脏数据

  2. token机制 -- 防止页面重复提交

  3. 悲观锁 -- 获取数据的时候加锁(锁表或锁行)

  4. 乐观锁 -- 基于版本号version实现, 在更新数据那一刻校验数据

  5. 分布式锁 -- redis(jedis、redisson)或zookeeper实现

  6. 状态机 -- 状态变更, 更新数据时判断状态

三、本文实现

本文采用第2种方式实现, 即通过redis + token机制实现接口幂等性校验

四、实现思路

为需要保证幂等性的每一次请求创建一个唯一标识 token, 先获取 token, 并将此 token存入redis, 请求接口时, 将此 token放到header或者作为请求参数请求接口, 后端接口判断redis中是否存在此 token:

  • 如果存在, 正常处理业务逻辑, 并从redis中删除此 token, 那么, 如果是重复请求, 由于 token已被删除, 则不能通过校验, 返回 请勿重复操作提示

  • 如果不存在, 说明参数不合法或者是重复请求, 返回提示即可

五、项目简介

  • springboot

  • redis

  • @ApiIdempotent注解 + 拦截器对请求进行拦截

  • @ControllerAdvice全局异常处理

  • 压测工具: jmeter

说明:

  • 本文重点介绍幂等性核心实现, 关于springboot如何集成redisServerResponseResponseCode等细枝末节不在本文讨论范围之内, 有兴趣的小伙伴可以查看作者的Github项目: https://github.com/wangzaiplus/springboot/tree/wxw

六、代码实现

pom

        <!-- Redis-Jedis -->  <dependency>  <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>  <version>2.9.0</version>    </dependency> <!--lombok 本文用到@Slf4j注解, 也可不引用, 自定义log即可-->  <dependency>  <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId> <version>1.16.10</version>  </dependency>

JedisUtil

package com.wangzaiplus.test.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
@Slf4j
public class JedisUtil {    @Autowired private JedisPool jedisPool;    private Jedis getJedis() {  return jedisPool.getResource(); }   /** * 设值    *   * @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);   }   }   /** * 设值    *   * @param key   * @param value * @param expireTime 过期时间, 单位: s    * @return  */  public String set(String key, String value, int expireTime) {   Jedis jedis = null;    try {   jedis = getJedis();    return jedis.setex(key, expireTime, value); } catch (Exception e) { log.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e);  return null;    } finally { close(jedis);   }   }   /** * 取值    *   * @param key   * @return  */  public String get(String key) { Jedis jedis = null;    try {   jedis = getJedis();    return jedis.get(key);  } catch (Exception e) { log.error("get key:{} error", key, e);    return null;    } finally { close(jedis);   }   }   /** * 删除key *   * @param key   * @return  */  public Long del(String key) {   Jedis jedis = null;    try {   jedis = getJedis();    return jedis.del(key.getBytes());   } catch (Exception e) { log.error("del key:{} error", key, e);    return null;    } finally { close(jedis);   }   }   /** * 判断key是否存在 *   * @param key   * @return  */  public Boolean exists(String key) { Jedis jedis = null;    try {   jedis = getJedis();    return jedis.exists(key.getBytes());    } catch (Exception e) { log.error("exists key:{} error", key, e); return null;    } finally { close(jedis);   }   }   /** * 设值key过期时间 *   * @param key   * @param expireTime 过期时间, 单位: s    * @return  */  public Long expire(String key, int expireTime) {    Jedis jedis = null;    try {   jedis = getJedis();    return jedis.expire(key.getBytes(), expireTime);    } catch (Exception e) { log.error("expire key:{} error", key, e); return null;    } finally { close(jedis);   }   }   /** * 获取剩余时间    *   * @param key   * @return  */  public Long ttl(String key) {   Jedis jedis = null;    try {   jedis = getJedis();    return jedis.ttl(key);  } catch (Exception e) { log.error("ttl key:{} error", key, e);    return null;    } finally { close(jedis);   }   }   private void close(Jedis jedis) {   if (null != jedis) {   jedis.close();  }   }
}

自定义注解 @ApiIdempotent

package com.wangzaiplus.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** * 在需要保证 接口幂等性 的Controller的方法上使用此注解  */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiIdempotent {
}
  1. ApiIdempotentInterceptor拦截器

package com.wangzaiplus.test.interceptor;
import com.wangzaiplus.test.annotation.ApiIdempotent;
import com.wangzaiplus.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
/** * 接口幂等性拦截器  */
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);// 幂等性校验, 校验通过则放行, 校验失败则抛出异常, 并通过统一异常处理返回友好提示    }   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

package com.wangzaiplus.test.service.impl;
import com.wangzaiplus.test.common.Constant;
import com.wangzaiplus.test.common.ResponseCode;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.exception.ServiceException;
import com.wangzaiplus.test.service.TokenService;
import com.wangzaiplus.test.util.JedisUtil;
import com.wangzaiplus.test.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
@Service
public 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

package com.wangzaiplus.test;
import com.wangzaiplus.test.interceptor.ApiIdempotentInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@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, 目前为止, 校验代码准备就绪, 接下来测试验证

七、测试验证

获取 token的控制器 TokenController

package com.wangzaiplus.test.controller;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/token")
public class TokenController {  @Autowired private TokenService tokenService;  @GetMapping    public ServerResponse token() { return tokenService.createToken();  }
}

TestController, 注意 @ApiIdempotent注解, 在需要幂等性校验的方法上声明此注解即可, 不需要校验的无影响

package com.wangzaiplus.test.controller;
import com.wangzaiplus.test.annotation.ApiIdempotent;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {   @Autowired private TestService testService;    @ApiIdempotent @PostMapping("testIdempotence")  public ServerResponse testIdempotence() {   return testService.testIdempotence();   }
}

获取 token

查看redis

测试接口安全性: 利用jmeter测试工具模拟50个并发请求, 将上一步获取到的token作为参数

header或参数均不传token, 或者token值为空, 或者token值乱填, 均无法通过校验, 如token值为"abcd"

八、注意点(非常重要)

上图中, 不能单纯的直接删除token而不校验是否删除成功, 会出现并发安全性问题, 因为, 有可能多个线程同时走到第46行, 此时token还未被删除, 所以继续往下执行, 如果不校验 jedisUtil.del(token)的删除结果而直接放行, 那么还是会出现重复提交问题, 即使实际上只有一次真正的删除操作, 下面重现一下

稍微修改一下代码:

再次请求

再看看控制台

虽然只有一个真正删除掉token, 但由于没有对删除结果进行校验, 所以还是有并发问题, 因此, 必须校验

九、总结

其实思路很简单, 就是每次请求保证唯一性, 从而保证幂等性, 通过拦截器+注解, 就不用每次请求都写重复代码, 其实也可以利用spring aop实现。

留言交流不过瘾?添加微信:zyc_enjoy

根据指引加入各种主题讨论群

每日一问

今日问题

(留言说说你的答案吧,明日推文公布答案)

昨日答案:C

这道应该怎么解析呢?直接在脑海里模拟吧,不然就找一张草稿红画一画,能很快得出答案。这道题中被拼合的图形还可以翻转,其实这类拼合类的图形推理题,个人感觉大多数都规定被拼合的图形只能上下左右平移,不能有其它行为。

(昨日问题可在昨日推文的文末查看)

推荐阅读

  • 百万年薪挖了个P8程序员,难道是“水货”?

  • 你必须收藏的Github技巧

  • 开发部署提速8倍!这款IDE插件了解一下?

  • 攻破MySQL性能瓶颈必知的调优技巧

  • 如何模拟将CPU、IO打满?

来星球聊聊技术人的斜杠生活

点一点“阅读原文”小惊喜在等你

Spring Boot + Redis 实现接口幂等性 | 分布式开发必知!相关推荐

  1. 太好了 | 这篇写的太好了!Spring Boot + Redis 实现接口幂等性

    Hi ! 我是小小,今天是本周的第四篇,第四篇主要内容是 Spring Boot + Redis 实现接口幂等性 介绍 幂等性的概念是,任意多次执行所产生的影响都与一次执行产生的影响相同,按照这个含义 ...

  2. Sprinig Boot + Redis 实现接口幂等性,写得太好了!

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:收藏了!7 个开源的 Spring Boot 前后端分离优质项目个人原创+1博客:点击前往,查看更多 作者:wa ...

  3. 【SpringBoot应用篇】SpringBoot+Redis实现接口幂等性校验

    [SpringBoot应用篇]SpringBoot+Redis实现接口幂等性校验 幂等性 解决方法 Pom token令牌 yml @ApiIdempotentAnn ApiIdempotentInt ...

  4. spring boot 搭建的一个企业级快速开发脚手架

    源码地址 https://github.com/javanan/slife slife spring boot 搭建的一个企业级快速开发脚手架. 技术栈 Spring Boot MySQL Freem ...

  5. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目

    项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...

  6. java+cache使用方法_java相关:Spring boot redis cache的key的使用方法

    java相关:Spring boot redis cache的key的使用方法 发布于 2020-8-16| 复制链接 摘记: 在数据库查询中我们往往会使用增加缓存来提高程序的性能,@Cacheabl ...

  7. Spring Boot Redis关闭

    Spring Boot Redis 在开发或者本地没有redis数据库时,控制台会一直报连接超时的日志,可以通过配置取消: spring: data:redis:repositories:enable ...

  8. redis实现接口幂等性

    redis实现接口幂等性 1. 说明 幂等性的概念:任意多次执行所产生得影响均与一次执行的影响相同,对数据库的影响只能是一次性的,不能重复处理.在实际项目中,在客户端没限制幂等性,重复调用接口,导致接 ...

  9. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台

    前言 项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于Layui的后台管理系统模板,扩展Layui原生UI样 ...

最新文章

  1. 半斤八两中级破解 (四) TCP_UDP协议转向本地验证
  2. 校园网服务器系统需求分析,校园网的网络系统集成建设需求分析的主要工作
  3. 创建dynamics CRM client-side (四) - Namespace Notation in JS
  4. 字符串在Python内部是如何省内存的
  5. linux挂载iso文件
  6. NOIP2020 赛前总结
  7. INNODB表快速迁移
  8. Laravel 学习路线【4】控制器
  9. 压缩感知及应用 源代码_信言动态|学院成功举办2019年机器学习与压缩感知理论及其应用研讨会...
  10. mysql left join 耗时_性能调优:mysql之left join
  11. Mschart应用。
  12. 求100以内的素数,全部打印出来
  13. 用Python画二元高次方程
  14. AutoGluon处理多模态数据方法及案例——Multimodal Data Tables: Tabular, Text, and Image
  15. 【生活常识】照片的尺寸
  16. 淘宝号搜索标签查询,买家标签查询、人群标签查询、淘宝号搜索打标接口、买家标签查询接口、人群标签查询接口
  17. 定时器的几种实现方案
  18. k8s删除node节点
  19. 对话淘宝无线王五洲:移动电商将改变许多现有的商业规则
  20. 正则表达式匹配字符串中的任何空格

热门文章

  1. linux c 延迟函数 sleep usleep 使用区别
  2. golang 面向对象编程
  3. linux 代码格式化工具 clang-format
  4. linux查看网卡速率
  5. golang 并发与并行学习笔记(二)
  6. 比一比Nmap、Zmap、Masscan三种扫描工具
  7. 程序(进程)内存分布 解析
  8. Android JUnit测试说明和实例演示
  9. INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES错误解决方法
  10. FreeNas安装、初始化和存储池设置