-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add http xa sample * Add XA pattern comment in README * Change the DBType of DtmOptions to SqlDbType in the configuration section of the README
- Loading branch information
Showing
4 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Dtmcli; | ||
using DtmSample.Dtos; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace DtmSample.Controllers | ||
{ | ||
/// <summary> | ||
/// XA 示例 | ||
/// </summary> | ||
[ApiController] | ||
[Route("/api")] | ||
public class XaTestController : ControllerBase | ||
{ | ||
|
||
private readonly ILogger<TccTestController> _logger; | ||
private readonly XaGlobalTransaction _globalTransaction; | ||
private readonly AppSettings _settings; | ||
|
||
public XaTestController(ILogger<TccTestController> logger, IOptions<AppSettings> optionsAccs, XaGlobalTransaction transaction) | ||
{ | ||
_logger = logger; | ||
_settings = optionsAccs.Value; | ||
_globalTransaction = transaction; | ||
} | ||
|
||
/// <summary> | ||
/// Xa 成功提交 | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
[HttpPost("commit")] | ||
public async Task<IActionResult> Commit(CancellationToken cancellationToken) | ||
{ | ||
//todo: Currently only supported by mysql, please modify the appsettings.json | ||
try | ||
{ | ||
await _globalTransaction.ExcecuteAsync(async (Xa xa) => | ||
{ | ||
//// 用户1 转出30元 | ||
var res1 = await xa.CallBranch(new TransRequest("1", -30), _settings.BusiUrl + "/XaTransOut", cancellationToken); | ||
|
||
//// 用户2 转入30元 | ||
var res2 = await xa.CallBranch(new TransRequest("2", 30), _settings.BusiUrl + "/XaTransIn", cancellationToken); | ||
}, cancellationToken); | ||
|
||
return Ok(TransResponse.BuildSucceedResponse()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Xa Error"); | ||
return Ok(TransResponse.BuildFailureResponse()); | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Xa 失败回滚 | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
[HttpPost("rollbcak")] | ||
public async Task<IActionResult> Rollbcak(CancellationToken cancellationToken) | ||
{ | ||
//todo: Currently only supported by mysql, please modify the appsettings.json | ||
try | ||
{ | ||
await _globalTransaction.ExcecuteAsync(async (Xa xa) => | ||
{ | ||
//// 用户1 转出30元 | ||
var res1 = await xa.CallBranch(new TransRequest("1", -30), _settings.BusiUrl + "/XaTransOut", cancellationToken); | ||
|
||
//// 用户2 转入30元 | ||
var res2 = await xa.CallBranch(new TransRequest("2", 30), _settings.BusiUrl + "/XaTransIn", cancellationToken); | ||
|
||
throw new Exception("rollbcak"); | ||
}, cancellationToken); | ||
|
||
return Ok(TransResponse.BuildSucceedResponse()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Xa Error"); | ||
return Ok(TransResponse.BuildFailureResponse()); | ||
} | ||
} | ||
} | ||
} |