PHP code example of brown / brown-dtm

1. Go to this page and download the library: Download brown/brown-dtm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brown / brown-dtm example snippets




use Dtm\Constants\DbType;
use Dtm\Constants\Protocol;

return [
    'dtm'=>[
        'protocol' => Protocol::HTTP,  //选择接入的协议,目前仅支持http协议接入,grpc后续开发中
        'server' => '127.0.0.1', //dtm服务器地址
        'port' => [
            'http' => 36789,
            'grpc' => 36790,
        ],
        'barrier' => [
            'db' => [
                'type' => DbType::MySQL, //dtm引擎的类型 redis引擎正在开发中
            ],
            'apply' => [],
        ],
        'guzzle' => [
            'options' => [],
        ],
    ]
];


/**
 *   Author:Brown
 *   Email: [email protected]
 *   Time:
 */

namespace app\controller;

use Dtm\Api\HttpApi;

use Dtm\Api\HttpApiFactory;
use Dtm\Saga;
use Dtm\TCC;
use Dtm\BranchIdGenerator;
use Dtm\Context\TransContext;
use GuzzleHttp\Client;
use think\Request;

class Index
{
    protected TCC $tcc;
    protected $url='https://gift.yourcharon.com/base-images-project';
    public function index(){
        $api=(new HttpApiFactory())->factory();  //获取httpapi实例
		$BranchIdGenerator=(new BranchIdGenerator()); //获取分支事务Id编号
        $this->tcc=new TCC($api,$BranchIdGenerator); 
        try {
            $this->tcc->globalTransaction(function (TCC $tcc){
                $tcc->callBranch(
                    ['account'=>30],     //事务数据
                    $this->url.'/trya',  //try方法
                    $this->url.'/confirma', //confirm方法
                    $this->url.'/cancela', //回滚方法
                );
                $tcc->callBranch(
                    ['account'=>30],
                    $this->url.'/tryb',
                    $this->url.'/confirmb',
                    $this->url.'/cancelb',
                );
            });
        }catch (\Throwable $e){
            print_r($e->getMessage());
            print_r($e->getFile());
            print_r($e->getLine());
        }
        echo '-----------';
        // 通过 TransContext::getGid() 获得 全局事务ID 并返回
//        echo TransContext::getGid();
        return TransContext::getGid();

    }
}


/**
 *   Author:Brown
 *   Email: [email protected]
 *   Time:
 */

namespace app\controller;

use Dtm\Api\HttpApi;

use Dtm\Api\HttpApiFactory;
use Dtm\Saga;
use Dtm\TCC;
use Dtm\BranchIdGenerator;
use Dtm\Context\TransContext;
use GuzzleHttp\Client;
use think\Request;

class Index
{
    protected TCC $tcc;
    protected $url='https://gift.yourcharon.com/base-images-project';
    public function index(){
        $api=(new HttpApiFactory())->factory();  //获取httpapi实例
		try {
            $data = ['amount' => 50];
            $saga=new Saga($api);
            $saga->init();

            $saga->add(
                $this->url.'/sagaout',
                $this->url.'/sagaoutCompensate',
                $data
            );

            $saga->add(
                $this->url.'/sagain',
                $this->url.'/sagainCompensate',
                $data
            );

            $saga->submit();

        }catch (\Throwable $exception){
            print_r($exception->getMessage());
            print_r($exception->getFile());
            print_r($exception->getLine());
        }
        echo TransContext::getGid();
        return TransContext::getGid();

    }
}