【转载】分布式系统中的事务问题

微服务 0 1171
唐志兴(技术开发部)
唐志兴(技术开发部) LV2 关注 2021年10月26日 15:53 编辑
<h1 style="margin-top: 0px; margin-bottom: 20px; font-size: 28px; line-height: 1.8; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><a id="cb_post_title_url" href="https://www.cnblogs.com/mushroom/p/13788039.html"><span style="vertical-align: middle;">分布式系统中的事务问题</span></a></h1><div style="clear: both; color: rgb(125, 139, 141); font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"></div><div style="color: rgb(125, 139, 141); font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><div id="cnblogs_post_body" style="margin-bottom: 20px; word-break: break-word; color: rgb(51, 51, 51); line-height: 1.8;"><h2 id="介绍" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">介绍</h2><p style="margin-right: auto; margin-left: auto;">在分布式系统、微服务架构大行其道的今天,服务间互相调用出现失败已经成为常态。如何处理异常,如何保证数据一致性,成为微服务设计过程中,绕不开的一个难题。 在不同的业务场景下,解决方案会有所差异,常见的方式有:</p><ol style="margin-top: 0px; margin-bottom: 1em; margin-left: 40px;"><li style="list-style-type: decimal;">阻塞式重试;</li><li style="list-style-type: decimal;">2PC、3PC 传统事务;</li><li style="list-style-type: decimal;">使用队列,后台异步处理;</li><li style="list-style-type: decimal;">TCC 补偿事务;</li><li style="list-style-type: decimal;">本地消息表(异步确保);</li><li style="list-style-type: decimal;">MQ 事务。</li></ol><p style="margin-right: auto; margin-left: auto;">本文侧重于其他几项,关于 2PC、3PC 传统事务,网上资料已经非常多了,这里不多做重复。</p><h2 id="阻塞式重试" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">阻塞式重试</h2><p style="margin-right: auto; margin-left: auto;">在微服务架构中,阻塞式重试是比较常见的一种方式。伪代码示例:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;">m := db.Insert(sql) err := request(B-Service,m) <span style="color: rgb(0, 0, 255);">func</span> <span style="color: rgb(163, 21, 21);">request</span>(url <span style="color: rgb(0, 0, 255);">string</span>,body <span style="color: rgb(0, 0, 255);">interface</span>{}){ <span style="color: rgb(0, 0, 255);">for</span> i:=0; i&lt;3; i ++ { result, err = request.POST(url,body) <span style="color: rgb(0, 0, 255);">if</span> err == <span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 0, 255);">break</span> }<span style="color: rgb(0, 0, 255);">else</span> { log.Print() } } } </code></pre><p style="margin-right: auto; margin-left: auto;">如上,当请求 B 服务的 API 失败后,发起最多三次重试。如果三次还是失败,就打印日志,继续执行下或向上层抛出错误。这种方式会带来以下问题</p><ol style="margin-top: 0px; margin-bottom: 1em; margin-left: 40px;"><li style="list-style-type: decimal;"><p style="margin-right: auto; margin-left: auto;">调用 B 服务成功,但由于网络超时原因,当前服务认为其失败了,继续重试,这样 B 服务会产生 2 条一样的数据。</p></li><li style="list-style-type: decimal;"><p style="margin-right: auto; margin-left: auto;">调用 B 服务失败,由于 B 服务不可用,重试 3 次依然失败,当前服务在前面代码中插入到 DB 的一条记录,就变成了脏数据。</p></li><li style="list-style-type: decimal;"><p style="margin-right: auto; margin-left: auto;">重试会增加上游对本次调用的延迟,如果下游负载较大,重试会放大下游服务的压力。</p></li></ol><p style="margin-right: auto; margin-left: auto;">第一个问题:通过让 B 服务的 API 支持幂等性来解决。</p><p style="margin-right: auto; margin-left: auto;">第二个问题:可以通过后台定时脚步去修正数据,但这并不是一个很好的办法。</p><p style="margin-right: auto; margin-left: auto;">第三个问题:这是通过阻塞式重试提高一致性、可用性,必不可少的牺牲。</p><p style="margin-right: auto; margin-left: auto;">阻塞式重试适用于业务对一致性要求不敏感的场景下。如果对数据一致性有要求的话,就必须要引入额外的机制来解决。</p><h2 id="异步队列" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">异步队列</h2><p style="margin-right: auto; margin-left: auto;">在解决方案演化的过程中,引入队列是个比较常见也较好的方式。如下示例:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;">m := db.Insert(sql) err := mq.Publish(<span style="color: rgb(163, 21, 21);">"B-Service-topic"</span>,m) </code></pre><p style="margin-right: auto; margin-left: auto;">在当前服务将数据写入 DB 后,推送一条消息给 MQ,由独立的服务去消费 MQ 处理业务逻辑。和阻塞式重试相比,虽然 MQ 在稳定性上远高于普通的业务服务,但在推送消息到 MQ 中的调用,还是会有失败的可能性,比如网络问题、当前服务宕机等。这样还是会遇到阻塞式重试相同的问题,即 DB 写入成功了,但推送失败了。</p><p style="margin-right: auto; margin-left: auto;">理论上来讲,分布式系统下,涉及多个服务调用的代码都存在这样的情况,在长期运行中,调用失败的情况一定会出现。这也是分布式系统设计的难点之一。</p><h2 id="tcc-补偿事务" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">TCC 补偿事务</h2><p style="margin-right: auto; margin-left: auto;">在对事务有要求,且不方便解耦的情况下,TCC 补偿式事务是个较好的选择。</p><p style="margin-right: auto; margin-left: auto;">TCC 把调用每个服务都分成 2 个阶段、 3 个操作:</p><ul style="margin-top: 0px; margin-bottom: 1em; margin-left: 40px;"><li style="list-style-type: disc;">阶段一、Try 操作:对业务资源做检测、资源预留,比如对库存的检查、预扣。</li><li style="list-style-type: disc;">阶段二、Confirm 操作:提交确认 Try 操作的资源预留。比如把库存预扣更新为扣除。</li><li style="list-style-type: disc;">阶段二、Cancel 操作:Try 操作失败后,释放其预扣的资源。比如把库存预扣的加回去。</li></ul><p style="margin-right: auto; margin-left: auto;">TCC 要求每个服务都实现上面 3 个操作的 API,服务接入 TCC 事务前一次调用就完成的操作,现在需要分 2 阶段完成、三次操作来完成。</p><p style="margin-right: auto; margin-left: auto;">比如一个商城应用需要调用 A 库存服务、B 金额服务、C 积分服务,如下伪代码:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;"> m := db.Insert(sql) aResult, aErr := A.Try(m) bResult, bErr := B.Try(m) cResult, cErr := C.Try(m) <span style="color: rgb(0, 0, 255);">if</span> cErr != <span style="color: rgb(163, 21, 21);">nil</span> { A.Cancel() B.Cancel() C.Cancel() } <span style="color: rgb(0, 0, 255);">else</span> { A.Confirm() B.Confirm() C.Confirm() } </code></pre><p style="margin-right: auto; margin-left: auto;">代码中分别调用 A、B、C 服务 API 检查并保留资源,都返回成功了再提交确认(Confirm)操作;如果 C 服务 Try 操作失败后,则分别调用 A、B、C 的 Cancel API 释放其保留的资源。</p><p style="margin-right: auto; margin-left: auto;">TCC 在业务上解决了分布式系统下,跨多个服务、跨多个数据库的数据一致性问题。但 TCC 方式依然存在一些问题,实际使用中需要注意,包括上面章节提到的调用失败的情况。</p><h3 id="空释放" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">空释放</h3><p style="margin-right: auto; margin-left: auto;">上面代码中如果 C.Try() 是真正调用失败,那下面多余的 C.Cancel() 调用会出现释放并没有锁定资源的行为。这是因为当前服务无法判断调用失败是不是真的锁定 C 资源了。如果不调用,实际上成功了,但由于网络原因返回失败了,这会导致 C 的资源被锁定,一直得不到释放。</p><p style="margin-right: auto; margin-left: auto;">空释放在生产环境经常出现,服务在实现 TCC 事务 API 时,应支持空释放的执行。</p><h3 id="时序" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">时序</h3><p style="margin-right: auto; margin-left: auto;">上面代码中如果 C.Try() 失败,接着调用 C.Cancel() 操作。因为网络原因,有可能会出现 C.Cancel() 请求会先到 C 服务,C.Try() 请求后到,这会导致空释放问题,同时引起 C 的资源被锁定,一直得不到释放。</p><p style="margin-right: auto; margin-left: auto;">所以 C 服务应拒绝释放资源之后的 Try() 操作。具体实现上,可以用唯一事务ID来区分第一次 Try() 还是释放后的 Try()。</p><h3 id="调用失败" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">调用失败</h3><p style="margin-right: auto; margin-left: auto;">Cancel 、Confirm 在调用过程中,还是会存在失败的情况,比如常见的网络原因。</p><p style="margin-right: auto; margin-left: auto;">Cancel() 或 Confirm() 操作失败都会导致资源被锁定,一直得不到释放。这种情况常见解决方案有:</p><ol style="margin-top: 0px; margin-bottom: 1em; margin-left: 40px;"><li style="list-style-type: decimal;"><p style="margin-right: auto; margin-left: auto;">阻塞式重试。但有同样的问题,比如宕机、一直失败的情况。</p></li><li style="list-style-type: decimal;"><p style="margin-right: auto; margin-left: auto;">写入日志、队列,然后有单独的异步服务自动或人工介入处理。但一样会有问题,写日志或队列时,会存在失败的情况。</p></li></ol><p style="margin-right: auto; margin-left: auto;">理论上来讲非原子性、事务性的二段代码,都会存在中间态,有中间态就会有失败的可能性。</p><h2 id="本地消息表" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">本地消息表</h2><p style="margin-right: auto; margin-left: auto;">本地消息表最初是 ebay 提出的,它让本地消息表与业务数据表处于同一个数据库中,这样就能利用本地事务来满足事务特性。</p><p style="margin-right: auto; margin-left: auto;">具体做法是在本地事务中插入业务数据时,也插入一条消息数据。然后在做后续操作,如果其他操作成功,则删除该消息;如果失败则不删除,异步监听这个消息,不断重试。</p><p style="margin-right: auto; margin-left: auto;">本地消息表是一个很好的思路,可以有多种使用方式:</p><h3 id="配合mq" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">配合MQ</h3><p style="margin-right: auto; margin-left: auto;">示例伪代码:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;"> messageTx := tc.NewTransaction(<span style="color: rgb(163, 21, 21);">"order"</span>) messageTxSql := tx.TryPlan(<span style="color: rgb(163, 21, 21);">"content"</span>) m,err := db.InsertTx(sql,messageTxSql) <span style="color: rgb(0, 0, 255);">if</span> err!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 0, 255);">return</span> err } aErr := mq.Publish(<span style="color: rgb(163, 21, 21);">"B-Service-topic"</span>,m) <span style="color: rgb(0, 0, 255);">if</span> aErr!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 128, 0);">// 推送到 MQ 失败</span> messageTx.Confirm() <span style="color: rgb(0, 128, 0);">// 更新消息的状态为 confirm</span> }<span style="color: rgb(0, 0, 255);">else</span> { messageTx.Cancel() <span style="color: rgb(0, 128, 0);">// 删除消息</span> } <span style="color: rgb(0, 128, 0);">// 异步处理 confirm 的消息,继续推送</span> <span style="color: rgb(0, 0, 255);">func</span> <span style="color: rgb(163, 21, 21);">OnMessage</span>(task *Task){ err := mq.Publish(<span style="color: rgb(163, 21, 21);">"B-Service-topic"</span>, task.Value()) <span style="color: rgb(0, 0, 255);">if</span> err==<span style="color: rgb(163, 21, 21);">nil</span> { messageTx.Cancel() } } </code></pre><p style="margin-right: auto; margin-left: auto;">上面代码中其 messageTxSql 是插入本地消息表的一段 SQL :</p><p style="margin-right: auto; margin-left: auto;"><code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">insert into `tcc_async_task` (`uid`,`name`,`value`,`status`) values ('?','?','?','?')</code></p><p style="margin-right: auto; margin-left: auto;">它和业务 SQL 在同一个事务中去执行,要么成功,要么失败。</p><p style="margin-right: auto; margin-left: auto;">成功则推送到队列,推送成功,则调用 messageTx.Cancel() 删除本地消息;推送失败则标记消息为&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">confirm</code>。本地消息表中&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">status</code>&nbsp;有 2 种状态&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">try</code>、<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">confirm</code>, 无论哪种状态在&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">OnMessage</code>&nbsp;都可以监听到,从而发起重试。</p><p style="margin-right: auto; margin-left: auto;">本地事务保障消息和业务一定会写入数据库,此后的执行无论宕机还是网络推送失败,异步监听都可以进行后续处理,从而保障了消息一定会推到 MQ。</p><p style="margin-right: auto; margin-left: auto;">而 MQ 则保障一定会到达消费者服务中,利用 MQ 的 QOS 策略,消费者服务一定能处理,或继续投递到下一个业务队列中,从而保障了事务的完整性。</p><h3 id="配合服务调用" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">配合服务调用</h3><p style="margin-right: auto; margin-left: auto;">示例伪代码:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;"> messageTx := tc.NewTransaction(<span style="color: rgb(163, 21, 21);">"order"</span>) messageTxSql := tx.TryPlan(<span style="color: rgb(163, 21, 21);">"content"</span>) body,err := db.InsertTx(sql,messageTxSql) <span style="color: rgb(0, 0, 255);">if</span> err!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 0, 255);">return</span> err } aErr := request.POST(<span style="color: rgb(163, 21, 21);">"B-Service"</span>,body) <span style="color: rgb(0, 0, 255);">if</span> aErr!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 128, 0);">// 调用 B-Service 失败</span> messageTx.Confirm() <span style="color: rgb(0, 128, 0);">// 更新消息的状态为 confirm</span> }<span style="color: rgb(0, 0, 255);">else</span> { messageTx.Cancel() <span style="color: rgb(0, 128, 0);">// 删除消息</span> } <span style="color: rgb(0, 128, 0);">// 异步处理 confirm 或 try 的消息,继续调用 B-Service </span> <span style="color: rgb(0, 0, 255);">func</span> <span style="color: rgb(163, 21, 21);">OnMessage</span>(task *Task){ <span style="color: rgb(0, 128, 0);">// request.POST("B-Service",body)</span> } </code></pre><p style="margin-right: auto; margin-left: auto;">这是本地消息表 + 调用其他服务的例子,没有 MQ 的引入。这种使用异步重试,并用本地消息表保障消息的可靠性,解决了阻塞式重试带来的问题,在日常开发中比较常见。</p><p style="margin-right: auto; margin-left: auto;">如果本地没有要写 DB 的操作,可以只写入本地消息表,同样在&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">OnMessage</code>中处理:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;">messageTx := tc.NewTransaction(<span style="color: rgb(163, 21, 21);">"order"</span>) messageTx := tx.Try(<span style="color: rgb(163, 21, 21);">"content"</span>) aErr := request.POST(<span style="color: rgb(163, 21, 21);">"B-Service"</span>,body) <span style="color: rgb(0, 128, 0);">// ....</span> </code></pre><h3 id="消息过期" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">消息过期</h3><p style="margin-right: auto; margin-left: auto;">配置本地消息表的&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Try</code>&nbsp;和&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Confirm</code>&nbsp;消息的处理器:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;">TCC.SetTryHandler(OnTryMessage()) TCC.SetConfirmHandler(OnConfirmMessage()) </code></pre><p style="margin-right: auto; margin-left: auto;">在消息处理函数中要判断当前消息任务是否存在过久,比如一直重试了一小时,还是失败,就考虑发邮件、短信、日志告警等方式,让人工介入。</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;"><span style="color: rgb(0, 0, 255);">func</span> <span style="color: rgb(163, 21, 21);">OnConfirmMessage</span>(task *tcc.Task) { <span style="color: rgb(0, 0, 255);">if</span> time.Now().Sub(task.CreatedAt) &gt; time.Hour { err := task.Cancel() <span style="color: rgb(0, 128, 0);">// 删除该消息,停止重试。</span> <span style="color: rgb(0, 128, 0);">// doSomeThing() 告警,人工介入</span> <span style="color: rgb(0, 0, 255);">return</span> } } </code></pre><p style="margin-right: auto; margin-left: auto;">在&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Try</code>&nbsp;处理函数中,还要单独判断当前消息任务是否存在过短,因为&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Try</code>状态的消息,可能才刚刚创建,还没被确认提交或删除。这会和正常业务逻辑的执行重复,意味着成功的调用,也会被重试;为尽量避免这种情况,可以检测消息的创建时间是否很短,短的话可以跳过。</p><p style="margin-right: auto; margin-left: auto;">重试机制必然依赖下游 API 在业务逻辑上的幂等性,虽然不处理也可行,但设计上还是要尽量避免干扰正常的请求。</p><h3 id="独立消息服务" style="font-size: 16px; font-weight: bold; border: 0px; -webkit-font-smoothing: antialiased;">独立消息服务</h3><p style="margin-right: auto; margin-left: auto;">独立消息服务是本地消息表的升级版,把本地消息表抽离成一个独立的服务。所有操作之前先在消息服务添加个消息,后续操作成功则删除消息,失败则提交确认消息。</p><p style="margin-right: auto; margin-left: auto;">然后用异步逻辑去监听消息,做对应的处理,和本地消息表的处理逻辑基本一致。但由于向消息服务添加消息,无法和本地操作放到一个事务里,所以会存在添加消息成功,后续失败,则此时的消息就是个无用消息。</p><p style="margin-right: auto; margin-left: auto;">如下示例场景:</p><pre style="margin-top: 0px; margin-bottom: 0px;"><code style="margin-right: 0px; margin-left: 0px; padding-top: 5px; padding-bottom: 5px; background: rgb(245, 245, 245); color: black; font-family: &quot;Courier New&quot;, sans-serif; border: 1px solid rgb(204, 204, 204); overflow-x: auto; line-height: 1.5;"> err := request.POST(<span style="color: rgb(163, 21, 21);">"Message-Service"</span>,body) <span style="color: rgb(0, 0, 255);">if</span> err!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 0, 255);">return</span> err } aErr := request.POST(<span style="color: rgb(163, 21, 21);">"B-Service"</span>,body) <span style="color: rgb(0, 0, 255);">if</span> aErr!=<span style="color: rgb(163, 21, 21);">nil</span> { <span style="color: rgb(0, 0, 255);">return</span> aErr } </code></pre><p style="margin-right: auto; margin-left: auto;">这个无用的消息,需要消息服务去确认这个消息是否执行成功,没有则删除,有继续执行后续逻辑。相比本地事务表&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">try</code>&nbsp;和&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">confirm</code>&nbsp;,消息服务在前面多了一种状态&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">prepare</code>。</p><h2 id="mq-事务" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">MQ 事务</h2><p style="margin-right: auto; margin-left: auto;">有些 MQ 的实现支持事务,比如 RocketMQ 。MQ 的事务可以看作独立消息服务的一种具体实现,逻辑完全一致。</p><p style="margin-right: auto; margin-left: auto;">所有操作之前先在 MQ 投递个消息,后续操作成功则&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Confirm</code>&nbsp;确认提交消息,失败则<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">Cancel</code>删除消息。MQ 事务也会存在&nbsp;<code style="padding-top: 0px; padding-bottom: 0px; font-family: &quot;Courier New&quot;, sans-serif; font-size: 12px; line-height: 1.8; overflow-x: auto; vertical-align: middle; background-color: rgb(246, 246, 246); color: rgb(232, 62, 140);">prepare</code>状态,需要 MQ 的消费处理逻辑来确认业务是否成功。</p><h2 id="总结" style="margin-top: 20px; font-size: 24px; font-weight: bold; border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: solid; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(204, 204, 204); border-left-color: initial; border-image: initial; -webkit-font-smoothing: antialiased; color: rgb(0, 0, 0);">总结</h2><p style="margin-right: auto; margin-left: auto;">从分布式系统实践中来看,要保障数据一致性的场景,必然要引入额外的机制处理。</p><p style="margin-right: auto; margin-left: auto;">TCC 的优点是作用于业务服务层,不依赖某个具体数据库、不与具体框架耦合、资源锁的粒度比较灵活,非常适用于微服务场景下。缺点是每个服务都要实现 3 个 API,对于业务侵入和改动较大,要处理各种失败异常。开发者很难完整处理各种情况,找个成熟的框架可以大大降低成本,比如阿里的 Fescar。</p><p style="margin-right: auto; margin-left: auto;">本地消息表的优点是简单、不依赖其他服务的改造、可以很好的配合服务调用和 MQ 一起使用,在大多业务场景下都比较实用。缺点是本地数据库多了消息表,和业务表耦合在一起。文中本地消息表方式的示例,来源于作者写的一个库,有兴趣的同学可以参考下&nbsp;<a href="https://github.com/mushroomsir/tcc" target="_blank" style="color: rgb(0, 0, 0); text-decoration-line: underline;">https://github.com/mushroomsir/tcc</a></p><p style="margin-right: auto; margin-left: auto;">MQ 事务和独立消息服务的优点是抽离出一个公共的服务来解决事务问题,避免每个服务都有消息表和服务耦合在一起,增加服务自身的处理复杂性。缺点是支持事务的 MQ 很少;且每次操作前都先调用 API 添加个消息,会增加整体调用的延迟,在绝大多数正常响应的业务场景下,是一种多余的开销。</p></div></div>
赞(0) 收藏(0)  分享
相关标签: 事务
0个回复
  • 消灭零回复
Vaptcha启动中...