接口的幂等性

作者: 奔跑的笨鸟 | 来源:发表于2019-03-29 19:20 被阅读0次

Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra (in particular, in the theory of projectors and closure operators) and functional programming (in which it is connected to the property of referential transparency).
https://en.wikipedia.org/wiki/Idempotence

以上是 幂 等性的定义,大概意思是:在数学和计算机服务中,对于 某种操作执行多次,结果 相同。

接口为什么需要幂等性?

在计算机中,由于网络抖动,临时故障,服务调用失败避免不了,尤其是分布式系统中,接口调用失败更为常见,接口的幂等设计尤其更为重要。

Rest 中方法中的幂等性

  • GET: 获取资源,不对资源更新,属于幂等。
  • DELETE: 删除资源,由于在URI上指定了要删除资源,一次和多次的结果肯定相同,属于幂等。
  • POST 和PUT的幂等?

POST
The POST method requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, for example, an annotation for existing resources; a message for a bulletin board, newsgroup, mailing list, or comment thread; a block of data that is the result of submitting a web form to a data-handling process; or an item to add to a database.
PUT
The PUT method requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI.

从HTTP 对POST和PUT的定义看出,POST是创建资源,PUT既可以创建也可以更新资源。
那Rest 中的POST和PUT应该这样使用:

  • POST URI: POST每次都创建新资源,应该不是幂等的。
POST URI:
✔http://www.xxx.com/orders
✖http://www.xxx.com/orders/1
  • PUT: PUT方法如果资源存在,就更新,如果不存在就创建 指定的资源,所以PUT是幂等的。
PUT URI:
http://www.xxx.com/orders/1

相关文章

  • SpringBoot接口幂等性实现的4种方案!

    目录 什么是幂等性 什么是接口幂等性 为什么需要实现幂等性 引入幂等性后对系统的影响 Restful API 接口...

  • 什么是接口的幂等性,如何实现接口幂等性?一文搞定

    每天一个知识点 什么是接口的幂等性,如何实现接口幂等性? (一)幂等性概念 幂等性原本是数学上的概念,用在接口上就...

  • 接口幂等性

    接口幂等性 什么是接口幂等性? 最简单想到的实现接口幂等性(重复提交)的操作是什么? 最简单稍靠谱的解决方案是什么?

  • 接口幂等性书目录

    1.幂等性定义 1.1 数学定义 1.2 HTTP规范的定义 2. 何种接口提供幂等性 2.1 HTTP支持幂等性...

  • 接口的幂等性的N种考虑

    分布式服务接口的幂等性如何设计 什么是幂等性 一个分布式系统中的某个接口,要保证幂等性,该如何保证?这个事儿其实是...

  • 电商课题:幂等性

    一. 断言: 幂等性的数学表达:f(f(x)) = f(x)。幂等性是系统接口对外的一种承诺。幂等性指的是,使用相...

  • 接口幂等性

    什么是接口幂等性? 幂等是数学和计算机学的概念,常见于抽象代数中,即f(f(x)) = f(x)。简单来讲就是接口...

  • 接口幂等性

    学习缘由 幂等性这个词很高大上,但是又不明白其中含义,因此查资料进行理解学习。参考原文:路人甲java 什么是幂等...

  • 接口幂等性

    一、概念 当微服务之间调用时服务A向服务B重复发送消息或者用户多次点击导致重复操作数据库。 例如支付订单接口,如果...

  • 接口幂等性

    含义:接口可重复调用后,在调用方多次调用的情况下,接口最终得到的结果是一致的。 有些接口天然具备幂等性,如查询接口...

网友评论

    本文标题:接口的幂等性

    本文链接:https://www.haomeiwen.com/subject/gqrlbqtx.html