PHPackages                             wangrunxinyes/hyperf-tcc - 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. [Framework](/categories/framework)
4. /
5. wangrunxinyes/hyperf-tcc

ActiveLibrary[Framework](/categories/framework)

wangrunxinyes/hyperf-tcc
========================

基于Hyperf框架的TCC分布式事务组件

v1.0.6(4y ago)14MITPHP

Since May 21Pushed 4y agoCompare

[ Source](https://github.com/wangrunxinyes/hyperf-tcc)[ Packagist](https://packagist.org/packages/wangrunxinyes/hyperf-tcc)[ Docs](https://github.com/yogcloud/hyperf-tcc)[ RSS](/packages/wangrunxinyes-hyperf-tcc/feed)WikiDiscussions main Synced 3d ago

READMEChangelogDependencies (6)Versions (8)Used By (0)

hyperf-tcc
==========

[](#hyperf-tcc)

> 基于 Hyperf 框架的分布式事务 TCC 组件

-Fork version -Original: -Use rabbitmq instead of Nsq

Install
-------

[](#install)

> 在使用 tcc 前先确保已经安装 Redis, Rabbitmq

```
composer require yogcloud/hyperf-tcc

```

发布资源

```
php bin/hyperf.php vendor:publish yogcloud/hyperf-tcc

```

执行迁移

```
php bin/hyperf.php migrate

```

Run
===

[](#run)

```
use YogCloud\TccTransaction\Example\Test;

   /**
    * @GetMapping(path="nsq")
    */
    public function nsq(RequestInterface $req, ResponseInterface $res)
    {
        $goodsId = $req->input('goods_id', 1);
        $couponId = $req->input('coupon_id', 0);
        $result = (new Test())->handle($goodsId,$couponId);
        return $res->json([
           'code' => 0,
           'data' => $result,
        ]);
    }
```

Test
====

[](#test)

> [Benchmark](./Benchmark.md)

```
curl http://localhost:9501/index/nsq?goods_id=1&coupon_id=0

{"code":0,"data":{"order":{"order_sn":255041892524236800,"body":"购买桃子","total_fee":"200.00","goods_id":1,"id":2483},"goods":{"id":1,"price":"200.00","name":"桃子","num":9994,"lock":0,"sale":6},"coupon":null}}
```

注意事项
====

[](#注意事项)

- 分布式事务, 本身是为了确保数据一致性, 在高并发测试下, 应该对接口服务做好限流(使用`RateLimit`组件)

功能列表
----

[](#功能列表)

- 实现TCC操作实现接口 `TccOption`
- 实现事务处理 `Tcc`
- 实现事务编排 `Tcc->rely(...)`
- 实现事务重试 `NSQ消息订阅`
- 实现无法回滚事务记录并通知 `Database`

Composer依赖
----------

[](#composer依赖)

- `"hyperf/redis": "~2.1.0"`
- `"hyperf/database": "~2.1.0"`
- `"hyperf/snowflake": "^2.1"`
- `"hyperf/logger": "~2.1.0"`
- `"hyperf/db-connection": "~2.1.0"`

使用方式
----

[](#使用方式)

- `Tcc` 事务创建
    - 对于事务进行流程编排
    - 对于事务的启动操作
- `TccOption` 事务操作
    - 需要针对性实现 `try, confirm, cancel` 方法
    - 例如 `商品库存锁定`, `商品库存扣除` 等
    - 在 `YogCloud\TccTransaction\Example\Tcc` 下能看到很多演示写法
    - 不应当把 `复杂的参数` 放到该类中去例如对象等, 因为它会作为一个 `序列化的类` 存放到 `redis` 中
    - 如果操作类 `参数过多`, 或者 `属性` 中 `对象过多` 会造成 `存储负担`
    - 不推荐在 `TccOption` 操作类中写业务逻辑, 它应当作为一个调用服务的封装
- `TccState` 事务状态
    - 存放操作和状态并且序列化到缓存中
- `Coordinator/TccCoordinator` 事务协调者 `NSQ消费者` 需要加入到消费进程
    - 请先继承该类实现 `NSQ消费者进程绑定 @Consumer 注解`
    - 具体查看 [Hyperf NSQ文档](https://hyperf.wiki/2.0/#/zh-cn/nsq)

### 代码演示

[](#代码演示)

- 下面的代码演示都在 `Example` 中有实现
- `Example\Tcc\*` 事务操作项
- `Example\Service\*` 微服务实现类
- `Example\Test` 模拟下单接口
- `Example\database.sql` 演示案例数据库脚本, 测试前先导入

```
use YogCloud\TccTransaction\Tcc;
use YogCloud\TccTransaction\Example\Tcc\GoodsLockTcc;
use YogCloud\TccTransaction\Example\Tcc\CouponLockTcc;
use YogCloud\TccTransaction\Example\Tcc\OrderTcc;
use YogCloud\TccTransaction\Example\Tcc\GoodsSubTcc;
use YogCloud\TccTransaction\Example\Tcc\CouponSubTcc;
use YogCloud\TccTransaction\Example\Tcc\OrderMessageTcc;
use YogCloud\TccTransaction\Example\Tcc\OrderStatisticsTcc;

$goodsId = 1;
$couponId = 0;
$tcc = new Tcc;
$tcc
    ->tcc(1, new GoodsLockTcc($goodsId)) // 商品库存锁定
    ->tcc(2, new CouponLockTcc($couponId)) // 优惠券锁定
    ->tcc(3, new OrderTcc) // 创建订单
    ->tcc(4, new GoodsSubTcc) // 扣减库存
    ->tcc(5, new CouponSubTcc) // 占用优惠券
    ->tcc(6, new OrderMessageTcc) // 创建订单消息
    ->tcc(7, new OrderStatisticsTcc) // 订单统计
    ->rely([          // 配置执行流程
        // 外层步骤是逐步执行的
        // [...] 内层步骤是同步执行的
        [1, 2],       // 1,2 锁定库存, 锁定优惠券
        [3],          // 3 创建订单
        [4, 5, 6, 7], // 4,5,6,7 扣减库存, 占用优惠券, 订单消息, 订单统计
    ])->begin(); // 开启事务
```

实现原理
----

[](#实现原理)

- `TccOption` 都必须实现 `try, confirm, cancel` 方法
- 其中 `confirm` 允许空操作

[![](https://camo.githubusercontent.com/084cf51cbc40386d244c77879113ddd4f84d0590173292dd7686a0ed9451ed11/68747470733a2f2f6836706c61792e6f73732d636e2d7368656e7a68656e2e616c6979756e63732e636f6d2f70726f636573732e706e67)](https://camo.githubusercontent.com/084cf51cbc40386d244c77879113ddd4f84d0590173292dd7686a0ed9451ed11/68747470733a2f2f6836706c61792e6f73732d636e2d7368656e7a68656e2e616c6979756e63732e636f6d2f70726f636573732e706e67)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 64.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Recently: every ~33 days

Total

7

Last Release

1686d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1cf057c48c552ae99a41792b1df6704e1a5348a181c5b2098087face419e38d0?d=identicon)[wangrunxinyes](/maintainers/wangrunxinyes)

---

Top Contributors

[![h6play](https://avatars.githubusercontent.com/u/61290455?v=4)](https://github.com/h6play "h6play (18 commits)")[![cexll](https://avatars.githubusercontent.com/u/26520956?v=4)](https://github.com/cexll "cexll (6 commits)")[![wangrunxinyes](https://avatars.githubusercontent.com/u/10919227?v=4)](https://github.com/wangrunxinyes "wangrunxinyes (2 commits)")[![NuoSummer](https://avatars.githubusercontent.com/u/55865730?v=4)](https://github.com/NuoSummer "NuoSummer (1 commits)")[![visionzk](https://avatars.githubusercontent.com/u/16333718?v=4)](https://github.com/visionzk "visionzk (1 commits)")

---

Tags

hyperftcctransactionyogcloud

### Embed Badge

![Health badge](/badges/wangrunxinyes-hyperf-tcc/health.svg)

```
[![Health](https://phpackages.com/badges/wangrunxinyes-hyperf-tcc/health.svg)](https://phpackages.com/packages/wangrunxinyes-hyperf-tcc)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
