mead-steve/tale
Composer 安装命令:
composer require mead-steve/tale
包简介
README 文档
README
What?
Tale is a small library to help write a "distributed transaction like" object across a number of services. It's loosely based on the saga pattern. A good intro is available on the couchbase blog: https://blog.couchbase.com/saga-pattern-implement-business-transactions-using-microservices-part/
Installation
composer require mead-steve/tale
Example Usage
An example use case of this would be some holiday booking software broken down into a few services.
Assuming we have the following services: Flight booking API, Hotel booking API, and a Customer API.
We'd write the following steps:
class DebitCustomerBalanceStep implements Step { //.. Some constructor logic for initialising the api etc... public function execute(CustomerPurchase $state) { $paymentId = $this->customerApi->debit($state->Amount); return $state->markAsPaid($paymentId); } public function compensate($state): void { $this->customerApi->refundAccountForPayment($state->paymentId) }
class BookFlightStep implements Step { //.. Some constructor logic for initialising the api etc... public function execute(FlightPurchase $state) { $flightsBookingRef = $this->flightApi->buildBooking( $state->Destination, $state->Origin, self::RETURN, $this->airline ); if ($flightsBookingRef=== null) { raise \Exception("Unable to book flights"); } return $state->flightsBooked($flightsBookingRef); } public function compensate($state): void { $this->customerApi->cancelFlights($state->flightsBookingRef) }
and so on for any of the steps needed. Then in whatever is handling the user's request a distributed transaction can be built:
$transaction = (new Transaction()) ->add(new DebitCustomerBalance($user)) ->add(new BookFlightStep($airlineOfChoice)) ->add(new BookHotelStep()) ->add(new EmailCustomerDetailsOfBookingStep()) $result = $transaction ->run($startingData) ->throwFailures() ->finalState();
If any step along the way fails then the compensate method on each step is called in reverse order until everything is undone.
State immutability
The current state is passed from one step to the next. This same state is also used to compensate for the transactions in the event of a failure further on in the transaction. Since this is the case it is important that implementations consider making the state immutable.
Tale provides a CloneableState interface to help with this. Any state implementing
this interface will have its cloneState method called before being passed to a step
ensuring that steps won't share references to the same state.
class FakeState implements CloneableState { public function cloneState() { return clone $this; } } $stepOne = new LambdaStep( function (MyStateExample $state) { $state->mutateTheState = "step one" return $state; } ); $stepTwo = new LambdaStep( function (MyStateExample $state) { $state->mutateTheState = "step two" return $state; } ); $transaction = (new Transaction()) ->add($stepOne) ->add($stepTwo); $startingState = new MyStateExample(); $finalState = $transaction->run($startingState)->finalState();
In the example above $startingState, $finalState and $state given to both function
calls are all clones of each other so changing one won't affect any earlier states.
Testing / Development
Contributions are very welcome. Please open an issue first if the change is large or will break backwards compatibility.
All builds must pass the travis tests before merge.
Running ./run_tests.sh will run the same tests as travis.yml but locally.
The dockerfile provides an environment that can execute all the tests & static analysis.
mead-steve/tale 适用场景与选型建议
mead-steve/tale 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 262 次下载、GitHub Stars 达 24, 最近一次更新时间为 2018 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mead-steve/tale 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mead-steve/tale 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 262
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-18