PHPackages                             wyi/dtm-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wyi/dtm-client

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wyi/dtm-client
==============

A PHP coroutine client for distributed transaction manager DTM. 分布式事务管理器 DTM 的 PHP 协程客户端

v0.2.2(3y ago)131MITPHPPHP &gt;=7.2

Since Feb 14Pushed 3y agoCompare

[ Source](https://github.com/YunzhiYike/dtm-client)[ Packagist](https://packagist.org/packages/wyi/dtm-client)[ RSS](/packages/wyi-dtm-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (8)Used By (0)

English | [中文](./README-CN.md)

[![DTM Logo](https://avatars.githubusercontent.com/u/6055142?s=200&v=4)](https://hyperf.wiki)

 [![Stable Version](https://camo.githubusercontent.com/379a35c74a77dc65ff20cca28fa1b3f907df3d89cf8af6ed48604cfc13c3bebf/68747470733a2f2f706f7365722e707567782e6f72672f64746d2d7068702f64746d2d636c69656e742f762f737461626c65)](https://github.com/dtm-php/dtm-client/releases) [![Php Version](https://camo.githubusercontent.com/3fe2b00a3b57acdbf9c6ee0890609712713224e8b654eff0d23e4a0ae3969828/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d382e302d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://www.php.net) [![dtm-client License](https://camo.githubusercontent.com/8fe97c978962ec642bf06e3ad59e113a9c4e6be67bd29358dd378c91ef003202/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f64746d2d7068702f64746d2d636c69656e742e737667)](https://github.com/dtm-php/dtm-client/blob/master/LICENSE)

 [![PHPUnit for dtm-client](https://github.com/dtm-php/dtm-client/actions/workflows/test.yml/badge.svg)](https://github.com/dtm-php/dtm-client/actions) [![Total Downloads](https://camo.githubusercontent.com/6b3029373e3c76893449b053e53b05f776521124c7be21c9eaa9d36e626f440e/68747470733a2f2f706f7365722e707567782e6f72672f64746d2f64746d2d636c69656e742f646f776e6c6f616473)](https://packagist.org/packages/dtm/dtm-client) [![Monthly Downloads](https://camo.githubusercontent.com/0e28985615ab5ada9d9a4e8804dff7e9f042f9dee6251c1fefaeab6baf217287/68747470733a2f2f706f7365722e707567782e6f72672f64746d2f64746d2d636c69656e742f642f6d6f6e74686c79)](https://packagist.org/packages/dtm/dtm-client)

Introduction
============

[](#introduction)

[dtm/dtm-client](https://packagist.org/packages/dtm/dtm-client) is the PHP client of Distributed Transaction Manager [DTM](https://github.com/dtm-labs/dtm). It has supported distributed transaction patterns of TCC pattern, Saga pattern, XA pattern, and two-phase message pattern. In communicate protocol it has supported communicate with DTM Server through HTTP protocol or gRPC protocol. Also the client can safely run in PHP-FPM and Swoole coroutine environment, and it has also make support more easier for [Hyperf](https://github.com/hyperf/hyperf) framework.

About DTM
=========

[](#about-dtm)

DTM is an open source distributed transaction manager based on Go language, which provides the powerful function of combining transactions across languages and storage engines. DTM elegantly solves distributed transaction problems such as interface idempotent, null compensation, and transaction suspension, and also provides a distributed transaction solutions that are easy to use, high performance, and easy to scale horizontally.

Advantage
---------

[](#advantage)

- Easy to start
    - Start the service with zero configuration and provide a very simple and clear HTTP interface, which greatly reduces the difficulty of getting started with distributed transactions
- Cross Programming language
    - Can be used by companies with multiple language stacks. It is convenient to use in various languages such as Go, Python, PHP, NodeJs, Ruby, C#, etc.
- Simple to use
    - Developers no longer worry about transaction suspension, null compensation, interface idempotent and other issues, and the first sub-transaction barrier technology handles it for you
- Easy to deploy and expand
    - Depends only on MySQL/Redis, easy to deploy, easy to cluster, and easy to scale horizontally
- Multiple distributed transaction protocol support
    - TCC, SAGA, XA, two-stage message, one-stop solution to various distributed transaction problems

Comparison
----------

[](#comparison)

In non-Java languages, there is still no mature distributed transaction manager other than DTM, so here is a comparison between DTM and Seata, the most mature open source project in Java:

FeaturesDTMSEATAMemo[language supports](https://dtm.pub/other/opensource.html#lang)Go、C#、Java、Python、PHP...JavaDTM is easier implemented the client to a new language[Storage Engine](https://dtm.pub/other/opensource.html#store)Support Database, Redis, Mongo, etc.Database[Exception Handle](https://dtm.pub/other/opensource.html#exception) Sub-transaction barrier is handled automatically By manualDTM solves transaction suspension, null compensation, interface idempotent etc.[SAGA](https://dtm.pub/other/opensource.html#saga)Easy to useComplex state machine[Two-phase message](https://dtm.pub/other/opensource.html#msg)✓✗Minimal Message Eventual Consistency Architecture[TCC](https://dtm.pub/other/opensource.html#tcc)✓✓[XA](https://dtm.pub/other/opensource.html#xa)✓✓[AT](https://dtm.pub/other/opensource.html#at)XA is more recommended✓AT is similar to XA, but with dirty rollback[Single service with multiple data sources](https://dtm.pub/other/opensource.html#multidb)✓✗[Communicate protocol](https://dtm.pub/other/opensource.html#protocol)HTTP、gRPCDubbo etc.DTM is more friendly to cloud native[Github Stargazers](https://dtm.pub/other/opensource.html#star)[![github stars](https://camo.githubusercontent.com/03f26488dec8134f9a9230ab4397ee6b2b0a341b7bfa19d35a0a1c9fccda9b3c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f64746d2d6c6162732f64746d2e7376673f7374796c653d736f6369616c)](https://camo.githubusercontent.com/03f26488dec8134f9a9230ab4397ee6b2b0a341b7bfa19d35a0a1c9fccda9b3c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f64746d2d6c6162732f64746d2e7376673f7374796c653d736f6369616c)[![github stars](https://camo.githubusercontent.com/5819e318ba7b39b32bc332a1bd00b2c1d490461ab5bccaa47cca38c55305895c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73656174612f73656174612e7376673f7374796c653d736f6369616c)](https://camo.githubusercontent.com/5819e318ba7b39b32bc332a1bd00b2c1d490461ab5bccaa47cca38c55305895c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73656174612f73656174612e7376673f7374796c653d736f6369616c)DTM released version 0.1 from 2021-06-04, developing rapidlyFrom the characteristics of the comparison above, DTM has great advantages in many aspects. If you consider multi-language support and multi-storage engine support, then DTM is undoubtedly your first choice.

Installation
============

[](#installation)

It is very convenient to install dtm-client through Composer

```
composer require dtm/dtm-client
```

- Don't forget to start DTM Server before you use it

Configuration
=============

[](#configuration)

Configuration file
------------------

[](#configuration-file)

If you are using the Hyperf framework, after installing the component, you can publish a configuration file to `./config/autoload/dtm.php` with the following `vendor:publish` command

```
php bin/hyperf.php vendor:publish dtm/dtm-client
```

If you are using a non-Hyperf framework, copy the `./vendor/dtm/dtm-client/publish/dtm.php` file to the corresponding configuration directory.

```
use DtmClient\Constants\Protocol;
use DtmClient\Constants\DbType;

return [
    // The communication protocol between the client and the DTM Server, supports Protocol::HTTP and Protocol::GRPC
    'protocol' => Protocol::HTTP,
    // DTM Server address
    'server' => '127.0.0.1',
    // DTM Server port
    'port' => [
        'http' => 36789,
        'grpc' => 36790,
    ],
    // Sub-transaction barrier
    'barrier' => [
        // Subtransaction barrier configuration in DB mode
        'db' => [
            'type' => DbType::MySQL
        ],
        // Subtransaction barrier configuration in Redis mode
        'redis' => [
            // Timeout for subtransaction barrier records
            'expire_seconds' => 7 * 86400,
        ],
        // Classes that apply sub-transaction barriers in non-Hyperf frameworks or without annotation usage
        'apply' => [],
    ],
    // Options of Guzzle client under HTTP protocol
    'guzzle' => [
        'options' => [],
    ],
];
```

Configure middleware
--------------------

[](#configure-middleware)

Before using it, you need to configure the `DtmClient\Middleware\DtmMiddleware` middleware as the server's global middleware. This middleware supports the PSR-15 specification and is applicable to all frameworks that support this specification. For middleware configuration in Hyperf, please refer to [Hyperf Documentation - Middleware](https://www.hyperf.wiki/2.2/#/zh-cn/middleware/middleware) chapter.

Usage
=====

[](#usage)

The usage of dtm-client is very simple, we provide a sample project [dtm-php/dtm-sample](https://github.com/dtm-php/dtm-sample) to help you better understand and debug. Before using this component, it is also strongly recommended that you read the [DTM official documentation](https://dtm.pub/) for a more detailed understanding.

TCC pattern
-----------

[](#tcc-pattern)

The TCC pattern is a very popular flexible distributed transaction solution. The concept of TCC is composed of the acronyms of the three words Try-Confirm-Cancel. It was first published in a paper named [Life beyond Distributed Transactions:an Apostate’s Opinion](https://www.ics.uci.edu/~cs223/papers/cidr07p15.pdf) by Pat Helland in 2007.

### Three stages of TCC

[](#three-stages-of-tcc)

Try phase: try to execute, complete all business checks (consistency), reserve necessary business resources (pre-isolation) Confirm stage: If all branches of the Try are successful, go to the Confirm stage. Confirm actually executes the business without any business check, and only uses the business resources reserved in the Try phase Cancel stage: If one of the Try of all branches fails, go to the Cancel stage. Releases the business resources reserved in the Try phase.

If we want to carry out a business similar to inter-bank transfer between banks, the transfer out (TransOut) and the transfer in (TransIn) are in different microservices, and a typical sequence diagram of a successfully completed TCC transaction is as follows:

[![](https://camo.githubusercontent.com/32f54cee96a2fe53243e8f4e25f6b82917808ced423adc515011d83ffbf74e52/68747470733a2f2f656e2e64746d2e7075622f6173736574732f7463635f6e6f726d616c2e38356365623636312e6a7067)](https://camo.githubusercontent.com/32f54cee96a2fe53243e8f4e25f6b82917808ced423adc515011d83ffbf74e52/68747470733a2f2f656e2e64746d2e7075622f6173736574732f7463635f6e6f726d616c2e38356365623636312e6a7067)

### Example

[](#example)

The following shows how to use it in the Hyperf framework, other frameworks are similar

```
