# 1. 安装
docker run -itd --name dtm -p 36789:36789 -p 36790:36790 yedf/dtm:latest
1
# 2. Test.php
<?php
/**
* @Author: gan
* @Description:
* @File: test
* @Version: 1.0.0
* @Date: 2022/3/1 8:55 上午
*/
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
function FireTcc()
{
$dtm = 'http://localhost:36789/api/dtmsvr';
$svc = "http://10.7.3.40:9501/index";
Dtmcli\tccGlobalTransaction($dtm, function ($tcc) use ($svc) {
/** @var Dtmcli\Tcc $tcc */
$req = ['amount' => 30];
echo 'calling trans out' . PHP_EOL;
$tcc->callBranch($req, $svc . '/ATransOutTry', $svc . '/ATransOutConfirm', $svc . '/ATransOutCancel');
echo 'calling trans in' . PHP_EOL;
$tcc->callBranch($req, $svc . '/BTransInTry', $svc . '/BTransInConfirm', $svc . '/BTransInCancel');
});
}
FireTcc();
//
//$vega = new Mix\Vega\Engine();
//$vega->handleFunc('/hello', function (Mix\Vega\Context $ctx) {
// FireTcc();
// $ctx->string(200, 'hello, world!');
//})->methods('GET');
//
//$http = new Swoole\Http\Server('0.0.0.0', 9511);
//$http->on('Request', $vega->handler());
//$http->start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 3. 业务逻辑
<?php
namespace App\HttpController;
use EasySwoole\Http\Message\Status;
use RX\Core\CoreController;
class Index extends CoreController
{
private array $db1Config = [];
private array $db2Config = [];
private string $username;
public function ATransOutTry()
{
var_dump("ATransOutTry");
try {
$params = $this->getParams();
$db = new \RX\Component\DB\EasyPdo\EasyPdo($this->db1Config, "db1");
$db->update("co_merchant", ["account[-]" => $params['amount']], ['id' => 1]);
$data = ["result" => "SUCCESS"];
} catch (\Throwable $e) {
$data = ["result" => "FAILURE"];
}
return self::rJson($data);
}
public function ATransOutConfirm()
{
var_dump("ATransOutConfirm");
return self::rJson(["result" => "SUCCESS"]);
}
public function ATransOutCancel()
{
var_dump("ATransOutCancel");
$params = $this->getParams();
$db = new \RX\Component\DB\EasyPdo\EasyPdo($this->db1Config, "db1");
$db->update("co_merchant", ["account[+]" => $params['amount']], ['id' => 1]);
return self::rJson(["result" => "SUCCESS"]);
}
public function BTransInTry()
{
try {
var_dump("BTransInTry");
$this->username = uniqid();
$db2 = new \RX\Component\DB\EasyPdo\EasyPdo($this->db2Config, "db2");
$db2->insert("user", ["username" => $this->username, "password" => time()]);
return self::rJson(["result" => "SUCCESS"]);
} catch (\Throwable $e) {
var_dump($e->getMessage());
return self::rJson(["result" => "FAILURE"]);
}
}
public function BTransInConfirm()
{
var_dump("BTransInConfirm");
return self::rJson(["result" => "SUCCESS"]);
}
public function BTransInCancel()
{
var_dump("BTransInCancel");
try {
$db2 = new \RX\Component\DB\EasyPdo\EasyPdo($this->db2Config, "db2");
$db2->delete("user", ["username" => $this->username]);
return self::rJson(["result" => "SUCCESS"]);
} catch (\Throwable $e) {
var_dump($e->getMessage());
return self::rJson(["result" => "FAILURE"]);
}
}
private function rJson($data)
{
if (!$this->response()->isEndResponse()) {
$this->response()->withStatus(Status::CODE_OK);
$this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
$this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$this->response()->end();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87