第一次有人把“分布式事务”讲的这么简单明了

                <section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style="padding-top: 10px;padding-right: 10px;padding-left: 10px;box-sizing: border-box;background-color: rgb(239, 239, 239);"><span style="display: inline-block; 5%;line-height: 0.8;font-weight: bolder;font-size: 48px;box-sizing: border-box;"><section style="box-sizing: border-box;">“</section></span> <section style="display: inline-block;vertical-align: top;float: right; 90%;line-height: 1.5;font-size: 15px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><span style="letter-spacing: 1px;">不知道你是否遇到过这样的情况,去小卖铺买东西,付了钱,但是店主因为处理了一些其他事,居然忘记你付了钱,又叫你重新付。</span></p></section><section style="clear: both;box-sizing: border-box;"></section></section></section></section></section><p style="line-height: 1.75em;"><br></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 5px;"><img class="" data-copyright="0" data-ratio="0.5800756620428752" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/MOwlO0INfQo4uVkSDvKvc7QiaMee0CC22LzZwD7TKg9ia4s4gd3ibfFw2BK2SBKr3gLTOA4MInRBDsNePZ2DcWRgw/640?wx_fmt=png" data-type="png" data-w="793" style=" 677px !important; height: auto !important; visibility: visible !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102806281-683950303.png" crossorigin="anonymous" data-fail="0"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">又或者在网上购物明明已经扣款,但是却告诉我没有发生交易。这一系列情况都是因为没有事务导致的。这说明了事务在生活中的一些重要性。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">有了事务,你去小卖铺买东西,那就是一手交钱一手交货。有了事务,你去网上购物,扣款即产生订单交易。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="border-bottom- 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"><section style="display: inline-block;border-bottom- 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;">事务的具体定义</p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元,组成事务的所有操作只有在所有操作均能正常执行的情况下方能提交,只要其中任一操作执行失败,都将导致整个事务的回滚。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">简单地说,事务提供一种“要么什么都不做,要么做全套(All or Nothing)”机制。</span></p><h1 style="line-height: normal;"><br></h1><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="border-bottom- 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"><section style="display: inline-block;border-bottom- 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;">数据库本地事务</p></section></section></section></section><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>ACID</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">说到数据库事务就不得不说,数据库事务中的四大特性 ACID:</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">A:原子性(Atomicity),</span></strong><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">就像你买东西要么交钱收货一起都执行,要么发不出货,就退钱。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">C:一致性(Consistency),</span></strong><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">事务的一致性指的是在一个事务执行之前和执行之后数据库都必须处于一致性状态。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果事务成功地完成,那么系统中所有变化将正确地应用,系统处于有效状态。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果在事务中出现错误,那么系统中的所有变化将自动地回滚,系统返回到原始状态。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">I:隔离性(Isolation),</span></strong><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">指的是在并发环境中,当不同的事务同时操纵相同的数据时,每个事务都有各自的完整数据空间。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">由并发事务所做的修改必须与任何其他并发事务所做的修改隔离。事务查看数据更新时,数据所处的状态要么是另一事务修改它之前的状态,要么是另一事务修改它之后的状态,事务不会查看到中间状态的数据。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">打个比方,你买东西这个事情,是不影响其他人的。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">D:持久性(Durability),</span></strong><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">指的是只要事务成功结束,它对数据库所做的更新就必须永久保存下来。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">即使发生系统崩溃,重新启动数据库系统后,数据库还能恢复到事务成功结束时的状态。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">打个比方,你买东西的时候需要记录在账本上,即使老板忘记了那也有据可查。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>InnoDB 实现原理</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">InnoDB 是 MySQL 的一个存储引擎,大部分人对&nbsp;<span style="letter-spacing: 1px;color: rgb(89, 89, 89);font-size: 15px;line-height: 25.6px;">MySQL&nbsp;</span>都比较熟悉,这里简单介绍一下数据库事务实现的一些基本原理。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">在本地事务中,服务和资源在事务的包裹下可以看做是一体的,如下图:</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.4785714285714286" data-type="png" data-w="700" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLNqkmtvawpIKBPZZScOicOukYx3IJiaibAdfdWpknHE0Me4oLdo1miaAiatQ/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; background-color: rgb(238, 237, 235); background-size: 22px; background-position: 50% 50%; background-repeat: no-repeat; overflow-wrap: break-word !important; visibility: visible !important;  677px !important; height: auto !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102836233-1494109914.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">我们的本地事务由资源管理器进行管理:</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.9601554907677357" data-type="png" data-w="1029" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLm3LObVhUf7QAl4zljjGaia2mhIxMwV1xqjow3gH6Z6bic8PTZhakL8sg/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  677px !important; height: auto !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102856263-1903152263.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">而事务的 ACID 是通过 InnoDB 日志和锁来保证。事务的隔离性是通过数据库锁的机制实现的,持久性通过 Redo Log(重做日志)来实现,原子性和一致性通过 Undo Log 来实现。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Undo Log 的原理很简单,为了满足事务的原子性,在操作任何数据之前,首先将数据备份到一个地方(这个存储数据备份的地方称为 Undo Log)。然后进行数据的修改。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果出现了错误或者用户执行了 Rollback 语句,系统可以利用 Undo Log 中的备份将数据恢复到事务开始之前的状态。&nbsp;</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">和 Undo Log 相反,Redo Log 记录的是新数据的备份。在事务提交前,只要将 Redo Log 持久化即可,不需要将数据持久化。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当系统崩溃时,虽然数据没有持久化,但是 Redo Log 已经持久化。系统可以根据 Redo Log 的内容,将所有数据恢复到最新的状态。对具体实现过程有兴趣的同学可以去自行搜索扩展。</span></p><h1 style="line-height: normal;"><br></h1><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="border-bottom- 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"><section style="display: inline-block;border-bottom- 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;">分布式事务</p></section></section></section></section><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>什么是分布式事务</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">分布式事务指事务的参与者、支持事务的服务器、资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">简单的说,就是一次大的操作由不同的小操作组成,这些小的操作分布在不同的服务器上,且属于不同的应用,分布式事务需要保证这些小操作要么全部成功,要么全部失败。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">本质上来说,分布式事务就是为了保证不同数据库的数据一致性。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>分布式事务产生的原因</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">从上面本地事务来看,我们可以分为两块:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Service 产生多个节点</span></strong></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Resource 产生多个节点</span></strong></p></li></ul><h3 style="line-height: normal;"><br></h3><h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;font-size: 16px;">Service 多个节点</span></strong></h3><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">随着互联网快速发展,微服务,SOA 等服务架构模式正在被大规模的使用。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">举个简单的例子,一个公司之内,用户的资产可能分为好多个部分,比如余额,积分,优惠券等等。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在公司内部有可能积分功能由一个微服务团队维护,优惠券又是另外的团队维护。</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.7280405405405406" data-type="png" data-w="592" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLLBpvygW8oSbJb5jRhXuc2rF4cmPew11D1NSDbqr1ib6Tt1HcB3zGUUg/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  592px !important; height: auto !important;" _width="592px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102920468-1991100842.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">这样的话就无法保证积分扣减了之后,优惠券能否扣减成功。</span></p><h3 style="line-height: normal;"><br></h3><h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">Resource多个节点</span></strong></span></h3><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">同样的,互联网发展得太快了,我们的&nbsp;<span style="letter-spacing: 1px;color: rgb(89, 89, 89);font-size: 15px;line-height: 25.6px;">MySQL&nbsp;</span>一般来说装千万级的数据就得进行分库分表。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于一个支付宝的转账业务来说,你给朋友转钱,有可能你的数据库是在北京,而你的朋友的钱是存在上海,所以我们依然无法保证他们能同时成功。&nbsp;</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.8050632911392405" data-type="png" data-w="395" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLVY9ZT60xNdYPwjPjRDO3YjZQCibd0UnatKfWTXO665DskUMHrMyl1Yg/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  395px !important; height: auto !important;" _width="395px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102941149-747407800.png" crossorigin="anonymous" data-fail="0"></p><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>分布式事务的基础</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">从上面来看分布式事务是随着互联网高速发展应运而生的,这是一个必然。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">我们之前说过数据库的 ACID 四大特性,已经无法满足我们分布式事务,这个时候又有一些新的大佬提出一些新的理论。</span></p><p style="line-height: normal;"><br></p><h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">CAP</span></strong></span></h3><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">CAP 定理,又被叫作布鲁尔定理。对于设计分布式系统(不仅仅是分布式事务)的架构师来说,CAP 就是你的入门理论。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">C (一致性):</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对某个指定的客户端来说,读操作能返回最新的写操作。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于数据分布在不同节点上的数据来说,如果在某个节点更新了数据,那么在其他节点如果都能读取到这个最新的数据,那么就称为强一致,如果有某个节点没有读取到,那就是分布式不一致。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">A (可用性):</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">非故障的节点在合理的时间内返回合理的响应(不是错误和超时的响应)。可用性的两个关键一个是合理的时间,一个是合理的响应。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">合理的时间指的是请求不能无限被阻塞,应该在合理的时间给出返回。合理的响应指的是系统应该明确返回结果并且结果是正确的,这里的正确指的是比如应该返回 50,而不是返回 40。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;line-height: 1.75em;margin-left: 8px;margin-right: 8px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">P (分区容错性):</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当出现网络分区后,系统能够继续工作。打个比方,这里集群有多台机器,有台机器网络出现了问题,但是这个集群仍然可以正常工作。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">熟悉 CAP 的人都知道,三者不能共有,如果感兴趣可以搜索 CAP 的证明,在分布式系统中,网络无法 100% 可靠,分区其实是一个必然现象。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果我们选择了 CA 而放弃了 P,那么当发生分区现象时,为了保证一致性,这个时候必须拒绝请求,但是 A 又不允许,所以分布式系统理论上不可能选择 CA 架构,只能选择 CP 或者 AP 架构。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于 CP 来说,放弃可用性,追求一致性和分区容错性,我们的 ZooKeeper 其实就是追求的强一致。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于 AP 来说,放弃一致性(这里说的一致性是强一致性),追求分区容错性和可用性,这是很多分布式系统设计时的选择,后面的 BASE 也是根据 AP 来扩展。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">顺便一提,CAP 理论中是忽略网络延迟,也就是当事务提交时,从节点 A 复制到节点 B 没有延迟,但是在现实中这个是明显不可能的,所以总会有一定的时间是不一致。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">同时 CAP 中选择两个,比如你选择了 CP,并不是叫你放弃 A。因为 P 出现的概率实在是太小了,大部分的时间你仍然需要保证 CA。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">就算分区出现了你也要为后来的 A 做准备,比如通过一些日志的手段,是其他机器回复至可用。</span></p><h3 style="line-height: normal;"><br></h3><h3 style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 16px;"><strong><span style="color: rgb(89, 89, 89);letter-spacing: 1px;">BASE</span></strong></span></h3><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">BASE 是 Basically Available(基本可用)、Soft state(软状态)和 Eventually consistent (最终一致性)三个短语的缩写,是对 CAP 中 AP 的一个扩展。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">基本可用:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">分布式系统在出现故障时,允许损失部分可用功能,保证核心功能可用。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">软状态:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">允许系统中存在中间状态,这个状态不影响系统可用性,这里指的是 CAP 中的不一致。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">最终一致:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">最终一致是指经过一段时间后,所有节点数据都将会达到一致。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">BASE 解决了 CAP 中理论没有网络延迟,在 BASE 中用软状态和最终一致,保证了延迟后的一致性。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">BASE 和 ACID 是相反的,它完全不同于 ACID 的强一致性模型,而是通过牺牲强一致性来获得可用性,并允许数据在一段时间内是不一致的,但最终达到一致状态。</span></p><h1 style="line-height: normal;"><br></h1><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="border-bottom- 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"><section style="display: inline-block;border-bottom- 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;">分布式事务解决方案</p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">有了上面的理论基础后,这里开始介绍几种常见的分布式事务的解决方案。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>是否真的要分布式事务</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">在说方案之前,首先你一定要明确你是否真的需要分布式事务?</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">上面说过出现分布式事务的两个原因,其中有个原因是因为微服务过多。我见过太多团队一个人维护几个微服务,太多团队过度设计,搞得所有人疲劳不堪。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">而微服务过多就会引出分布式事务,这个时候我不会建议你去采用下面任何一种方案,而是请把需要事务的微服务聚合成一个单机服务,使用数据库的本地事务。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">因为不论任何一种方案都会增加你系统的复杂度,这样的成本实在是太高了,千万不要因为追求某些设计,而引入不必要的成本和复杂度。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果你确定需要引入分布式事务可以看看下面几种常见的方案。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>2PC</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">说到 2PC 就不得不聊数据库分布式事务中的 XA Transactions。&nbsp;</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.6096938775510204" data-type="jpeg" data-w="784" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrL2Fpo6TgKVpEO6XUmeQCSDCv1gdzPfZVFXKPpkTx0r8sxel8pNuhtBA/640?wx_fmt=jpeg" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  677px !important; height: auto !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207102958697-1835395995.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">在 XA 协议中分为两阶段:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">事务管理器要求每个涉及到事务的数据库预提交(precommit)此操作,并反映是否可以提交。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">事务协调器要求每个数据库提交数据,或者回滚数据。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">优点:&nbsp;</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">尽量保证了数据的强一致,实现成本较低,在各大主流数据库都有自己实现,对于 MySQL 是从 5.5 开始支持。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">缺点:</span></p><ul class="list-paddingleft-2" style="margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">单点问题:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">事务管理器在整个流程中扮演的角色很关键,如果其宕机,比如在第一阶段已经完成,在第二阶段正准备提交的时候事务管理器宕机,资源管理器就会一直阻塞,导致数据库无法使用。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">同步阻塞:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在准备就绪之后,资源管理器中的资源一直处于阻塞,直到提交完成,释放资源。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">数据不一致:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">两阶段提交协议虽然为分布式数据强一致性所设计,但仍然存在数据不一致性的可能。</span></p><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">比如在第二阶段中,假设协调者发出了事务 Commit 的通知,但是因为网络问题该通知仅被一部分参与者所收到并执行了 Commit 操作,其余的参与者则因为没有收到通知一直处于阻塞状态,这时候就产生了数据的不一致性。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">总的来说,XA 协议比较简单,成本较低,但是其单点问题,以及不能支持高并发(由于同步阻塞)依然是其最大的弱点。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>TCC</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">关于 TCC(Try-Confirm-Cancel)的概念,最早是由 Pat Helland 于 2007 年发表的一篇名为《Life beyond Distributed Transactions:an Apostate’s Opinion》的论文提出。&nbsp;</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">TCC 事务机制相比于上面介绍的 XA,解决了如下几个缺点:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">解决了协调者单点,</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">由主业务方发起并完成这个业务活动。业务活动管理器也变成多点,引入集群。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">同步阻塞:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">引入超时,超时后进行补偿,并且不会锁定整个资源,将资源转换为业务逻辑形式,粒度变小。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;margin-bottom: 5px;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">数据一致性,</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">有了补偿机制之后,由业务活动管理器控制一致性。</span></p></li></ul><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.6322916666666667" data-type="png" data-w="1920" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrL8JiaQCljO1cq47hwuR9eNZib5J83o6SCtIGST9GHgGhXIbePs1lfX3Qg/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  677px !important; height: auto !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207103016952-1257538091.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">对于 TCC 的解释:</span></p><ul class="list-paddingleft-2" style="margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Try 阶段:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">尝试执行,完成所有业务检查(一致性),预留必需业务资源(准隔离性)。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Confirm 阶段:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">确认真正执行业务,不作任何业务检查,只使用 Try 阶段预留的业务资源,Confirm 操作满足幂等性。要求具备幂等设计,Confirm 失败后需要进行重试。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Cancel 阶段:</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">取消执行,释放 Try 阶段预留的业务资源,Cancel 操作满足幂等性。Cancel 阶段的异常和 Confirm 阶段异常处理方案基本上一致。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="color: rgb(89, 89, 89);"><strong><span style="font-size: 15px;letter-spacing: 1px;">举个简单的例子:</span></strong></span><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果你用 100 元买了一瓶水, Try 阶段:你需要向你的钱包检查是否够 100 元并锁住这 100 元,水也是一样的。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果有一个失败,则进行 Cancel(释放这 100 元和这一瓶水),如果 Cancel 失败不论什么失败都进行重试 Cancel,所以需要保持幂等。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果都成功,则进行 Confirm,确认这 100 元被扣,和这一瓶水被卖,如果 Confirm 失败无论什么失败则重试(会依靠活动日志进行重试)。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">对于 TCC 来说适合一些:</span></p><ul class="list-paddingleft-2" style="margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">强隔离性,严格一致性要求的活动业务。</span></strong></p></li><li><p style="text-align: justify;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">执行时间较短的业务。</span></strong></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">实现参考:https://github.com/liuyangming/ByteTCC/。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>本地消息表</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">本地消息表这个方案最初是 eBay 提出的,eBay 的完整方案 https://queue.acm.org/detail.cfm?id=1394128。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">此方案的核心是将需要分布式处理的任务通过消息日志的方式来异步执行。消息日志可以存储到本地文本、数据库或消息队列,再通过业务规则自动或人工发起重试。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">人工重试更多的是应用于支付场景,通过对账系统对事后问题的处理。&nbsp;</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.46166666666666667" data-type="png" data-w="600" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrL8w0nib0Hej1ys7mfibcjKjyYFN0MxyY12nUFLt8Lm11xZI2VuDVbCUXg/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; background-color: rgb(238, 237, 235); background-size: 22px; background-position: 50% 50%; background-repeat: no-repeat; overflow-wrap: break-word !important; visibility: visible !important;  600px !important; height: auto !important;" _width="600px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207103036169-1928964222.png" crossorigin="anonymous" data-fail="0"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">对于本地消息队列来说核心是把大事务转变为小事务。</span><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">还是举上面用 100 元去买一瓶水的例子。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">1. </span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">当你扣钱的时候,你需要在你扣钱的服务器上新增加一个本地消息表,你需要把你扣钱和减去水的库存写入到本地消息表,放入同一个事务(依靠数据库本地事务保证一致性)。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">2. </span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">这个时候有个定时任务去轮询这个本地事务表,把没有发送的消息,扔给商品库存服务器,叫它减去水的库存,到达商品服务器之后,这时得先写入这个服务器的事务表,然后进行扣减,扣减成功后,更新事务表中的状态。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">3. </span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">商品服务器通过定时任务扫描消息表或者直接通知扣钱服务器,扣钱服务器在本地消息表进行状态更新。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">4. </span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">针对一些异常情况,定时扫描未成功处理的消息,进行重新发送,在商品服务器接到消息之后,首先判断是否是重复的。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果已经接收,再判断是否执行,如果执行在马上又进行通知事务;如果未执行,需要重新执行由业务保证幂等,也就是不会多扣一瓶水。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">本地消息队列是 BASE 理论,是最终一致模型,适用于对一致性要求不高的情况。实现这个模型时需要注意重试的幂等。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>MQ 事务</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">在 RocketMQ 中实现了分布式事务,实际上是对本地消息表的一个封装,将本地消息表移动到了 MQ 内部。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">下面简单介绍一下MQ事务,如果想对其详细了解可以参考:https://www.jianshu.com/p/453c6e7ff81c。&nbsp;</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.533011272141707" data-type="png" data-w="621" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLCcFeibsiawPb8IP5N1sVdGp8UJbUcEJpr09w4C7Q6qyrAkx2qNB3mKOA/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; background-color: rgb(238, 237, 235); background-size: 22px; background-position: 50% 50%; background-repeat: no-repeat; overflow-wrap: break-word !important; visibility: visible !important;  621px !important; height: auto !important;" _width="621px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207103055322-331550199.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">基本流程如下:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;margin-left: 8px;margin-right: 8px;"><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">第一阶段 Prepared 消息,会拿到消息的地址。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">第二阶段执行本地事务。</span></p></li><li><p style="text-align: justify;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">第三阶段通过第一阶段拿到的地址去访问消息,并修改状态。消息接受者就能使用这个消息。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果确认消息失败,在 RocketMQ Broker 中提供了定时扫描没有更新状态的消息。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果有消息没有得到确认,会向消息发送者发送消息,来判断是否提交,在&nbsp;<span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 28px;text-align: justify;">RocketMQ&nbsp;</span>中是以 Listener 的形式给发送者,用来处理。&nbsp;</span></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;line-height: 1.75em;margin-bottom: 5px;"><img class="" data-ratio="0.5288461538461539" data-type="png" data-w="624" data-src="https://mmbiz.qpic.cn/mmbiz_png/WLIGprPy3z6QR2ahbaxn60XjuXS3XNrLOfOQg3tt68VTvgALGu5ERmeeyic0vibbyEgk11WbAtFc6YaOvGvQyZ0w/640?wx_fmt=png" style="box-sizing: border-box; border: 2px solid rgb(238, 238, 238); border-radius: 6px; overflow-wrap: break-word !important; visibility: visible !important;  624px !important; height: auto !important;" _width="624px" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207103117098-1816060300.png" crossorigin="anonymous" data-fail="0"><br style="max- 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">如果消费超时,则需要一直重试,消息接收端需要保证幂等。如果消息消费失败,这时就需要人工进行处理,因为这个概率较低,如果为了这种小概率时间而设计这个复杂的流程反而得不偿失。</span></p><h2 style="line-height: normal;"><br></h2><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 10px;margin-bottom: 10px;box-sizing: border-box;"><section style=" 0.6em;display: inline-block;vertical-align: middle;box-sizing: border-box;"><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.2;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <strong><span style=" 0.6em;height: 0.6em;display: block;opacity: 0.6;margin-top: 2px;margin-bottom: 2px;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> <span style=" 0.6em;height: 0.6em;display: block;opacity: 1;box-sizing: border-box;background-color: rgb(89, 89, 89);"></span> </strong></section><section style="display: inline-block;vertical-align: middle;font-size: 18px;padding-left: 5px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;"><strong>Saga 事务</strong></p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Saga 是 30 年前一篇数据库伦理提到的一个概念。其核心思想是将长事务拆分为多个本地短事务,由 Saga 事务协调器协调,如果正常结束那就正常完成,如果某个步骤失败,则根据相反顺序一次调用补偿操作。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">Saga 的组成:</span></strong><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">每个 Saga 由一系列 sub-transaction Ti 组成,每个 Ti 都有对应的补偿动作 Ci,补偿动作用于撤销 Ti 造成的结果。这里的每个 T,都是一个本地事务。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">可以看到,和 TCC 相比,Saga 没有“预留 try”动作,它的 Ti 就是直接提交到库。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">Saga 的执行顺序有两种:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">T1,T2<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>T3<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>...<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>Tn。</span></strong></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">T1<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>T2<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>...<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>Tj<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>Cj<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>...<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>C2<strong style="line-height: 28px;text-align: justify;white-space: normal;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">,</span></strong>C1,其中 0 &lt; j &lt; n 。</span></strong></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">Saga 定义了两种恢复策略:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">向后恢复,</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">即上面提到的第二种执行顺序,其中 j 是发生错误的 sub-transaction,这种做法的效果是撤销掉之前所有成功的 sub-transation,使得整个 Saga 的执行结果撤销。</span></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">向前恢复,</span></strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">适用于必须要成功的场景,执行顺序是类似于这样的:T1,T2,...,Tj(失败),Tj(重试),...,Tn,其中 j 是发生错误的 sub-transaction。该情况下不需要 Ci。</span></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">这里要注意的是,在 Saga 模式中不能保证隔离性,因为没有锁住资源,其他事务依然可以覆盖或者影响当前事务。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">还是拿 100 元买一瓶水的例子来说,这里定义:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">T1 = 扣 100 元,T2 = 给用户加一瓶水,T3 = 减库存一瓶水。</span></strong></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">C1 = 加100元,C2 = 给用户减一瓶水,C3 = 给库存加一瓶水。</span></strong></p></li></ul><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">我们一次进行 T1,T2,T3 如果发生问题,就执行发生问题的 C 操作的反向。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">上面说到的隔离性的问题会出现在,如果执行到 T3 这个时候需要执行回滚,但是这个用户已经把水喝了(另外一个事务),回滚的时候就会发现,无法给用户减一瓶水了。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">这就是事务之间没有隔离性的问题。</span><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">可以看见 Saga 模式没有隔离性的影响还是较大,可以参照华为的解决方案:从业务层面入手加入一 Session 以及锁的机制来保证能够串行化操作资源。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">也可以在业务层面通过预先冻结资金的方式隔离这部分资源, 最后在业务操作的过程中可以通过及时读取当前状态的方式获取到最新的更新。(</span><span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 1.75em;">具体实例:可以参考华为的 Service Comb<span style="color: rgb(89, 89, 89);font-size: 15px;letter-spacing: 1px;line-height: 28px;text-align: justify;">)</span></span></p><h1 style="line-height: normal;"><br></h1><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="border-bottom- 1px;border-bottom-style: solid;border-bottom-color: black;margin-top: 0.5em;margin-bottom: 0.5em;line-height: 1.2;box-sizing: border-box;"><section style="display: inline-block;border-bottom- 6px;border-bottom-style: solid;border-color: rgb(89, 89, 89);margin-bottom: -1px;font-size: 20px;color: rgb(89, 89, 89);box-sizing: border-box;"><p style="box-sizing: border-box;">最后</p></section></section></section></section><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">还是那句话,能不用分布式事务就不用,如果非得使用的话,结合自己的业务分析,看看自己的业务比较适合哪一种,是在乎强一致,还是最终一致即可。</span></p><p style="line-height: normal;"><br></p><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><span style="font-size: 15px;letter-spacing: 1px;color: rgb(71, 193, 168);">最后在总结一些问题,大家可以下来自己从文章找寻答案:</span></p><ul class="list-paddingleft-2" style="list-style-type: disc;"><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">ACID 和 CAP 的 CA 是一样的吗?</span></strong></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">分布式事务常用的解决方案的优缺点是什么?适用于什么场景?</span></strong></p></li><li><p style="text-align: justify;margin-left: 8px;margin-right: 8px;line-height: 1.75em;"><strong><span style="font-size: 15px;color: rgb(89, 89, 89);letter-spacing: 1px;">分布式事务出现的原因?用来解决什么痛点?</span></strong></p></li></ul><p style="line-height: normal;"><br></p><p style="white-space: normal;text-align: justify;line-height: 1.75em;"><span style="color: rgb(89, 89, 89);letter-spacing: 1px;"><em><span style="font-size: 14px;">作者:<span style="line-height: 25.6px;">咖啡拿铁,目前就职于美团点评</span></span></em></span></p><p style="white-space: normal;text-align: justify;line-height: 1.75em;"><span style="color: rgb(89, 89, 89);letter-spacing: 1px;"><em><span style="font-size: 14px;">编辑:陶家龙、孙淑娟</span></em></span><br></p><p style="text-align: justify;line-height: 1.75em;margin-bottom: 5px;"><span style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;"><em>出处:转载自咖啡拿铁(ID:close_3092860495)微信公众号</em></span></p><section style="box-sizing: border-box;"><section class="" style="box-sizing: border-box;" powered-by="xiumi.us"><section style="margin-top: 0.5em;margin-bottom: 0.5em;box-sizing: border-box;"><section style="font-size: 15px;border-style: solid;border- 0px 0px 1px;color: rgb(89, 89, 89);border-bottom-color: rgba(215, 215, 215, 0.960784);box-sizing: border-box;"><p style="box-sizing: border-box;"><span style="letter-spacing: 1px;"><strong>精彩文章推荐:</strong></span></p></section></section></section></section><p style="text-align: justify;line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA==&amp;mid=2655818244&amp;idx=1&amp;sn=b691cd0c915e1f3adcf8a5a2aa29232b&amp;chksm=bd74dfd38a0356c582392f4900e85fce946d48dacf2f5095f2f93bb665c6868f98a7e2f849b7&amp;scene=21#wechat_redirect" target="_blank" style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;text-decoration: none;"><span style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;">NoSQL还是SQL?这一篇讲清楚</span></a><br></p><p style="text-align: justify;line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA==&amp;mid=2655818227&amp;idx=1&amp;sn=4604868860964684808b398d1600c11d&amp;chksm=bd74dc248a035532a4d900e3bd6e6e72324cdda1430160b867db7962bee3e4ad23d6f63f980a&amp;scene=21#wechat_redirect" target="_blank" style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;text-decoration: none;"><span style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;">6张动态图轻松学习TCP三次握手和四次挥手</span></a><br></p><p style="text-align: justify;line-height: 2em;"><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA==&amp;mid=2655818245&amp;idx=1&amp;sn=39a21658f60a502ea3e6741368f0abcb&amp;chksm=bd74dfd28a0356c46f9735ca0ff3e145d2f78bc61bc6a183d7fe6f3925e6527a13bdb2776e5e&amp;scene=21#wechat_redirect" target="_blank" style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;text-decoration: none;"><span style="font-size: 14px;color: rgb(89, 89, 89);letter-spacing: 1px;">29岁成为阿里P8,如何在5年内完成晋升“三连跳”?</span></a><br></p>
     原文地址:https://mp.weixin.qq.com/s/T-Q9eouj4unrWh8Q9bJoOA       </div>
原文地址:https://www.cnblogs.com/jpfss/p/12000838.html