欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

关于如何在项目接口保证幂等性的一点思考

发布时间:2025/3/17 编程问答 26 豆豆
生活随笔 收集整理的这篇文章主要介绍了 关于如何在项目接口保证幂等性的一点思考 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

什么是接口幂等?

接口幂等就是无论客户端调用服务端接口发起多少次请求,有且只有一次有效。

如何解决幂等问题呢?

1.暴露获取幂等token接口,且在此时存储redis、mysql、本地内存等(可根据具体业务场景选择token存储方式)

@Autowired private RedissonClient redissonClient;private String createToken(){return UUID.randomUUID().toString().replace("-",""); }@GetMapping("/getLdempotentToken")public Response<String> getLdempotentToken(){RMapCache<String,String> rMapCache = redissonClient.getMapCache(LdempotentAspect.LDEMPOTENT_TONE);String token = createToken();rMapCache.put(token,token);return Response.ok(token);} 复制代码

2.客户端在请求接口前先获取幂等接口,然后在请求接口前写入请求头中.

keyvalue
ldempotent_tokenba4b441e75f2449792fce5eb0ccfa2ab

3.利用spring aop技术代码需要处理幂等接口。在执行接口之前验证客户端请求头中的幂等token,验证成功则删除token,验证失败则直接返回错误信息.

@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Ldempotent {} 复制代码@Slf4j @Component @Aspect public class LdempotentAspect {public static final String LDEMPOTENT_TONE = "ldempotent_token";@Autowiredprivate RedissonClient redissonClient;@Pointcut("@annotation(com.fast.family.framework.core.redis.ldempotent.Ldempotent)")public void pointcut(){}@Around("pointcut()")public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {String token = Optional.ofNullable(WebUtils.getRequestHeader(LDEMPOTENT_TONE)).orElseThrow(() -> new LdempotentException(ResponseCode.LDEMPOTENT_ERROR.getCode(),ResponseCode.LDEMPOTENT_ERROR.getMsg()));RMapCache<String,String> rMapCache = redissonClient.getMapCache(LDEMPOTENT_TONE);Optional.ofNullable(rMapCache.get(token)).orElseThrow(() -> new LdempotentException(ResponseCode.LDEMPOTENT_ERROR.getCode(),ResponseCode.LDEMPOTENT_ERROR.getMsg()));rMapCache.remove(rMapCache.get(token));//token一次有效,所以在验证完后需删除return proceedingJoinPoint.proceed();}} 复制代码

那么按照上述步骤则可以保证接口幂等性(这种方式除了可以处理接口幂等,还能处理其他问题吗?哈哈哈哈哈哈)

总结

以上是生活随笔为你收集整理的关于如何在项目接口保证幂等性的一点思考的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。