ibcadmin 发表于 2019-11-8 09:50:19

关于分布式事务的实现梳理

<p><strong>关于分布式事故的实现梳理</strong></p>
<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>场景形貌</strong></p>
<p style="width: 100%; line-height: 30px;">   在实际开发过程中,往往会碰到微服务架构中(数据分区存储),用户的一个操作,会筹划到多个模块的数据落地大概更新查找,并且每个模块数据都是存储在差别的数据库,并且业务要求还必要确保操作结果的同等性。比如,用户在下单时:首选必要落地订单数据,其次,必要落地:账单数据、日志数据、大概库存更新等等操作。起首我们想到的办理方式就是事故来实现,由于在差别库,以是必要涉及到分布式事故。</p>
<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>办理方案</strong></p>
<p style="width: 100%; line-height: 30px;">   为了到达上述要求,在实现上根据我的履历大概有如下3种实现方式:</p>
<p style="width: 100%; line-height: 30px;">  其一、分布式事故</p>
<p style="width: 100%; line-height: 30px;">    分布式事故就是接纳微软进步的分布式事故机制实现,在实现服从上不是很抱负,并且也不是符合微服务筹划的单一功能原则,以是不是很发起使用。</p>
<p style="width: 100%; line-height: 30px;">  其二、消息队列</p>
<p style="width: 100%; line-height: 30px;">    消息队列是现在使用的比力多的办理方案,通过一些消息队列中心件, 实现逻辑解耦,异步实现,相应服从也大大提拔。</p>
<p style="width: 100%; line-height: 30px;">  其三、异步作业</p>
<p style="width: 100%; line-height: 30px;">    异步作业的实现思绪和消息队列雷同,都是对操作的步调的解耦,异步实现,但是在处理处罚上有肯定的耽误性,由于异步作业是周期性的实验,但是异步作业也是对消息队里的一个保障和增补。</p>
<p style="width: 100%; line-height: 30px;">    在实际使用过程中,一样平常都是消息队列和异步作业配套实现,当消息队列出现问题,异步作业能正常的把流程走完。</p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  分布式事故</strong></p>
<p style="width: 100%; line-height: 30px;">   在先容分布式事故时,分两部门来先容:sql分布式事故、ADO.NET分布式事故。</p>
<p style="background-color: #38f; color: white; width: 100%; height: 50px; line-height: 50px; padding-left: 10px;">  sql分布式事故</p>
<p style="width: 100%; line-height: 30px;">  分布式事故的实现,起首总结一下sql分布式事故的实现,重要实用于存储过程大概方法函数中。</p>
<p style="width: 100%; line-height: 30px;">   sql分布式事故的关键词为:distributed,分布式事故在使用前,必要做一下几点的情况准备:</p>
<p style="width: 100%; line-height: 30px;">  <strong>分布式事故必要的前期情况准备:</strong></p>
<p style="margin-left: 30px;">  在控制面板--->管理工具--->服务 中,开启Distributed Transaction Coordinator 服务。</p>
<p style="margin-left: 30px;">  a、控制面板->管理工具->组件服务->盘算机->我的电脑->右键->属性<br />  b、选择MSDTC页, 确认"使用本地和谐器"<br />  c、点击下方"安全设置"按钮<br />  d、勾选: "答应网络DTC访问","答应长途客户端","答应入站","答应出站","不要求举行身份验证".</p>
<p style="margin-left: 30px;">  e、对于数据库服务器端, 可选择"要求对呼唤方验证"<br />  f、勾选:"启用事故Internet协议(TIP)事故"。<br />  g、在两边防火墙中增长MSDTC.exe破例<br />  可用下令行: netsh firewall set allowedprogram %windir%/system32/msdtc.exe MSDTC enable</p>
<p style="margin-left: 30px;"> </p>
<p style="margin-left: 30px;"><strong>sql分布式事故的使用实例:</strong></p>

use ecshop;
go
<br />set XACT_ABORT ON<br /><br />--开启分布式事故
begin distributed tran tranInsetName

begin

----必要实验的sql语句;
    insert into ecshop..TEST_name values(8,8)
    insert into ecshopTest..TEST_name values(9,null)
    insert into ecshopTest..TEST_name values(8,8)
commit tran tranInsetName
end
go

