当前位置:网站首页>Interface non idempotent solution
Interface non idempotent solution
2022-04-21 19:58:00 【Wow, big mouth is so handsome (I love big mouth)】
Interface non idempotent solution
Project code :https://github.com/DaZuiZui/Interface-idempotency
1. What is idempotence
First, let's think about what idempotency is , There is no side effect of multiple calls of an interface, which is idempotent , If an interface is called many times, the side effect is not idempotent , There is the problem of idempotency
2. Solution
2.1 Distributed lock +token

We guarantee idempotency by using redis+token Realized , First, we get... From the server token, Then bring... When requested token For the request , If token stay redis If you exist in the world token Delete , If it does not exist, do not operate .
3.code
3.1 Distributed lock +token
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Controller layer
@RestController
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/getToken")
public String getToken(){
String token = UUID.randomUUID().toString().substring(0,8);
redisTemplate.opsForValue().set(token,"1",60*10, TimeUnit.SECONDS);
return token;
}
@GetMapping("/sub")
public void sub(String token) throws InterruptedException {
//todo Business operations
}
}
Aop layer
@Aspect
@Component
public class TestAop {
@Autowired
private RedisTemplate redisTemplate;
@Before("execution(* com.dazuizui.idempotentinterface.controller.TestController.sub(..))")
public void befor(JoinPoint proceedingJoinPoint) throws Exception {
Object[] args = proceedingJoinPoint.getArgs();
boolean b = redisTemplate.delete(args[0]);
System.out.println(b);
if (!b){
//todo Log operations
throw new Exception(" Idempotent operation ");
}
}
}
版权声明
本文为[Wow, big mouth is so handsome (I love big mouth)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211952243612.html
边栏推荐
- 【时序】LSTNet:结合 CNN、RNN 以及 AR 的时间序列预测模型
- 80.(leaflet篇)leaflet调用geoserver发布的postgis数据图层
- 端口被占用时解决方法
- Source Insight配置及问题汇总
- 什么是注解
- 一加宣布未来产品将入驻 OPPO 门店,还将首发集团新资源
- JS monitor mobile phone screen rotation (horizontal screen or vertical screen)
- Building / Qt 5 and QT 6 compatibility using cmake
- Experience and guidance of seniors preparing for the postgraduate entrance examination of labor health and environmental hygiene of Southeast University in 2023
- MFC CComboBox 使用例子
猜你喜欢

SVPWM模块为什么会出现扇区判断错误?

fiddler抓包PC微信小程序失败的解决方案

Enterprise cross-border e-commerce platform service solution, cross-border e-commerce trade business framework construction, operation and maintenance

Mysql错误2005

High performance distributed cache redis - redis infrastructure and cache principle - continuous update

DolphinDB VSCode 插件使用教程

快速排序的三种实现方式

【Gradle】问题解析+下载安装+环境配置+验证安装

码出高效 第七章 并发与多线程

Solution of "unable to load and save data" in Eldon law ring
随机推荐
824. Goat Latin
Introduction to OpenCL of opencv
Create thread pool manually
thread
一加宣布未来产品将入驻 OPPO 门店,还将首发集团新资源
Comprehensive solution for digital construction of double prevention mechanism in hazardous chemical enterprises
小程序项目文件介绍
Modifying the root password of MySQL
多因子策略
HW-新员工考试-遍历
MFC CComboBox 使用例子
redis
第九届”大唐杯“全国大学生移动通信5G技术大赛省赛获奖名单公示
PostgreSql postgres_fdw
Use of complex function in MATLAB
vtkjs介绍
nodejs笔记1
杰理之VDDIO_SYSVDD_DCDC14系统电压配置说明【篇】
我的“中医”就医经历
VIM from dislike to dependence (6) -- insertion mode