首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF技术剖析之三十二:一步步创建一个完整的分布式事务应用

WCF技术剖析之三十二:一步步创建一个完整的分布式事务应用

作者头像
蒋金楠
发布2018-01-16 17:49:47
6200
发布2018-01-16 17:49:47
举报
文章被收录于专栏:大内老A大内老A

在完成了对于WCF事务编程(《上篇》、《中篇》、《下篇》)的介绍后,本篇文章将提供一个完整的分布式事务的WCF服务应用,通过本例,读者不仅仅会了解到如何编程实现事务型服务,还会获得其他相关的知识,比如DTC和AS-AT的配置等。本例还是沿用贯通本章的应用场景:银行转帐。我们将会创建一个BankingService服务,并将其中的转帐操作定义成事务型操作。我们先从物理部署的角度来了解一下BankingService服务,以及需要实现怎样的分布式事务。

一、从部署的角度看分布式事务

既然是实现分布式事务,那么事务会跨越多台机器。简单起见,我使用两台机器来模拟。有条件的读者可以在自己的局域网中进行练习,如果你没有局域网可用,你可以使用虚拟机来模拟局域网。假设两台机器名分别是Foo和Bar,整个应用的物理拓扑结构如图1所示。

image
image

图1 BankingService物理部署拓扑

BankingService和客户端部署与主机Foo,定义在BankingService的转账的两个子操作“提取(Withdraw)”和“存储(Deposit)”通过调用部署于主机Bar的同名服务(WithdrawService和DepositService)实现。而WithdrawService和DepositService最终实现对存储于数据库(这里是SQL Server)的数据进行修改,MSSQL部署与主机Bar中。实际上,整个应用主要涉及到对三个服务(BankingService、WithdrawService和DepositService)的实现,我们先来看看服务契约和服务的实现。

步骤1:服务契约和服务的实现

我们仍然采用契约共享的方式将服务契约定义在单独的项目之中,共服务端和客户端共享。涉及到的三个服务对应的服务契约定义如下,事务型操作的TransactionFlow选项被设置为Allwed(默认值)。

IBankingService:

   1: using System.ServiceModel;
   2: namespace Artech.TransactionalService.Service.Interface
   3: {
   4:     [ServiceContract(Namespace = "http://www.artech.com/banking/")]
   5:     public interface IBankingService
   6:     {       
   7:         [OperationContract]
   8:         [TransactionFlow(TransactionFlowOption.Allowed)]
   9:         void Transfer(string fromAccountId, string toAccountId, double amount);
  10:     }
  11: }

IWithdrawService:

   1: using System.ServiceModel;
   2: namespace Artech.TransactionalService.Service.Interface
   3: {
   4:     [ServiceContract(Namespace = "http://www.artech.com/banking/")]
   5:     public interface IWithdrawService
   6:     {
   7:         [OperationContract]
   8:         [TransactionFlow(TransactionFlowOption.Allowed)]
   9:         void Withdraw(string accountId, double amount);
  10:     }
  11: }

IDepositService:

   1: using System.ServiceModel;
   2: namespace Artech.TransactionalService.Service.Interface
   3: {
   4:     [ServiceContract(Namespace="http://www.artech.com/banking/")]
   5:     public interface IDepositService
   6:     {
   7:         [OperationContract]
   8:         [TransactionFlow(TransactionFlowOption.Allowed)]
   9:         void Deposit(string accountId, double amount);
  10:     }
  11: }

实现了服务操作的IWithdrawService和IDepositService的WithdrawService和DepositService分别实现基于给定银行账户的提取和存储操作。限于篇幅的问题,具体对数据库相应数据的更新操作就不再这里一一介绍了。下面是WithdrawService和DepositService的定义,由于不管是单独被调用,还是作为转帐的一个子操作,Withdraw和Deposit操作均需要在一个事务中执行,所以我们需要通过应用OperationBehaviorAttribute将TransactionScopeRequired属性设为True。

