blog/cn/material/seata/index.html (102 lines of code) (raw):
<!DOCTYPE html>
<html lang="en-us">
<head>
<style>
a {
word-wrap: break-word;
}
</style>
</head>
<body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Apache ShardingSphere整合Seata AT分布式事务 · ShardingSphere - 博客</title>
<link rel="stylesheet" href="https://shardingsphere.apache.org/blog/css/style.css">
<link rel="stylesheet" href="https://shardingsphere.apache.org/blog/css/fonts.css">
<link rel="icon" type="image/png" sizes="32x32" href="https://shardingsphere.apache.org/blog/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://shardingsphere.apache.org/blog/images/favicon-16x16.png">
<link rel="apple-touch-icon" sizes="180x180" href="https://shardingsphere.apache.org/blog/images/apple-touch-icon.png">
<link href="" rel="alternate" type="application/rss+xml" title="ShardingSphere - 博客" />
</head>
<div style="position: sticky; position: -webkit-sticky; top: 0px; background-color: rgba(255, 255, 255, 0.888);"> <nav class="nav">
<div class="nav-container">
<a href="https://shardingsphere.apache.org/blog/">
<h1 class="nav-title">ShardingSphere - 博客</h1>
</a>
<span style="padding-left: 65px;">
<li>
<a href="https://shardingsphere.apache.org/blog/cn/material/">
<h3>文章</h3>
</a>
</li>
<li>
<a href="https://shardingsphere.apache.org/blog/cn/videos/">
<h3>视频</h3>
</a>
</li>
</span>
<span style="position: relative; left: 75px;
background: rgba(0, 0, 0, 0.664);
font-weight: bold;
padding:0.1rem;
padding-left:15px;
padding-right:15px;
padding-top: 2px;
padding-bottom: 5px;
border-radius:10px;">
<a style="color: white;" href="https://shardingsphere.apache.org/blog/en/material/seata/">English</a>
</span>
</div>
</div>
</nav></div>
<main>
<div class="post">
<h1 class="post-title">Apache ShardingSphere整合Seata AT分布式事务</h1>
<div class="post-info">
</div>
<h3 id="背景知识">背景知识</h3>
<p>Seata是阿里集团和蚂蚁金服联合打造的分布式事务框架,目前版本包含了AT事务和TCC事务。其中AT事务的目标是在微服务架构下,提供增量的事务ACID语意,让用户像使用本地事务一样,使用分布式事务,核心理念同ShardingSphere一脉相承。</p>
<p>Github: <a href="https://github.com/seata/seata">https://github.com/seata/seata</a></p>
<h3 id="seata-at模型">Seata AT模型</h3>
<p>Seata AT事务模型包含TM(事务管理器),RM(资源管理器),TC(事务协调器)。</p>
<p>其中TC是一个独立的服务需要单独部署,TM和RM以jar包的方式同业务应用部署在一起,它们同TC建立长连接,在整个事务生命周期内,保持RPC通信。</p>
<p>其中全局事务的发起方作为TM,全局事务的参与者作为RM ; TM负责全局事务的begin和commit/rollback,RM负责分支事务的执行和commit/rollback。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata1.jpg" alt=""></p>
<h3 id="shardingsphere分布式事务spi">ShardingSphere分布式事务SPI</h3>
<p>ShardingSphere提供了一套接入分布式事务的SPI,设计的目标是保证数据分片后,事务的ACID语意。分布式事务的实现目前主要包含两阶段的XA和BASE柔性事务。Seata AT事务作为BASE柔性事务的一种实现,可以无缝接入到ShardingSphere生态中。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata2.jpg" alt=""></p>
<p>两阶段XA事务方面,我们已经整合了Atomikos,Narayana,Bitronix事务管理器,XA事务底层依赖具体的数据库厂商对XA两阶段提交协议的支持,通常XA协议通过在Prepare和Commit阶段进行2PL(2阶段锁),保证了分布式事务的ACID,通常适用于短事务及非云化环境(云化环境下一次IO操作大概需要20ms,两阶段锁会锁住资源长达40ms,因此事务的TPS会降到25/s左右,非云化环境通常一次IO只需几毫秒,因此锁热点数据的时间相对较低)[1]。</p>
<p>BASE柔性事务方面,目前我们已经完成了对ServiceComb Saga的整合,Saga通过一阶段提交+补偿的方式提高了整体事务的性能,其中补偿的方式同Seata大致相同,即对分片后的物理SQL进行revert来生成补偿的SQL,但Saga模型在理论上不支持隔离级别,适用于对性能要求较高,对一致性要求比较低的业务。Seata AT事务在一阶段提交+补偿的基础上,通过TC的全局锁实现了RC隔离级别的支持,是介于XA和Saga之间的另一种实现。消息柔性事务方面,也欢迎大家参考我们的SPI提供整合的方案。</p>
<h3 id="整合方案">整合方案</h3>
<p>整合Seata AT事务时,需要把TM,RM,TC的模型融入到ShardingSphere 分布式事务的SPI的生态中。在数据库资源上,Seata通过对接DataSource接口,让JDBC操作可以同TC进行RPC通信。同样,ShardingSphere也是面向DataSource接口对用户配置的物理DataSource进行了聚合,因此把物理DataSource二次包装为Seata的DataSource后,就可以把Seata AT事务融入到ShardingSphere的分片中。</p>
<p>在Seata模型中,全局事务的上下文存放在线程变量中,通过扩展服务间的transport,可以完成线程变量的传递,分支事务通过线程变量判断是否加入到整个Seata全局事务中。而ShardingSphere的分片执行引擎通常是按多线程执行,因此整合Seata AT事务时,需要扩展主线程和子线程的事务上下文传递,这同服务间的上下文传递思路完全相同。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata3.jpg" alt=""></p>
<h3 id="quick-start">Quick Start</h3>
<p>我们已经实现了base-seata-raw-jdbc-example,大家可以自行进行尝试。</p>
<p><a href="https://github.com/apache/incubator-shardingsphere-example/tree/dev/sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example">https://github.com/apache/incubator-shardingsphere-example/tree/dev/sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example</a></p>
<p>操作手册:</p>
<p>1.按照seata-work-shop中的步骤,下载并启动seata server。</p>
<p><a href="https://github.com/seata/seata-workshop">https://github.com/seata/seata-workshop</a></p>
<p>参考 Step6 和 Step7即可</p>
<p>2.在每一个分片数据库实例中执行resources/sql/undo_log.sql脚本,创建undo_log表</p>
<p>3.Run YamlConfigurationTransactionExample.java</p>
<h3 id="待优化项">待优化项</h3>
<p>Seata AT事务在Revert SQL时,需要对ShardingSphere分片后的物理SQL进行二次的解析,这里我们需要设计一个SPI,避免SQL二次解析的性能损耗。</p>
<p>参考论文</p>
<p>[1]: Transactions for Distributed Actors in the Cloud</p>
<p><a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/10/EldeebBernstein-TransactionalActors-MSR-TR-1.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/10/EldeebBernstein-TransactionalActors-MSR-TR-1.pdf</a></p>
</div>
<div class="pagination">
<a href="https://shardingsphere.apache.org/blog/cn/material/cncf/" class="left arrow">←</a>
<a href="https://shardingsphere.apache.org/blog/cn/videos/newfeature/" class="right arrow">→</a>
<a href="#" class="top">Top</a>
</div>
</main>
<footer>
<span>
Copyright © 2018-2020, <a href="https://shardingsphere.apache.org/blog/">Apache ShardingSphere</a>, ShardingSphere, Apache, the Apache feather logo, and the Apache ShardingSphere project logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.
</span>
</footer>
</body>
</html>