Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
070c239
Update README-CN.md
ZiYangJia Jul 9, 2024
ace2909
Refactor Saga class to use Context for state management
tw2066 Oct 22, 2025
ded4c2c
Refactor context keys to use class constants
tw2066 Oct 22, 2025
66a2998
Fixed: Saga 单例情况下, 导致concurrent和orders持久化
PandaLIU-1111 Oct 28, 2025
8373733
Update conditions for branch operations in MySqlBarrier
tw2066 Jan 24, 2026
78056cc
add $originOP Branch::BranchRollback
tw2066 Jan 25, 2026
602eff6
add msg topic
tw2066 Jan 27, 2026
ce49718
add msg topic
PandaLIU-1111 Jan 27, 2026
18a1b04
Update conditions for branch operations in MySqlBarrier
PandaLIU-1111 Jan 27, 2026
aa0f98b
Merge pull request #76 from ZiYangJia/patch-1
PandaLIU-1111 Jan 27, 2026
f6e531f
add db builder
tw2066 Jan 27, 2026
1ce29d2
db优化
tw2066 Jan 28, 2026
bee9c6c
优化msg call方法
tw2066 Jan 28, 2026
a034493
Format code
tw2066 Jan 28, 2026
7349e47
Format code
tw2066 Jan 28, 2026
6363b01
Format code
tw2066 Jan 28, 2026
80f38d1
增加msg代码示例
tw2066 Jan 28, 2026
037b5f3
TCC add branchHeaders
tw2066 Jan 29, 2026
7c3e307
增加日志记录
tw2066 Jan 30, 2026
f6deddc
add doAndSubmitDB method
tw2066 Jan 30, 2026
6203351
optimize
tw2066 Jan 30, 2026
b9fe313
optimize return array|ResponseInterface
tw2066 Feb 1, 2026
6f49817
优化msg call方法, 添加doAndSubmitDB方法
PandaLIU-1111 Feb 4, 2026
0114186
ide tcc return array|ResponseInterface
PandaLIU-1111 Feb 4, 2026
f0cf2c0
增加 Barrier 支持 Db 操作
PandaLIU-1111 Feb 4, 2026
edae028
增加日志记录-转string
tw2066 Feb 4, 2026
fc47f4c
TCC : Try 阶段增加branchHeaders
PandaLIU-1111 Feb 5, 2026
1cf7e3c
适配hyperf 3.2版本
tw2066 Jul 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 23 additions & 78 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,38 @@ name: PHPUnit

on: [ push, pull_request ]

env:
SWOOLE_VERSION: '4.8.6'
SWOW_VERSION: 'develop'

jobs:
ci:
name: Test PHP ${{ matrix.php-version }} on ${{ matrix.engine }}
runs-on: "${{ matrix.os }}"
ci32:
runs-on: ubuntu-latest
container: hyperf/hyperf:${{ matrix.hyperf-version }}
strategy:
matrix:
os: [ ubuntu-latest ]
php-version: [ '8.0', '8.1' ]
engine: [ 'none', 'swoole', 'swow' ]
max-parallel: 6
hyperf-version:
- "8.2-alpine-v3.22-swoole"
- "8.3-alpine-v3.23-swoole"
- "8.4-alpine-v3.23-swoole"
fail-fast: false
max-parallel: 15

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: phpize
ini-values: |
opcache.enable_cli=1

coverage: none
- name: Setup Swoole
if: ${{ matrix.engine == 'swoole' }}
uses: actions/checkout@v4
- name: Setup Environment
run: |
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f
sudo apt-get install libcurl4-openssl-dev
wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz
mkdir -p swoole
tar -xf swoole.tar.gz -C swoole --strip-components=1
rm swoole.tar.gz
cd swoole
phpize
./configure --enable-openssl --enable-http2 --enable-swoole-curl --enable-swoole-json
make -j$(nproc)
sudo make install
sudo sh -c "echo extension=swoole > /etc/php/${{ matrix.php-version }}/cli/conf.d/swoole.ini"
pwd
ls -al
php -v
php -m
php --ri swoole
- name: Setup Swow
if: ${{ matrix.engine == 'swow' }}
run: |
wget https://github.com/swow/swow/archive/"${SWOW_VERSION}".tar.gz -O swow.tar.gz
mkdir -p swow
tar -xf swow.tar.gz -C swow --strip-components=1
rm swow.tar.gz
cd swow/ext || exit
composer -V

