Text|
Zhao Xin (flower name: Yu Yu): Ant Group Seata Project Open Source Director, Open Atom Open Source Foundation Code Contribution Star
Guo Cheng (flower name: Xingbei): co-sponsor of Seata-php project, technical expert of Ant Group
Liu Yuejian: Co-founder of Seata-php project, member of Hyperf development team, senior back-end engineer of Guangdong Quick E-Commerce Co., Ltd.
This article 5894 words read 12 minutes
Introduction
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 [1], and the whole system includes five commonly used languages: Java, Go, Python, JS and PHP. The current situation is that the last four languages are based on the Seata Java version to build the corresponding language implementation.
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. . These scenarios have very strong requirements for data consistency, which is the biggest incentive to build seata-php and its technical value.
PART. 1--Seata Architecture and Multilingual System
The picture comes from Seata's 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 the Seata Java version defines a private binary two-way 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. In the case 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. It is only necessary to build the TM and RM SDKs of their corresponding languages. API package to communicate with Seata Java TC.
PART. 2--Seata and PHP technology
Distributed transaction technology is a part of the microservice technology system. To build Seata PHP, you first need to select 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 with the Spring framework, Lumen is Spring Boot. However, its disadvantage is that its performance is worrying. For example, on a common 8C machine, when running an HTTP service that only runs echo logic, 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 the service thread is allocated within 1ms, while the response time of Lumen is usually about 10ms;
2. Because of the characteristics of Hyperf service resident in memory, its stability is good, and the resource utilization rate is of course much lower than that of Lumen running with CGI mechanism;
3. Hyperf's request processing process draws on the Go language mechanism, and its runtime layer executes the upper-layer user synchronous call asynchronously, which has higher throughput and lower latency than Lumen. For example, using Hyperf to implement the same echo HTTP service in the same environment can easily reach 60K QPS;
In addition to Hyperf's own stability and high performance, relying on the resident memory of Hyperf's service process, TC can easily initiate two-stage transaction processing to the RM of seata-php, that is, the Java TC as the Server pairs 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.
PART. 3--Quick start 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, a tool can quickly create a development environment, and it 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>
Note :
- If you are a Mac user for the first time, you need to authorize the Box tool in "System Preferences" --> "Security and Privacy";
- Has been tested, X86 Box, can be used on M1 version 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 download the PHP 8.0 version
# 下载 php8.0
box get php@8.0
# 将 box 设置为 php8.0 版本
box config set-php-version 8.0
3.1.3 Download composer
# 下载 composer
box get composer
3.2--Run 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 更新项目内的组件包
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 [2].
The community provides a code formatting tool similar to the Go language gofmt - composer cs-fix, which is used in the following ways:
# 格式化某个文件
composer cs-fix ${FileName}
# 格式化某个目录
composer cs-fix ${DirName}
3.4--Test Cases
Currently, seata-php only provides single test cases, which are placed in the tests directory of the project. These single test cases can be executed directly through the composer test command. We will soon deploy these single test cases on GitHub actions to test each PR submitted.
Next, we will complement the integration test cases like seata-go and configure each PR on the GitHub action to automatically test the project.
PART. 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] Align the TCC, AT, SATA and XA modes of Seata Java v1.6.0 which will be released in September;
2. [Test Cases] The single test coverage rate is more than 70%, and the PHP version of the existing integration test cases in Seata Java in two modes is implemented;
3. [Code samples] Implement the PHP version of the existing samples in Seata Java in two modes;
4. [Documentation] Build detailed documentation of API interface level;
5. [Production case] There are more than 3 actual production users;
6. [Community Construction] Train more than 5 Seata Committers.
The above goals can be understood as KPIs of the seata-php community. To achieve this, there is the following execution plan which can be divided into "three steps".
4.1 -- release 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
As the initiator of a distributed transaction, when TM communicates with downstream microservice applications, it can transfer the transaction context in the HTTP protocol and gRPC protocol, and downstream services can also join the transaction at any time.
2. Implement distributed lock API
It is used to avoid the failure of the two-phase commit and rollback due to concurrent modification of business data between the first phase and the second phase.
3. Implement TCC and AT modes
Fully implement TCC mode . The AT mode depends on the specific DB type and DB version. We limit the DB to MySQL v5.7, which supports the most basic INSERT and UPDATE statements, and can basically cover most practical application scenarios.
4. Support registry
The purpose of supporting the registry is to facilitate TM and RM to perform microservice discovery on TC. Two service discovery methods, File and Nacos, will be supported.
The File service discovery method is preferentially supported. The advantage is that in the K8s environment, dynamic configuration can be achieved through environment variables or by mounting configmap, without relying on manual changes.
Secondly, it supports Nacos as the service discovery method of the registry. At present, mainstream cloud vendors such as Alibaba Cloud, Tencent Cloud, and Huawei Cloud in China all support the Nacos registration center, which can facilitate the connection of services for users.
5. Others
Such as automated unit tests, integration tests and project samples.
The community has published all the tasks involved in the first stage on the seata-php issue as tasks, which can easily view the task leader and track the progress of the project in time _(directly view the current progress)_.
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. Implement XA and SAGA patterns
In addition to complementing these two modes, it will continue to improve the SQL supported by AT mode, which can support most of the SQL statements.
2. Support configuration center
The purpose of supporting the configuration center is to facilitate the pulling of transaction-related configurations. The preliminary plan supports three configuration methods: File, Nacos, and Apollo.
3. Support gRPC
Seata Java v1.6, scheduled to be released in September, will support gRPC communication. Seata-php will also support this RPC transfer method for transaction propagation in the second phase.
4. Other databases
First to support more MySQL versions, such as v8.0. And supports PostgreSQL, OceanBase, Redis and more types of DB.
5. Transaction exception handling
Improve the ability of distributed transactions to prevent suspension, and automatically handle transaction exception logic such as request idempotence, empty commit, empty rollback, and resource suspension.
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
- Title can be written: bugfix: NotFoundClass Redis with PHP version is 7.2
-Content You 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.
When submitting a 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.
This step is accompanied by the first two steps and is carried out synchronously.
PART. 5--Summary
Seata-php has the benchmark of Seata Java, and in the initial 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 to post blogs in major technical forums, and promote technical dry goods and production cases on voice, 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 document】
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/
Recommended reading of the week
Implementation of Seata in Ant International Banking Business
Seata Multilingual System Construction
In-depth HTTP/3 (2) | Not-so-boring SSL
Full analysis of Go native plugin usage problems
Welcome to scan the code and follow our official account:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。