Dtmcli 1.2.0
See the version list below for details.
dotnet add package Dtmcli --version 1.2.0
NuGet\Install-Package Dtmcli -Version 1.2.0
<PackageReference Include="Dtmcli" Version="1.2.0" />
paket add Dtmcli --version 1.2.0
#r "nuget: Dtmcli, 1.2.0"
// Install Dtmcli as a Cake Addin #addin nuget:?package=Dtmcli&version=1.2.0 // Install Dtmcli as a Cake Tool #tool nuget:?package=Dtmcli&version=1.2.0
English | 简体中文
dtmcli-csharp
dtmcli-csharp
is the C# client of Distributed Transaction Manager DTM that communicates with DTM Server through HTTP protocol.
It has supported distributed transaction patterns of Saga pattern, TCC pattern and 2-phase message pattern.
What is DTM
DTM is a distributed transaction solution which provides cross-service eventually data consistency. It provides saga, tcc, xa, 2-phase message strategies for a variety of application scenarios. It also supports multiple languages and multiple store engine to form up a transaction as following:
<img alt="function-picture" src="https://en.dtm.pub/assets/function.7d5618f8.png" height=250 />
Features
Extremely easy to adapt
- Support HTTP and gRPC, provide easy-to-use programming interfaces, lower substantially the barrier of getting started with distributed transactions. Newcomers can adapt quickly.
Easy to use
- Relieving developers from worrying about suspension, null compensation, idempotent transaction, and other tricky problems, the framework layer handles them all.
Language-agnostic
- Suit for companies with multiple-language stacks. Easy to write bindings for Go, Python, PHP, Node.js, Ruby, and other languages.
Easy to deploy, easy to extend
- DTM depends only on MySQL, easy to deploy, cluster, and scale horizontally.
Support for multiple distributed transaction protocol
- TCC, SAGA, XA, Transactional messages.
DTM vs. others
There is no mature open-source distributed transaction framework for non-Java languages. Mature open-source distributed transaction frameworks for Java language include Ali's Seata, Huawei's ServiceComb-Pack, Jingdong's shardingsphere, himly, tcc-transaction, ByteTCC, and so on, of which Seata is most widely used.
The following is a comparison of the main features of dtm and Seata.
Features | DTM | Seata | Remarks |
---|---|---|---|
Supported languages | <span style="color:green">Golang, C#, Java, Python, PHP, and others</span> | <span style="color:orange">Java</span> | dtm allows easy access from a new language |
Exception handling | Sub-transaction barrier | <span style="color:orange">manual</span> | dtm solves idempotent transaction, hanging, null compensation |
TCC | <span style="color:green">✓</span> | <span style="color:green">✓</span> | |
XA | <span style="color:green">✓</span> | <span style="color:green">✓</span> | |
AT | <span style="color:orange">suggest XA</span> | <span style="color:green">✓</span> | AT is similar to XA with better performance but with dirty rollback |
SAGA | <span style="color:green">support concurrency</span> | <span style="color:green">complicated state-machine mode</span> | dtm's state-machine mode is being planned |
Transactional Messaging | <span style="color:green">✓</span> | <span style="color:red">✗</span> | dtm provides Transactional Messaging similar to RocketMQ |
Multiple DBs in a service | <span style="color:green">✓</span> | <span style="color:red">✗</span> | |
Communication protocols | <span style="color:green">HTTP, gRPC</span> | <span style="color:green">Dubbo, no HTTP</span> | |
Star count | <img src="https://img.shields.io/github/stars/dtm-labs/dtm.svg?style=social" alt="github stars"/> | <img src="https://img.shields.io/github/stars/seata/seata.svg?style=social" alt="github stars"/> | dtm 0.1 is released from 20210604 and under fast development |
From the features' comparison above, if your language stack includes languages other than Java, then dtm is the one for you. If your language stack is Java, you can also choose to access dtm and use sub-transaction barrier technology to simplify your business development.
Installation
Add nuget package via the following command
dotnet add package Dtmcli
Configuration
There are two ways to configure
- Configure with setup action
services.AddDtmcli(x =>
{
// DTM server HTTP address
x.DtmUrl = "http://localhost:36789";
// request timeout for DTM server, unit is milliseconds
x.DtmTimeout = 10000;
// request timeout for trans branch, unit is milliseconds
x.BranchTimeout = 10000;
// barrier database type, mysql, postgres, sqlserver
x.DBType = "mysql";
// barrier table name
x.BarrierTableName = "dtm_barrier.barrier";
});
- Configure with
IConfiguration
services.AddDtmcli(Configuration, "dtm");
And the configuration file
{
"dtm": {
"DtmUrl": "http://localhost:36789",
"DtmTimeout": 10000,
"BranchTimeout": 10000,
"DBType": "mysql",
"BarrierTableName": "dtm_barrier.barrier",
}
}
Usage
SAGA pattern
public class MyBusi
{
private readonly Dtmcli.IDtmTransFactory _transFactory;
public MyBusi(Dtmcli.IDtmTransFactory transFactory)
{
this._transFactory = transFactory;
}
public async Task DoBusAsync()
{
var gid = Guid.NewGuid().ToString();
var req = new BusiReq { Amount = 30 };
// NOTE: After DTM v1.12.2
// when svc start with http or https, DTM server will send HTTP request to svc
// when svc don't start with http or https, DTM server will send gRPC request to svc
var svc = "http://localhost:5005";
var saga = _transFactory.NewSaga(gid);
// Add sub-transaction
saga.Add(
// URL of forward action
svc + "/api/TransOut",
// URL of compensating action
svc + "/api/TransOutCompensate",
// Arguments of actions
req);
saga.Add(
svc + "/api/TransIn",
svc + "/api/TransInCompensate",
req);
await saga.Submit();
}
}
TCC pattern
public class MyBusi
{
private readonly Dtmcli.TccGlobalTransaction _globalTransaction;
public MyBusi(Dtmcli.TccGlobalTransaction globalTransaction)
{
this._globalTransaction = globalTransaction;
}
public async Task DoBusAsync()
{
var gid = Guid.NewGuid().ToString();
var req = new BusiReq { Amount = 30 };
var svc = "http://localhost:5005";
await _globalTransaction.Excecute(gid, async tcc =>
{
// Create tcc sub-transaction
await tcc.CallBranch(
// Arguments of stages
req,
// URL of Try stage
svc + "/api/TransOutTry",
// URL of Confirm stage
svc + "/api/TransOutConfirm",
// URL of Cancel stage
svc + "/api/TransOutCancel");
await tcc.CallBranch(
req,
svc + "/api/TransInTry",
svc + "/api/TransInConfirm",
svc + "/api/TransInCancel");
});
}
}
2-phase message pattern
public class MyBusi
{
private readonly Dtmcli.IDtmTransFactory _transFactory;
public MyBusi(Dtmcli.IDtmTransFactory transFactory)
{
this._transFactory = transFactory;
}
public async Task DoBusAsync()
{
var gid = Guid.NewGuid().ToString();
var req = new BusiReq { Amount = 30 };
var svc = "http://localhost:5005";
var msg = _transFactory.NewMsg(gid);
// Add sub-transaction
msg.Add(
// URL of action
svc + "/api/TransOut",
// Arguments of action
req);
msg.Add(
svc + "/api/TransIn",
req);
// Usage 1:
// Send prepare message
await msg.Prepare(svc + "/api/QueryPrepared");
// Send submit message
await msg.Submit();
// Usage 2:
using (var conn = GetDbConnection())
{
await msg.DoAndSubmitDB(svc + "/api/QueryPrepared", conn, async tx =>
{
await conn.ExecuteAsync("insert ....", new { }, tx);
await conn.ExecuteAsync("update ....", new { }, tx);
await conn.ExecuteAsync("delete ....", new { }, tx);
});
}
}
}
Complete example
Refer to https://github.com/dtm-labs/dtmcli-csharp-sample
Contact us
Wechat communication group
Add wechat friend with id yedf2008, or scan the OR code. Fill in csharp as verification.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Dtmcli:
Package | Downloads |
---|---|
DtmDapr
a c# client for distributed transaction framework dtm with Dapr utilities. 分布式事务管理器dtm的c#客户端,附带Dapr相关的工具类。 |
|
Dtmcli.EFCore
对Dtmcli的二次封装 |
|
Dtmworkflow
a c# client for distributed transaction framework dtm. 分布式事务管理器dtm的c#客户端 |
|
Cngot.Extensions.Saga
分布式事务 |
GitHub repositories
This package is not used by any popular GitHub repositories.