phpize
./configure --enable-debug
make -j "$(nproc)"
sudo make install
sudo sh -c "echo extension=swow > /etc/php/${{ matrix.php-version }}/cli/conf.d/swow.ini"
php --ri swow

- name: Setup Packages
- name: Install Dependencies
run: |
composer update -o --no-scripts
composer update doctrine/instantiator:^1.0
composer require symfony/finder:^5.0
composer require hyperf/di
composer require hyperf/grpc-client
composer require hyperf/framework
composer require psr/log:^1.0
composer require psr/container:^1.0
composer require symfony/console:^5.0
composer require hyperf/dispatcher
composer require hyperf/server
composer require hyperf/http-server
composer require hyperf/db
composer require hyperf/db-connection
composer require hyperf/json-rpc
composer require hyperf/rpc-client
composer require symfony/serializer:^5.0
composer update -o
composer info

- name: Setup Swow Packages
if: ${{ matrix.engine == 'swow' }}
run: |
composer require hyperf/engine-swow
- name: Static Analysis
run: composer analyse

- name: Run Test Cases
run: |
composer analyse
composer test
- name: Run Tests
run: composer test
71 changes: 71 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ composer require dtm/dtm-client
```bash
php bin/hyperf.php vendor:publish dtm/dtm-client
```
另需运行vendor/dtm/dtm-client/publish下的barrier表迁移文件

如果您是在非 Hyperf 框架中使用,可复制 `./vendor/dtm/dtm-client/publish/dtm.php` 文件到对应的配置目录中。

Expand Down Expand Up @@ -374,3 +375,73 @@ class XAController

```
上面的代码首先注册了一个全局XA事务,然后添加了两个子事务transIn、transOut。子事务全部执行成功之后,提交给dtm。dtm收到提交的xa全局事务后,会调用所有子事务的xa commit,完成整个xa事务。

## 二阶段消息

### 代码示例

以下展示在 Hyperf 框架中的使用方法,其它框架类似

```php
<?php

declare(strict_types=1);

namespace App\Controller;

