In layman's terms, Seata-php is the PHP language implementation of seata, which realizes the interoperability between Java and PHP, so that PHPer can also use seata-php to realize distributed transactions.
Seata is a very mature distributed transaction framework and is the de facto standard platform for distributed transaction technology in the Java field. Seata is currently building its multi-language system [Reference Document 1]. The whole system includes five commonly used languages: Java, Go, Python, Js and PHP. The current situation is that the latter four languages are based on the Seata Java version. The implementation of the corresponding language.
In addition to the reason that the PHP version of Seata is required to be built due to the open source value in the pursuit of the Seata multi-language system, as a key role in building the LAMP architecture of the technology foundation in the Web 1.0 era, the PHP language is still widely used in e-commerce and financial transaction scenarios. , and these scenarios have very strong requirements for data consistency, which is the biggest incentive to build Seata-php and its technical value.
1 Seata Architecture and Multilingual System
The picture comes from the seata official website
The overall architecture of Seata consists of the following roles:
Transaction Coordinator
TC for short, maintains the state of global transactions and branch transactions, and drives the commit or rollback of global transactions.
Transaction Manager
TM for short, defines the scope of the global transaction, commits or rolls back the global transaction.
Resource Manager
RM for short, in the same application as the branch transaction, registering the branch transaction, reporting the status of the branch transaction, and driving the commit or rollback of the branch transaction.
From the perspective of C/S communication architecture, TC is the server, TM and RM are the clients. The netty framework is used for long-link communication between TC and TM and each RM. Specifically, the communication protocol of Seata Java version defines a private binary bidirectional communication protocol on top of the four-layer TCP protocol, and the communication framework uses netty. The other four languages can communicate and call services between any languages in the multilingual ecosystem as long as their communication functions are implemented according to Seata's communication protocol standards.
Among the three roles, TM and RM are called by the upper-layer APP in the form of SDK API, while TC is deployed in an independent process and can be implemented in any language. It is said that laziness is the first virtue of programmers. Under the circumstance that Seata Java has already implemented the Java version of TC, there is no need to repeat the work for other languages in the multi-language system, and only need to build the TM and RM SDKs of their corresponding languages. API package to communicate with Seata Java TC.
2 Seata and PHP technology
Distributed transaction technology is a part of the microservice technology system. To build Seata PHP, you first need to choose its microservice technology platform. The microservice framework currently used by Seata-php is hyperf.
PHP is known in the industry for its low entry threshold. Currently, the commonly used microservice frameworks include laravel and lumen built on it. The biggest advantage of the laravel framework is that it is rich in ecology and has all kinds of components. If laravel can be compared to the Spring framework, lumen is Spring Boot. But its disadvantage is that its performance is worrying. For example, on an ordinary 8C machine, if an HTTP service running only echo logic is run empty, its throughput is only 1k qps.
The Hyperf framework is a microservice framework developed by Chinese people based on swoole in recent years. The features are as follows:
- 1 Similar to nginx, hyperf resides in memory in the form of multiple processes, and each process has an elastic thread pool. Under normal circumstances, after hyperf receives a call request, it can ensure that service threads are allocated within 1ms, and the response time of lumen is often around 10ms;
- 2 Because of the resident memory characteristics of the hyperf service, its stability is good, and the resource utilization rate is of course much lower than that of lumen running with the cgi mechanism;
- 3 The request processing process of hyperf draws on the Go language mechanism. Its runtime layer executes the upper-layer user synchronous call asynchronously. Compared with lumen, its throughput rate is high and the delay is low. For example, using hyperf to achieve the same echo HTTP in the same environment service, which can easily reach 60k qps;
In addition to the stability and high performance of hyperf itself, relying on the resident memory characteristics of the hyperf service process, TC can easily initiate two-stage transaction processing for the RM of seata-php, that is, the Java TC as the server is to the PHP version as the client. The RM initiates the RPC callback. If lumen is used as the microservice framework of seata-php, it is almost impossible to achieve this technical point.
3 Quick Start with Seata-php
Based on the hyperf microservice framework, seata-php has implemented the AT transaction mode and given test cases. The purpose of this chapter is to enable students who are interested in the seata-php project to quickly get started with seata-php based on the existing implementation.
3.1 Build a PHP development environment
Using hyperf/box this tool can quickly create a development environment, and can be isolated from other self-built development tool chains to avoid polluting the daily development environment.
3.1.1 Download hyperf/box
# Mac
wget https://github.com/hyperf/box/releases/download/v0.0.3/box_php8.1_x86_64_macos -O box
# Linux x86_64
wget https://github.com/hyperf/box/releases/download/v0.0.3/box_php8.1_x86_64_linux -O box
# Linux aarch64
wget https://github.com/hyperf/box/releases/download/v0.0.3/box_php8.1_aarch64_linux -O box
sudo mv ./box /usr/local/bin/box
sudo chmod +x /usr/local/bin/box
# 在 https://github.com/settings/tokens/new 创建 token 后,配置到 box 中
box config set github.access-token <Your Token>
Notice:
- If you are a Mac user for the first time, you need to authorize the box tool in
“系统偏好设置”->“安全性与隐私”
- It has been tested, x86 box, can be used on the M1 version of the Mac
- When using box, creating github access token permissions requires
repo
,workflow
3.1.2 Configuring the PHP environment
When
box
is downloaded, continue to downloadphp8.0
version# 下载 php8.0 box get php@8.0 # 将 box 设置为 php8.0 版本 box config set-php-version 8.0
- If you are a Mac user for the first time, you need to authorize the box tool in
3.1.3 Download composer
# 下载 composer
box get composer
3.2 Running Seata-php
After the environment is set up, find a directory to store the code of the seata-php project.
# 找个地方创建一个目录
mkdir ./seata
# 进入到目录内
cd ./seata
# 下载 seata 骨架包
git clone https://github.com/PandaLIU-1111/seata-skeleton
# 下载 seata/seata-php 组件包
git clone git@github.com:seata/seata-php.git
# 进入到 seata骨架包内
cd seata-skeleton
# 执行 composer 更新项目内的组件包
box composer update -o
# 查看是否与 seata/seata-php 建立软连接
ls -al vendor/hyperf/ | grep seata
# 查看命令执行后是否有以下内容
...
seata -> ../../../seata-php/ // 与 seata/seata-php 包建立软连接
...
# 启动项目
box php bin/hyperf.php start
At this point, you can see that seata-php runs successfully, and you can see the interactive messages between the seata-php client and the seata Java server TC on the command line.
3.3 Project code style
Seata-php follows the PSR-1 code specification [Ref 2].
The community provides a code formatting tool that is similar to the Go language gofmt
composer cs-fix
. The specific usage is:
# 格式化某个文件
composer cs-fix ${FileName}
# 格式化某个目录
composer cs-fix ${DirName}
3.4 Test Cases
At present, seata-php only provides single test cases, which are placed in the project tests
directory, and these single test cases can be executed directly through the composer test
command. In the near future, we will configure these single test cases on github actions to test the pr of each commit.
Next, we'll add integration test cases like seata-go and configure each pr on github actions for automated testing of the project.
4 Planning for the second half of the year
The existing work of Seata-php is only the first step of the long march in the second half of the year, and it has not yet reached the state of production availability. The overall goals for the second half of the year are:
- 1 Transaction mode aligns TCC, AT, SATA and XA modes for seata java v1.6.0 to be released in September
- 2 The single test coverage rate of test cases is more than 70%, and the php version of the existing integration test cases in seata java in two modes is implemented
- 3 The code samples implement the php version of the existing samples in seata java in two modes
- 4 Document Construction Build detailed documentation at the API interface level
- 5 Production cases More than 3 actual production users
- 6 Community building and training seata committers for more than 5 people
The above goals can be understood as KPIs of the seata-php community. In order to achieve the purpose, there are the following execution plans which can be divided into "three steps".
4.1 Publish a usable version
This is the first stage. We plan to release the first GA version around the National Day. The detailed technical points are as follows:
1 Implement TM and RM
作为分布式事务的发起方,TM 在与下游的微服务应用在通信时,能够在 HTTP 协议与 GRPC 协议中,传递事务上下文,下游的服务也可以随时加入到事务中。
2 Implementing the distributed lock API
用于避免业务数据在一阶段与二阶段之间,由于并发被修改,导致二阶段提交、回滚失效。
3 Implement TCC and AT modes
完全实现 TCC 模式。<br /> 而 AT 模式依赖于具体的 DB 类型和 DB 版本,我们把 DB 限定为 MySQL v5.7,在此之上支持最基本的 INSERT 与 UPDATE 语句,基本可以完成大部分的实际应用场景覆盖。
4 Support registry
支持注册中心的目的,是方便 TM 和 RM 对 TC 进行微服务发现。将会支持 File 与 nacos 两种服务发现方式。<br /> 优先支持 File 服务发现方式。其好处是,在 K8s 环境下,可以通过环境变量或者是挂载 configmap,实现动态配置,不依赖人力变更。<br /> 其次支持 nacos 作为注册中心的服务发现方式。目前,国内的 阿里云、腾讯云、华为云等主流云厂商都支持 nacos 注册中心,可以方便广大用户进行服务联通。
5 other
如 自动化的单元测试,集成测试和项目的 samples。
The community has posted all the tasks in the first phase as tasks on the seata-php issue, which makes it easy to check the task leader and track the progress of the project in time to view the current progress directly.
4.2 Comprehensive alignment of technical capabilities
This is the second phase of the half-year target. The version produced at this stage will be a relatively complete version, which can cover most business scenarios and reduce the threshold and cost for developers to use seata-php.
The key technical points are as follows:
1 Implementing XA and SAGA modes
除了补齐这两个模式外,还将继续完善 AT 模式支持的 SQL ,能够做到支持大部分的 SQL 语句。
2 Support Configuration Center
支持配置中心的目的,是方便拉取事务相关的配置。初步计划支持 File、Nacos、Apollo 三种配置方式。
3 Support gRPC
计划于 9 月份发布的 Seata Java v1.6,将支持 gRPC 通信方式。seata-php 在第二阶段也将支持这一 RPC 调动方式进行事务传播。
4 Other databases
首先支持更多的 MySQL 版本,如 v8.0。并支持 PostgreSQL、OceanBase、Redis 等更多类型的 DB。
5 Transaction exception handling
提升分布式事务防悬挂的能力,自动处理请求幂等、空提交、空回滚、资源悬挂等事务异常逻辑。
The deadline for the second phase of the time node is probably around the end of November this year.
4.3 Community Construction
The first two steps mainly focus on the technical capacity building of seata-php itself. At this point, seata-php can be considered technically mature.
The advancement of these two steps first depends on the healthy development of the community itself. After all, the open source project needs to be promoted by community students. At present, the community is in charge of development and growth, and Xingbei is responsible for the overall implementation of the project. Currently, there are 4 code contributors.
Of course, we welcome more students to participate in the code construction of seata-php. When submitting issues and prs, it is recommended to describe the relevant details as thoroughly as possible. for example:
When submitting a bug issue
- The title can be written:
bugfix:NotFoundClass Redis with php version is 7.2
- The content can submit the details of the bug, the details of the phenomenon, the corresponding stack information, the expected situation, the current environmental situation, what happened, the repair opinions, and the supplementary information, the current environmental situation and other information.
- The title can be written:
when submitting pr
- The title can be written:
Feature: AT mode need to support pgsql
- The content can be written: the meaning of this Feature, the expected usage, and other related information.
- The title can be written:
This step is accompanied by the first two steps and is carried out synchronously.
5 Summary
Seata-php has the benchmark of seata java, and in the early stage, it is mainly to promote the progress of the code.
As an open source project, the open source value of seata-php is of course used in the user's production environment, and production users are also part of community construction. At present, two users are willing to verify seata-php in their development and testing environment to help improve the stability, ease of use and code quality of the project.
In order to maintain the healthy and sustainable development of the project and the community, contributors to open source projects include not only coding contributors, but also contributors in document contribution, product promotion and brand promotion. We will organize enthusiastic community participants Publish blogs in major technical forums, and promote technical dry goods and production cases on voice and video websites and technical conferences. Friends who are interested in these jobs are welcome to join the community DingTalk group 44788115 and communicate with us.
Reference documentation
- 1 Seata Multilingual System Construction https://mp.weixin.qq.com/s/UwzscqfuCYtsSdWYj-t-uQ
- 2 PHP PSR-1 Code Specification https://www.php-fig.org/psr/psr-1/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。