Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Aug 17, 2022
1 parent a172497 commit a04beaa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ AsyncLocal 负责存储执行上下文 DBKey 值,在异步或同步并发场

1、简介

FreeSqlCloud 提供 TCC/SAGA 分布式事务调度,遇错重试、程序重启不影响的事务单元的管理功能
FreeSqlCloud 提供 TCC/SAGA 分布式事务调度、失败重试、持久化重启后重新唤醒事务单元、等管理功能

TCC 事务特点:

Expand Down Expand Up @@ -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<HttpTcc>(default, new HttpUnitState
.Then<HttpSaga>(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<HttpTcc>(default, new HttpUnitState
.Then<HttpSaga>(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<HttpTcc>(default, new HttpUnitState
.Then<HttpSaga>(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<HttpUnitState>
class HttpSaga : SagaUnit<HttpUnitState>
{
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;
}
}
Expand Down
44 changes: 42 additions & 2 deletions examples/netcore31_tcc_saga/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async static Task Main(string[] args)
await TestTcc();
await TestSaga();
//await TestHttpTcc();
//await TestHttpSaga();

Console.ReadKey();
DB.Cloud.Dispose();
Expand Down Expand Up @@ -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<HttpSaga>(default, new HttpUnitState
{
Url = "https://192.168.1.100/saga/UserPoint",
Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId
})
.Then<HttpSaga>(default, new HttpUnitState
{
Url = "https://192.168.1.100/saga/GoodsStock",
Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId
})
.Then<HttpSaga>(default, new HttpUnitState
{
Url = "https://192.168.1.100/saga/OrderNew",
Data = "UserId=1&Point=10&GoodsId=1&OrderId=" + orderId
})
.ExecuteAsync();
}
class HttpSaga : SagaUnit<HttpUnitState>
{
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();
Expand All @@ -75,8 +117,6 @@ await DB.Cloud.StartTcc(orderId.ToString(), "支付购买webapi",
})
.ExecuteAsync();
}


class HttpTcc : TccUnit<HttpUnitState>
{
public override Task Try()
Expand Down

0 comments on commit a04beaa

Please sign in to comment.