WithdrawService:

   1: using System.ServiceModel;
   2: using Artech.TransactionalService.Service.Interface;
   3: namespace Artech.TransactionalService.Service
   4: {
   5:     public class WithdrawService : IWithdrawService
   6:     {
   7:         [OperationBehavior(TransactionScopeRequired = true)]
   8:         public void Withdraw(string accountId, double amount)
   9:         {
  10:             //省略实现
  11:         }
  12:     }
  13: }

DepositService:

   1: using System.ServiceModel;
   2: using Artech.TransactionalService.Service.Interface;
   3: namespace Artech.TransactionalService.Service
   4: {
   5:       public class DepositService : IDepositService
   6:     {       
   7:         [OperationBehavior(TransactionScopeRequired = true)]
   8:         public void Deposit(string accountId, double amount)
   9:         {
  10:              //省略实现
  11:         }
  12:     }
  13: }

定义在BankingService的Transfer操作就是调用上述的两个服务,由于服务调用设置到对服务代理的关闭以及异常的处理(相关的内容在《WCF技术剖析(卷1)》的第8章有详细的介绍),为了实现代码的复用,我定义了一个静态的ServiceInvoker类。ServiceInvoker定义如下,泛型方法Invoke<TChannel>用于进行服务的调用,并实现了服务代理的关闭(Close),以及异常抛出是对服务代理的中止(Abort)。Invoke<TChannel>的泛型参数类型为服务契约类型,方法接受两个操作,委托action代表服务调用操作,endpointConfigurationName表示配置的终结点名称。

   1: using System;
   2: using System.ServiceModel;
   3: namespace Artech.TransactionalService.Service.Interface
   4: {
   5:     public static class ServiceInvoker
   6:     {
   7:         public static void Invoke<TChannel>(Action<TChannel> action, string endpointConfigurationName)
   8:         {
   9:             Guard.ArgumentNotNull(action, "action");
  10:             Guard.ArgumentNotNullOrEmpty(endpointConfigurationName, "endpointConfigurationName");
  11:  
  12:             using (ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(endpointConfigurationName))
  13:             {
  14:                 TChannel channel = channelFactory.CreateChannel();
  15:                 using (channel as IDisposable)
  16:                 {
  17:                     try
  18:                     {
  19:                         action(channel);
  20:                     }
  21:                     catch (TimeoutException)
  22:                     {
  23:                         (channel as ICommunicationObject).Abort();
  24:                         throw;
  25:                     }
  26:                     catch (CommunicationException)
  27:                     {
  28:                         (channel as ICommunicationObject).Abort();
  29:                         throw;
  30:                     }
  31:                 }
  32:             }
  33:         }
  34:     }
  35: }

那么,借助于ServiceInvoker,BankingService的定义就很简单了。对于Transfer操作,我们依然通过OperationBehaviorAttribute特性将TransactionScopeRequired设置成True。

   1: using System;
   2: using System.Collections.Generic;
   3: using System.ServiceModel;
   4: using Artech.TransactionalService.Service.Interface;
   5: namespace Artech.TransactionalService.Service
   6: {
   7:     public class BankingService : IBankingService
   8:     {        
   9:         [OperationBehavior(TransactionScopeRequired = true)]
  10:         public void Transfer(string fromAccountId, string toAccountId, double amount)
  11:         {
  12:             ServiceInvoker.Invoke<IWithdrawService>(proxy => proxy.Withdraw(fromAccountId, amount), "withdrawservice");
  13:             ServiceInvoker.Invoke<IDepositService>(proxy => proxy.Deposit(toAccountId, amount), "depositservice");
  14:         }
  15:     }
  16: }

步骤2:部署服务

BankingService和依赖的WithdrawService与DepositService已经定义好了,现在我们需要对它们进行部署。本实例采用基于IIS的服务寄宿方式,在进行部署之前需要为三个服务创建.svc文件。在这里,我.svc文件命名为与服务类型相同的名称(BankingService.svc、WithdrawService.svc和DepositService.svc)。关于.svc文件的具体定义,在这里就不再重复介绍了,对此不了解的读者,可以参阅《WCF技术剖析(卷1)》第7章关于IIS服务寄宿部分。

