当前位置:网站首页>分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)
2022-06-28 01:10:00 【May Hacker】
前言
基于消息补偿的最终一致性方案是一种分布式事务的实现方式,又包括本地消息表、消息队列两种方式。
一、本地消息表
核心思想是将分布式事务拆分成本地事务进行处理,本地消息表实现最终一致性。

- 步骤1和2,系统收到用户下单请求,将订单业务数据写入订单表中,同时把该订单对应的消息数据写入本地消息表中,订单表与本地消息表为同一个数据库,更新订单和存储消息为同一个本地事务,数据库事务处理,要么都成功,要么都失败。
- 步骤345,订单服务发送消息到消息队列,库存服务收到消息,进行库存业务操作,更新库存数据
- 步骤6和7,返回业务处理结果,订单服务收到结果后,将本地消息表中的数据设置完成状态或者删除数据。
- 步骤8,另起定时任务,定时扫描本地消息表,看是否有未完成的任务,有则重试。
优缺点
- 实现简单,开发成本低
- 订单表与本地消息表为同一个数据库
二、支持事务的消息队列
RMQ支持事务消息,与常见的数据库事务流程类似,生产者生产一条事务消息,等生产者本地操作ok了,会向MQ Server发送提交确认。
在这种情况下,事务消息初始被扔到消息队列中是没办法消费的,只有当该事务消息被提交了,这个消息才能被消费者所消费。
同理,如果rollback了,RMQ会从消息队列中把该消息删掉。

- 生产者生产消息给MQ,此时未提交,称该消息为HalfMsg,即半消息
- MQ受到该消息,会给生产者一个OK确认
- 生产者执行本地事务
- 根据本地事务执行的结果向MQ提交commit或者rollback,MQ根据commit或者rollback对消息进行相应操作,即消费或者丢弃
- 如果生产者提交commit或者rollback提交超时,即第四步没有收到来自生产者的请求,MQ回调检查该消息
- MQ回调检查本地事务的状态
参考
https://www.jianshu.com/p/e31d9ebed201
边栏推荐
- 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
- > Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
- Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
- "Dadao Zhichuang" won a ten million prea+ round of financing and launched a technology consumption robot
- How to systematically learn LabVIEW?
- Win11不能拖拽图片到任务栏软件上快速打开怎么办
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- Cloud native (30) | kubernetes' app store Helm
- NER中BiLSTM-CRF解读Forward_algorithm
- Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
猜你喜欢

【Kotlin】在Android官方文档中对其语法的基本介绍和理解
![[today in history] June 17: the creator of the term](/img/00/30ccc2f54415a6aca000c42e277dc3.png)
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on

How to run unity webgl after packaging (Firefox configuration)

Usage differences between isempty and isblank

Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法

【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用

PSM总结

简单ELK配置实现生产级别的日志采集和查询实践

The graduation season is coming, and the number of college graduates in 2022 has exceeded 10 million for the first time
![[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
随机推荐
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
Opencv——霍夫变换以及遇到的一些问题
为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out
Mysql database foundation: DML data operation language
Win11不能拖拽图片到任务栏软件上快速打开怎么办
【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
Usage differences between isempty and isblank
Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
Flask Foundation: template inheritance + static file configuration
Unity WebGL打包后怎么运行(火狐配置)
Win11 ne peut pas faire glisser l'image sur le logiciel de la barre des tâches
PSM总结
[cloud native] - docker installation and deployment of distributed database oceanbase
2-5基础配置-Win2003增加攻击面
Writing based on stm32
Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born