当前位置:网站首页>幂等性实践操作,基于业务讲解幂等性
幂等性实践操作,基于业务讲解幂等性
2022-04-23 03:15:00 【小道仙97】
之前面试也提到过关于幂等的问题,自己百度查了关于它的概念倒是很简单,但就一直不是很理解,今天和人讨论一下,然后再自己结合实际理解了一下。
-
幂等概念来自数学,表示N次变换和1次变换的结果是相同的。这里讨论在某些场景下,客户端在调用服务没有达到预期结果时,会进行多次调用,为避免多次重复的调用对服务资源产生副作用,服务提供者会承诺满足幂等。
-
HTTP/1.1中对幂等性的定义是:一次和多次请求某一个资源对于资源本身应该具有同样的副作用(网络超时等问题除外)。也就是说,其任意多次执行对资源本身所产生的影响均与一次执行的影响相同。
-
简单来说就是:多次调用同一个接口,返回的结果是同样的。
其实我们在实际代码中可能已经做过了幂等但是因为不知道这个概念导致我们不清楚(其实这样的情况还挺多)
单说概念可能大家并不是很理解,下面举两个例子,一个是我同事给我讲的,一个是我之前的实际例子。
一、
在工单系统里面有一个接单的操作,一个工单可以派发给多个人,每个人都可以接单,规则是谁先接到就算谁的。
如果我们不对其进行幂等处理那就会发生第二个人覆盖了第一个人的接单。
错误操作如下:
工单下派给A、B两个人,然后A、B同时进入接单界面,这时候A接单了,但是B来电话了,他去接电话,接完电话他还停留在接单界面,然后他也接单了。
改造如下:
- 每次进来先获取Redis里面的 key,判断是否存在,如果不存在就继续往下走
- 判断当前工单的状态,是否等于待接收,如果是继续往下走
- 使用工单id作为 key 存入Redis里面去(分布式锁实现)
- 执行一系列的业务逻辑
- 删除Redis里面的key
伪代码实现:
public String fun(String id) {
if (RedisUtils.get(id) != null) {
return "该工单已被接收";
}
RedisUtils.put(id,id);
if (testDao.getStateById(id) != 3) {
return "该工单已被接收";
}
// 执行具体的业务逻辑
//......
if (RedisUtils.get(id) != null) {
RedisUtils.delete(id);
}
return "操作成功";
}
二、
这是一个典型的支付场景:用户支付下单场景,理论上一个订单用户只能支付一次,但是如果不做幂等处理就可能支付多次。
错误操作如下:
因为网络或者各种原因卡住了,用户刷新页面再次点击支付,这样会造成支付两次的情况
改造如下:
- 每次点击支付后锁住按钮。(但是刷新页面会导致解开锁)
- 支付完成后返回给前端,前端跳到支付成功页面。
- 每次进来先获取Redis里面的 key,判断是否存在,如果不存在就继续往下走
- 判断当前订单的状态,是否等于待支付,如果是继续往下走
- 使用订单id作为 key 存入Redis里面去(分布式锁实现)
- 执行一系列的业务逻辑
- 删除Redis里面的key
这个和上面的代码实现如出一辙就不再写了。
版权声明
本文为[小道仙97]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Tomwildboar/article/details/118807634
边栏推荐
- be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
- Student achievement management
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Xamarin effect Chapter 21 expandable floating operation button in GIS
- Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- MySQL port is occupied when building xampp
- Is it difficult to choose binary version control tools? After reading this article, you will find the answer
- Queue storage and circular queue
- TP5 email (2020-05-27)
猜你喜欢

Xamarin effect Chapter 22 recording effect

数据挖掘系列(3)_Excel的数据挖掘插件_估计分析

手机连接电脑后,QT的QDIR怎么读取手机文件路径

Fight leetcode again (290. Word law)

The most understandable life cycle of dependency injection

C language to achieve address book - (static version)

ASP. Net 6 middleware series - Custom middleware classes

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?

宁德时代地位不保?

Configuration table and page information automatically generate curd operation page
随机推荐
Student achievement management
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
2022G2电站锅炉司炉考试题库及在线模拟考试
Web Course Design - his system
C language to achieve address book - (static version)
2022t elevator repair test simulation 100 questions and online simulation test
[MySQL] left Function | Right Function
[authentication / authorization] customize an authentication handler
C语言实现通讯录----(静态版本)
求二叉树的叶子结点个数
Source generator actual combat
Establishing and traversing binary tree
Peut recevoir plusieurs paramètres de type de données - paramètres variables
MySQL port is occupied when building xampp
C WPF UI framework mahapps switching theme
交换二叉树中每个结点的左和右
Configure automatic implementation of curd projects
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
A set of combination boxing to create an idea eye protection scheme