我们需要分别在主机Foo和Bar上创建两个IIS虚拟目录(假设名称为Banking),并将定义服务契约和服务类型的两个程序集拷贝到Foo\Banking\Bin和Bar\Banking\Bin。然后再将BankingService.svc拷贝到Foo\ Banking下,将WithdrawService.svc和DepositService.svc拷贝到Bar\Banking下。

最后,我们需要创建两个Web.config,分别拷贝到Foo\Banking\Bin和Bar\Banking下面。下面两段XML代表两个Web.config的配置。

Foo\Banking\Web.config:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>  
   3:     <system.serviceModel>
   4:         <bindings>
   5:           <customBinding>
   6:             <binding name="transactionalBinding">
   7:               <textMessageEncoding />
   8:               <transactionFlow/>
   9:               <httpTransport/>
  10:             </binding>
  11:           </customBinding>
  12:         </bindings>
  13:         <services>
  14:             <service name="Artech.TransactionalService.Service.WithdrawService">
  15:                 <endpoint  binding="customBinding" bindingConfiguration="transactionalBinding" contract="Artech.TransactionalService.Service.Interface.IWithdrawService" />
  16:             </service>
  17:           <service name="Artech.TransactionalService.Service.DepositService">
  18:             <endpoint binding="customBinding" bindingConfiguration="transactionalBinding" contract="Artech.TransactionalService.Service.Interface.IDepositService" />
  19:           </service>
  20:         </services>     
  21:     </system.serviceModel>
  22: </configuration>

Bar\Banking\Web.config:


   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>  
   3:   <system.serviceModel>
   4:     <bindings>
   5:       <customBinding>
   6:         <binding name="transactionalBinding">
   7:           <textMessageEncoding />
   8:           <transactionFlow/>
   9:           <httpTransport/>
  10:         </binding>
  11:       </customBinding>
  12:     </bindings>
  13:     <services>
  14:       <service name="Artech.TransactionalService.Service.BankingService">
  15:         <endpoint  binding="customBinding" bindingConfiguration="transactionalBinding" contract="Artech.TransactionalService.Service.Interface.IBankingService" />
  16:       </service>
  17:     </services>
  18:     <client>
  19:       <endpoint name="withdrawservice" address="http://Bar/banking/withdrawservice.svc"  binding="customBinding" bindingConfiguration="transactionalBinding"
  20:             contract="Artech.TransactionalService.Service.Interface.IWithdrawService" />
  21:       <endpoint name="depositservice" address="http://Bar/banking/depositservice.svc"  binding="customBinding" bindingConfiguration="transactionalBinding"
  22:           contract="Artech.TransactionalService.Service.Interface.IDepositService" />
  23:     </client>
  24:   </system.serviceModel>
  25: </configuration>

步骤3:调用BankingService

现在我们已经部署好了定义的三个服务,现在我们可以调用它们实施转帐处理了。我们可以像调用普通服务一样调用BankingService,无须考虑事务的问题。因为我们通过OperationBehaviorAttribute特性将BankingService的Transfer操作的TransactionScopeRequired设置成True,这会确保整个操作的执行是在一个事务中进行(可能是流入的事务,也可能是重新创建的事务)。下面进行转帐处理的客户端代码和配置。

   1: string fromAccountId = "123456789";
   2: string tooAccountId = "987654321";
   3: double amount = 1000;
   4: ServiceInvoker.Invoke<IBankingService>(proxy => proxy.Transfer(fromAccountId, tooAccountId, amount), "bankingservice");

配置:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <system.serviceModel>
   4:     <bindings>
   5:       <customBinding>
   6:         <binding name="transactionalBinding">
   7:           <textMessageEncoding />
   8:           <transactionFlow />
   9:           <httpTransport/>
  10:         </binding>
  11:       </customBinding>
  12:     </bindings>   
  13:     <client>
  14:       <endpoint address="http://Foo/banking/bankingservice.svc"
  15:        binding="customBinding" bindingConfiguration="transactionalBinding" contract="Artech.TransactionalService.Service.Interface.IBankingService"
  16:        name="bankingservice" />      
  17:     </client>
  18:   </system.serviceModel>
  19: </configuration>