use DtmClient\Barrier;
use DtmClient\Middleware\DtmMiddleware;
use DtmClient\Msg;
use DtmClient\TransContext;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Controller(prefix: '/msg')]
class MsgController extends AbstractSagaController
{
private Msg $msg;

public function __construct(Msg $msg)
{
$this->msg = $msg;
}

#[RequestMapping(path: 'msg')]
public function msg()
{
$this->msg->init();
$this->msg->add('http://127.0.0.1:19501/msg/test', ['name' => 'dtmMsg']);
//添加Topic
//$this->msg->addTopic('TransIn', ['name' => 'Topic dtmMsg']);

$this->msg->doAndSubmitDB('http://127.0.0.1:19501/msg/queryPrepared', function () {
var_dump('执行业务');
});
return TransContext::getGid();
}

#[RequestMapping(path: 'queryPrepared')]
public function queryPrepared(Barrier $barrier)
{
var_dump(__METHOD__);
$transType = $this->request->query('trans_type');
$gid = $this->request->query('gid');
$barrier->queryPrepared($transType, $gid);
}

#[RequestMapping(path: 'test')]
public function test()
{
var_dump(__METHOD__);
return 'rest';
}

#[RequestMapping(path: 'subscribe')]
public function subscribe()
{
//订阅
$this->msg->subscribe('TransIn', 'http://127.0.0.1:19501/msg/test', 'subscribe test');
return 'subscribe';
}
}
```
27 changes: 18 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,35 @@
}
},
"require": {
"php": ">=8.0",
"php": ">=8.2",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.4",
"hyperf/context": "^2.2|^3.0|^3.1",
"hyperf/context": "~3.2.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"hyperf/config": "~3.2.0",
"hyperf/db": "~3.2.0",
"hyperf/redis": "~3.2.0",
"hyperf/di": "~3.2.0",
"hyperf/grpc-client": "~3.2.0",
"hyperf/rpc-client": "~3.2.0",
"hyperf/json-rpc": "~3.2.0",
"hyperf/http-server": "~3.2.0",
"hyperf/db-connection": "~3.2.0",
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^9.5"
},
"suggest": {
"hyperf/config": "^2.2|^3.0|^3.1",
"hyperf/db": "^2,2|^3.0|^3.1",
"hyperf/redis": "^2,2|^3.0|^3.1",
"hyperf/di": "^2.2|^3.0|^3.1",
"hyperf/grpc-client": "^2.2|^3.0|^3.1",
"hyperf/rpc-client": "^2.2|^3.0|^3.1",
"hyperf/json-rpc": "^2.2|^3.0|^3.1",
"hyperf/config": "~3.2.0",
"hyperf/db": "~3.2.0",
"hyperf/redis": "~3.2.0",
"hyperf/di": "~3.2.0",
"hyperf/grpc-client": "~3.2.0",
"hyperf/rpc-client": "~3.2.0",
"hyperf/json-rpc": "~3.2.0",
"ext-openssl": "Required to use HTTPS.",
"ext-pdo": "Required to use MySQL Client.",
"ext-pdo_mysql": "Required to use MySQL Client.",
Expand Down
7 changes: 2 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
#
parameters:
level: 1
excludePaths:
- */src/DbTransaction/LaravelDbTransaction.php
ignoreErrors:
- '#Call to static method beginTransaction\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#'
- '#Call to static method commit\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#'
- '#Call to static method rollback\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#'
- '#Static call to instance method Hyperf\\DB\\DB::execute\(\)\.#'
- '#Static call to instance method Hyperf\\DB\\DB::beginTransaction\(\)\.#'
- '#Static call to instance method Hyperf\\DB\\DB::commit\(\)\.#'
- '#Static call to instance method Hyperf\\DB\\DB::rollback\(\)\.#'
- '#Call to static method currentRouteAction\(\) on an unknown class Illuminate\\Support\\Facades\\Route\.#'
- '#Parameter \$request of method DtmClient\\Middleware\\DtmLaravelMiddleware::handle\(\) has invalid type Illuminate\\Http\\Request\.#'
- '#Call to static method affectingStatement\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#'
- '#Static call to instance method Hyperf\\DB\\DB::query\(\)\.#'
- '#Call to static method select\(\) on an unknown class Illuminate\\Support\\Facades\\DB\.#'
1 change: 1 addition & 0 deletions publish/dtm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'barrier' => [
'db' => [
'type' => DbType::MySQL,
// 'type' => DbType::DB,
],
'apply' => [],
],
Expand Down
2 changes: 2 additions & 0 deletions src/Api/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public function query(array $body);

public function queryAll(array $body);

public function subscribe(array $body);

public function transRequestBranch(RequestBranch $requestBranch);
}
5 changes: 5 additions & 0 deletions src/Api/GrpcApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function queryAll(array $body)
throw new UnsupportedException('Unsupported QueryAll operation');
}

public function subscribe(array $body)
{
throw new UnsupportedException('Unsupported QueryAll operation');
}

public function transRequestBranch(RequestBranch $requestBranch)
{
[$hostname, $method] = $this->parseHostnameAndMethod($requestBranch->url);
Expand Down
5 changes: 5 additions & 0 deletions src/Api/HttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function queryAll(array $body)
return $this->transQuery($body, Operation::QUERY_ALL);
}

public function subscribe(array $body)
{
return $this->transQuery($body, Operation::SUBSCRIBE);
}

public function getClient(): Client
{
return $this->client;
Expand Down
5 changes: 5 additions & 0 deletions src/Api/JsonRpcHttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public function queryAll(array $body)
throw new UnsupportedException('Unsupported Query operation');
}

public function subscribe(array $body)
{
throw new UnsupportedException('Unsupported Query operation');
}

public function getClient(): Client
{
return $this->client;
Expand Down
7 changes: 6 additions & 1 deletion src/Barrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class Barrier

protected MySqlBarrier $mySqlBarrier;

public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier)
protected DbBarrier $dbBarrier;

public function __construct(ConfigInterface $config, MySqlBarrier $mySqlBarrier, DbBarrier $dbBarrier)
{
$this->config = $config;
$this->mySqlBarrier = $mySqlBarrier;
$this->dbBarrier = $dbBarrier;
}

public function call(callable $businessCall)
Expand Down Expand Up @@ -77,6 +80,8 @@ protected function getBarrier(): BarrierInterface
switch ($this->config->get('dtm.barrier.db.type', DbType::MySQL)) {
case DbType::MySQL:
return $this->mySqlBarrier;
case DbType::DB:
return $this->dbBarrier;
default:
throw new UnsupportedException('Barrier DB type is unsupported.');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Constants/DbType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ class DbType
{
public const MySQL = 'mysql';

public const DB = 'db';

public const Redis = 'redis';
}
2 changes: 2 additions & 0 deletions src/Constants/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ class Operation
public const ACTION = 'action';

public const ROLLBACK = 'rollback';

public const SUBSCRIBE = 'subscribe';
}
Loading
Loading