From a04beaa0182933b668b002c68fadd94712b20306 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Wed, 17 Aug 2022 15:29:13 +0800 Subject: [PATCH] update --- README.md | 31 ++++++++---------- examples/netcore31_tcc_saga/Program.cs | 44 ++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9bfa7cb..cdc74ce 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ AsyncLocal 负责存储执行上下文 DBKey 值,在异步或同步并发场 1、简介 -FreeSqlCloud 提供 TCC/SAGA 分布式事务调度,遇错重试、程序重启不影响的事务单元的管理功能。 +FreeSqlCloud 提供 TCC/SAGA 分布式事务调度、失败重试、持久化重启后重新唤醒事务单元、等管理功能。 TCC 事务特点: @@ -226,44 +226,39 @@ TccUnit、SagaUnit 方法内可以使用 Orm 访问当前事务对象。 ```c# // HTTP 服务编排?? var orderId = Guid.NewGuid(); -await DB.Cloud.StartTcc(orderId.ToString(), "支付购买webapi", - new TccOptions +await DB.Cloud.StartSaga(orderId.ToString(), "支付购买webapi(saga)", + new SagaOptions { MaxRetryCount = 10, RetryInterval = TimeSpan.FromSeconds(10) }) - .Then(default, new HttpUnitState + .Then(default, new HttpUnitState { - Url = "https://192.168.1.100/tcc/UserPoint", + Url = "https://192.168.1.100/saga/UserPoint", Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId }) - .Then(default, new HttpUnitState + .Then(default, new HttpUnitState { - Url = "https://192.168.1.100/tcc/GoodsStock", + Url = "https://192.168.1.100/saga/GoodsStock", Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId }) - .Then(default, new HttpUnitState + .Then(default, new HttpUnitState { - Url = "https://192.168.1.100/tcc/OrderNew", + Url = "https://192.168.1.100/saga/OrderNew", Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId }) .ExecuteAsync(); -class HttpTcc : TccUnit +class HttpSaga : SagaUnit { - public override Task Try() - { - Console.WriteLine("请求 webapi:" + State.Url + "/Try" + State.Data); - return Task.CompletedTask; - } - public override Task Confirm() + public override Task Commit() { - Console.WriteLine("请求 webapi:" + State.Url + "/Confirm" + State.Data); + //Console.WriteLine("请求 webapi:" + State.Url + "/Commit" + State.Data); return Task.CompletedTask; } public override Task Cancel() { - Console.WriteLine("请求 webapi:" + State.Url + "/Cancel" + State.Data); + //Console.WriteLine("请求 webapi:" + State.Url + "/Cancel" + State.Data); return Task.CompletedTask; } } diff --git a/examples/netcore31_tcc_saga/Program.cs b/examples/netcore31_tcc_saga/Program.cs index 66ed409..35714dd 100644 --- a/examples/netcore31_tcc_saga/Program.cs +++ b/examples/netcore31_tcc_saga/Program.cs @@ -14,6 +14,7 @@ async static Task Main(string[] args) await TestTcc(); await TestSaga(); //await TestHttpTcc(); + //await TestHttpSaga(); Console.ReadKey(); DB.Cloud.Dispose(); @@ -49,6 +50,47 @@ await DB.Cloud.StartSaga(orderId.ToString(), "支付购买SAGA事务", .ExecuteAsync(); } + + async static Task TestHttpSaga() + { + var orderId = Guid.NewGuid(); + await DB.Cloud.StartSaga(orderId.ToString(), "支付购买webapi(saga)", + new SagaOptions + { + MaxRetryCount = 10, + RetryInterval = TimeSpan.FromSeconds(10) + }) + .Then(default, new HttpUnitState + { + Url = "https://192.168.1.100/saga/UserPoint", + Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId + }) + .Then(default, new HttpUnitState + { + Url = "https://192.168.1.100/saga/GoodsStock", + Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId + }) + .Then(default, new HttpUnitState + { + Url = "https://192.168.1.100/saga/OrderNew", + Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId + }) + .ExecuteAsync(); + } + class HttpSaga : SagaUnit + { + public override Task Commit() + { + //Console.WriteLine("请求 webapi:" + State.Url + "/Commit" + State.Data); + return Task.CompletedTask; + } + public override Task Cancel() + { + //Console.WriteLine("请求 webapi:" + State.Url + "/Cancel" + State.Data); + return Task.CompletedTask; + } + } + async static Task TestHttpTcc() { var orderId = Guid.NewGuid(); @@ -75,8 +117,6 @@ await DB.Cloud.StartTcc(orderId.ToString(), "支付购买webapi", }) .ExecuteAsync(); } - - class HttpTcc : TccUnit { public override Task Try()