<p> </p>
<p style="background-color: #38f; color: white; width: 100%; height: 50px; line-height: 50px; padding-left: 10px;">  ADO.NET中分布式事故</p>
<p style="width: 100%; line-height: 30px;">    <strong>下面在总结一下ADO.NET中分布式事故的使用:</strong></p>
<p style="width: 100%; line-height: 30px;">    ADO.NET分布式事故关键词为:TransactionScope</p>
<p style="width: 100%; line-height: 30px;">    ADO.NET分布式事故必要引用定名空间:using System.Transactions</p>
<p style="width: 100%; line-height: 30px;">    <strong>起首必要相识ADO.NET分布式事故的级别</strong></p>
<p>    Chaos:无法改写隔离级别更高的事故中的挂起的更改。</p>
<p>    ReadCommitted:不可以在事故期间读取可变数据,但是可以修改它。</p>
<p>    ReadUncommitted:可以在事故期间读取和修改可变数据。</p>
<p>    RepeatableRead:可以在事故期间读取可变数据,但是不可以修改。可以在事故期间添加新数据。</p>
<p>    Serializable:可以在事故期间读取可变数据,但是不可以修改,也不可以添加任何新数据---默认级别。</p>
<p>    Snapshot:可以读取可变数据。在事故修改数据之前,它验证在它最初读取数据之后另一个事故是否更改过这些数据。如果数据已被更新,则会引发错误。如许使事故可获取先条件交的数据值。</p>
<p>    Unspecified:正在使用与指定隔离级别差别的隔离级别,但是无法确定该级别。如果设置了此值,则会引发非常。</p>
<p>    实例代码:</p>
<p>    </p>

      //// 事故附件消息
            TransactionOptions transactionOption = new TransactionOptions();
            //设置事故隔离级别
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            // 设置事故超时时间为60秒
            transactionOption.Timeout = new TimeSpan(0, 0, 60);

            //启动一个分布式事故
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))
            {
                ///// 处理处罚一个库操作
                using (SqlConnection conn = new SqlConnection(sqlConn))
                {
                  conn.Open();
                  using (SqlCommand cmd = conn.CreateCommand())
                  {

                        cmd.CommandText = "insert into TEST_name values(25,25);insert into TEST_name values(26,null);";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "insert into TEST_name values(26,null);";
                        cmd.ExecuteNonQuery();
                  }
                }

                ///// 创建一个新的毗连,处理处罚别的一个库操作
                using (SqlConnection conn = new SqlConnection(sqlConn))
                {
                  conn.Open();
                  using (SqlCommand cmd = conn.CreateCommand())
                  {

                        cmd.CommandText = "insert into TEST_name values(25,25);insert into TEST_name values(26,null);";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "insert into TEST_name values(26,null);";
                        cmd.ExecuteNonQuery();
                  }
                }
            }

<p>   分布式事故在实验服从上低,在实际项目中不怎么使用,尤其是微服务项目。在微服务项目中,重要通过消息队列变相的实现事故,确保操作结果的同等性</p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  消息队列</strong></p>
<p style="width: 100%; line-height: 30px;">   消息队列在实际工作中使用场景照旧许多的,重要目的是实现步调解耦、消峰、高并发。在这只简单整理一下消息队列在分布式事故中的使用,</p>
<p style="width: 100%; line-height: 30px;">  消息队列在分布式事故中使用逻辑大概是:主流程天生完成后,天生一个消息,直接返回结果给用户,通过消息中心件,告诉后续流程的斲丧者,举行各自的后续流程逻辑处理处罚、</p>
<p style="width: 100%; line-height: 30px;">  比如:以一个实际的电商中用户订单付出乐成为例,假设订单付出乐成后起首必要更新订单状态,别的后续流程包罗:落地账单数据、落地分佣数据,假设账单数据和分佣数据没有数据关系,可并行实验</p>
<p style="width: 100%; line-height: 30px;">  那么实现逻辑是:</p>
<p style="width: 100%; line-height: 30px;">    消息生产者:付出乐成,更新订单状态-->发送一个消息到消息队列中心件(广播)</p>
<p style="width: 100%; line-height: 30px;">    消息斲丧者:此处有两个消息斲丧订阅对象,账单落地、分佣数据落地。两个消息斲丧者都会收到一条消息,并做各自的数据落地处理处罚</p>
<p style="width: 100%; line-height: 30px;">  消息队里,在体系架构上,大概用户体验上都有是一个很不错的选择,但是在实际工作中,仅仅使用消息队里也不是完成的办理方案,由于消息队列也有肯能出现宕机大概数据丢失,导致业务逻辑制止,以是在实际工作中,一样平常还会借助一个辅助步调(异步作业),实现对消息队里的增补的加固</p>
<p style="background-color: deepskyblue; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>  异步作业</strong></p>
<p style="width: 100%; line-height: 30px;">   异步作业的实现思绪就是,步调定期的实验某一些数据流程操作,比如:账单数据落地异步作业小步调,查找到订单付出乐成,但是账单为乐成,则落地账单数据</p>
<p style="width: 100%; line-height: 30px;">  在实现上,保举使用:Quartz开源的异步作业框架,使用起来很不错。</p>
<p style="width: 100%; line-height: 30px;">  具体Quartz的实现方式,保举一个博客:<a href="https://www.cnblogs.com/ll409546297/p/7793877.html">https://www.cnblogs.com/ll409546297/p/7793877.html</a></p>
<p style="width: 100%; line-height: 30px;">  异步作业的宿主有:控制台步调、窗体步调、IIS、Windows服务</p>
<p style="width: 100%; line-height: 30px;">  在实际开发过程中,保举使用windows服务,方便控制管理</p>
<p style="background-color: darkcyan; color: white; width: 100%; height: 50px; line-height: 50px;"><strong>总结</strong></p>
<p style="width: 100%; line-height: 30px;">   上面临分布式事故做了简单的先容,如果有说的不对的地方勿喷,望多多辅导学习。</p>
<p style="width: 100%; line-height: 30px;">  通过上面的先容,我们也知道在实际项目中的使用选择,我照旧发起接纳:消息队列+异步作业 来确保体系的高可用性</p>
页: [1]
查看完整版本: 关于分布式事务的实现梳理