实际上,由于定义在BankingService的Transfer操作完全是通过调用WithdrawService和DepositService实现的,我们也可以绕过BankingService直接调用这两个服务实现转账的处理。为此我们需要在配置中添加调用WithdrawService和DepositService的终结点:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <system.serviceModel>
   4:     ......
   5:     <client>
   6:       <endpoint name="withdrawservice" address="http://Bar/banking/withdrawservice.svc"  binding="customBinding" bindingConfiguration="transactionalBinding"            contract="Artech.TransactionalService.Service.Interface.IWithdrawService" />
   7:       <endpoint name="depositservice" address="http://Bar/banking/depositservice.svc"  binding="customBinding" bindingConfiguration="transactionalBinding"          contract="Artech.TransactionalService.Service.Interface.IDepositService" />
   8:     </client>
   9:   </system.serviceModel>
  10: </configuration>

由于整个转帐的操作必须纳入到一个事务中进行,并且客户端主动发起对WithdrawService和DepositService两个服务的调用,所以客户端是事物的初始化者。为此,我们需要将对这两个服务的调用放到一个TransactionScope中进行,相应的代码如下所示:

   1: string fromAccountId = "123456789";
   2: string tooAccountId = "987654321";
   3: double amount = 1000;
   4: using (TransactionScope transactionScope = new TransactionScope())
   5: {
   6:     ServiceInvoker.Invoke<IWithdrawService>(proxy => proxy.Withdraw(fromAccountId, 100), "withdrawservice");
   7:     ServiceInvoker.Invoke<IDepositService>(proxy => proxy.Deposit("B001", 100), "depositservice");
   8:     transactionScope.Complete();
   9: }

对于上面两种不同的实现方式,实际上都已经涉及到了分布式事务的应用,所以需要借助于DTC。如果读者在运行该实例的时候,两个主机的DTC没有进行合理的设置,将不会成功运行,现在我们简单介绍一下如何进行DTC的设置。

步骤4:设置DTC

通过“控制面板”|“管理工具”|“组件服务”打开组件服务的对话框。然后右击“组件服务”\“计算机”\“我的电脑”结点,并在的上下文菜单中选择“属性”,会弹出“我的电脑 属性”对话框。在该对话框的“MSDTC”Tab页,选择默认的协调器,一般地我们选择“使用本地协调器”选项。

当我们选择使用本地协调器作为默认的DTC之后,在组件服务对话框的“组件服务”\“计算机”\“我的电脑”\“Distributed Transaction Coordinator”结点下面会出现“本地DTC”结点。右击该节点选择“属性”选项,会弹出如图2所示的“本地DTC属性”。你可以对DTC的跟踪(Trace)方式、日志记录、安全和WS-AT进行相应的设置。在这里要是DTC在本实例中可用,重点是对“安全”进行正确的设置。图2是我机器上的设置,限于篇幅问题,我不能对每一个选项进行详细说明,有兴趣的读者相信很容易从网上找到相关的参考资料。此外,如果在已经对DTC作了相应设置之后还出现DTC的问题,你可以看看DTC通信是否被防火墙屏蔽。

image
image

图2 本地DTC设置对话框

步骤5:采用WS-AT协议

在本例中,所有终结点采用的绑定类型均是包含有TransactionFlowBindingElement的CustomBinding。通过前面的介绍我们知道,默认采用的事务处理协议是OleTx,如果我们希望采用WS-AT协议,我们需要通过配置将协议类型改成WSAtomicTransactionOctober2004或者WSAtomicTransaction11。下面的配置中,我们将实例中使用的绑定支持的事务处理协议设置成WSAtomicTransaction11,使之采用WS-AT协议进行事务处理。

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <system.serviceModel>
   4:     <bindings>
   5:       <customBinding>
   6:         <binding name="TransactionalBinding">
   7:           <textMessageEncoding />
   8:           <transactionFlow transactionProtocol="WSAtomicTransaction11" />
   9:           <httpTransport/>
  10:         </binding>
  11:       </customBinding>
  12:     </bindings>
  13:   </system.serviceModel>
  14: </configuration>

DTC通过基于HTTPS的通信方式为WS-AT(1.0和1.1)提供实现,我们只知道HTTPS通过SSL实现传输安全,而SSL需要将相应的证书(Certificate)绑定在HTTPS站点上面。为了让DTC支持WS-AT,我们需要对DTC进行相关的配置。

首先我们需要为参与到事务的两台主机创建相对应的证书,在这里我们直接采用Makecert.exe这个X.509证书生成工具。我们可以通过下面两个命令行创建两张X.509证书,它们分别代表Foo和Bar两台主机,读者在做练习这个例子时,需要换成自己相应的机器名称。关于Makecert.exe的命令行选项的含义,可以参考MSDN(http://msdn.microsoft.com/zh-cn/library/bfsktky3%28VS.80%29.aspx)

MakeCert –n CN=Foo –pe –sky exchange –sr LocalMachine –ss MY
MakeCert –n CN=Bar –pe –sky exchange –sr LocalMachine –ss MY

在主机Foo运行上面的命令行后,会创建两张个X.509证书并将其存入“本地计算机\个人(LocalMachine\MY)”存储中。你需要通过MMC将其导出成证书文件,并将其导入到主机Foo的“本地计算机\受信任的根证书颁发机构(LocalMachine\Root)”存储中,以及主机Bar的“本地计算机\个人(LocalMachine\MY)”和“本地计算机\受信任的根证书颁发机构(LocalMachine\Root)”存储中。需要注意的是,在到处的时候务必选择“导出私钥”选项,因为不包含私钥的正式是不能和SSL站点绑定的。

DTC的WS-AT可以借助WS-AT配置工具wsatConfig .exe以命令行的方式进行设置,该工具位于"%WINDIR%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation"目录下面。关于wsatConfig.exe配置工具的用法,可以参考MSDN相关文档:http://msdn.microsoft.com/zh-cn/library/ms732007.aspx。在这里,我需要介绍的是一种可视化的WS-AT配置方式。

图2所示的DTC设置对话框中,有一个WS-AT Tab页,通过它我们可以很容易地进行WS-AT的相关配置。不过,在默认的情况下,这个Tab页是不存在的。我们需要借助Regasm.exe这个程序集注册工具以命令行的形式对包含有WS-AT配置界面的程序集进行注册,该程序集位于Windows SDK目录下:%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0\Bin或者%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\Bin。当运行下面的命令行后,DTC设置对话框中将会出现WS-AT Tab页了。

regasm.exe /codebase WsatUI.dll 

选择WS-AT Tab,你将会看到如图3所示的界面。然后,我们针对主机Foo和Bar分别进行如下的设置,使之建立相互信任关系:

  • Foo:选择证书Foo(CN=Foo)和Bar(CN=Bar)分别作为终结点证书(Endpoint certificate)和授权证书(Authorized certificates);
  • Bar:选择证书Bar(CN=Bar)和Foo(CN=Foo)分别作为终结点证书(Endpoint certificate)和授权证书(Authorized certificates)。
image
image

图3 WS-AT设置界面

对于本实例来说,既是你将绑定的事务处理协议设置成WSAtomicTransactionOctober2004或者WSAtomicTransaction11,并对WS-AT进行了正确的设置,但是依然采用的是OleTx协议。这是由于DTC的OleTx提升(OleTx Upgrade)机制导致。关于OleTx提升机制,会在本章后续部分介绍。如果希望本实例真正采用WS-AT进行事务事务,你需要显示关闭OleTx的自动提升。我们只需要通过注册表编辑器在HKLM\SOFTWARE\Microsoft\WSAT\3.0结点下添加一个名称为OleTxUpgradeEnabled的双字节(DWORD)注册表项,比较值设为0。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-03-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、从部署的角度看分布式事务
  • 步骤1:服务契约和服务的实现
  • 步骤2:部署服务
  • 步骤3:调用BankingService
  • 步骤4:设置DTC
  • 步骤5:采用WS-AT协议
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档