近些年,随着微服务的广泛使用,业务对系统的分布式事务处理能力的要求越来越高。
早期的基于XA协议的二阶段提交方案,将分布式事务的处理放在数据库驱动层,实现了对业务的无侵入,但是对数据的锁定时间很长,性能较低。
现在主流的TCC事务方案和SAGA事务方案,都是基于业务补偿机制,虽然没有全局锁,性能很高,但是一定程度上入侵了业务逻辑,增加了业务开发人员的开发时间和系统维护成本。
新兴的AT事务解决方案,例如Seata和Seata-golang,通过数据源代理层的资源管理器RM记录SQL回滚日志,跟随本地事务一起提交,大幅减少了数据的锁定时间,性能好且对业务几乎没有侵入。其缺点是支持的语言比较单一,例如Seata只支持Java语言类型的微服务,Seata-golang只支持Go语言类型的微服务。
为了突破AT事务对业务编程语言的限制,现在业界正在往DB Mesh的方向发展,通过将事务中间件部署在SideCar的方式,达到任何编程语言都能使用分布式事务中间件的效果。
DBPack是一个处理分布式事务的数据库代理,其能够拦截MySQL流量,生成对应的事务回滚镜像,通过与ETCD协调完成分布式事务,性能很高,且对业务没有入侵,能够自动补偿SQL操作,支持接入任何编程语言。DBPack还支持TCC事务模式,能够自动补偿HTTP请求。目前其demo已经有Java、Go、Python和PHP,TCC的sample也已经在路上了,demo示例可以关注dbpack-samples。
最新版DBPack不仅支持预处理的sql语句,还支持text类型的sql。DBPack最新版还兼容了php8的pdo_mysql扩展。Mysql 客户端在给用户发送 sql 执行结果时,如果执行没有异常,发送的第一个包为 OKPacket,该包中有一个标志位可以标识 sql 请求是否在一个事务中。如下图所示

这个包的内容为:
07 00 00 // 前 3 个字节表示 payload 的长度为 7 个字节01 // sequence 响应的序号,前 4 个字节一起构成了 OKPacket 的 header00 // 标识 payload 为 OKPacket00 // affected row00 // last insert id03 00 // 状态标志位00 00 // warning 数量dbpack 之前的版本将标志位设置为 0,java、golang、.net core、php 8.0 之前的 mysql driver 都能正确协调事务,php 8.0 的 pdo driver 会对标志位进行校验,所以 php 8.0 以上版本在使用 dbpack 协调分布式事务时,会抛出 transaction not active 异常。最新版本已经修复了这个问题。
下图是具体的DBPack事务流程图。

其事务流程简要描述如下:
本文将以PHP语言为例,详细介绍如何使用PHP对接DBPack完成分布式事务。实际使用其他语言时,对接过程也是类似的。
ETCD_VER=v3.5.3# choose either URLGOOGLE_URL=https://storage.googleapis.com/etcdGITHUB_URL=https://github.com/etcd-io/etcd/releases/downloadDOWNLOAD_URL=${GOOGLE_URL}rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gzrm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-testcurl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gztar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz/tmp/etcd-download-test/etcd --version/tmp/etcd-download-test/etcdctl version/tmp/etcd-download-test/etcdutl versionundo_log表用于存储本地事务的回滚镜像。
-- ------------------------------ Table structure for undo_log-- ----------------------------DROP TABLE IF EXISTS `undo_log`;CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) NOT NULL, `context` varchar(128) NOT NULL, `rollback_info` longblob NOT NULL, `log_status` int(11) NOT NULL, `log_created` datetime NOT NULL, `log_modified` datetime NOT NULL, `ext` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_unionkey` (`xid`,`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;# 更新distributed_transaction.etcd_config.endpoints# 更新listeners配置项,调整为实际聚合层服务的地址和端口# 更新filters配置项,配置聚合层服务的API endpointvim /path/to/your/aggregation-service/config-aggregation.yaml# 更新distributed_transaction.etcd_config.endpoints# 更新listeners配置项,配置业务数据库信息,包括dbpack代理的端口# 更新data_source_cluster.dsnvim /path/to/your/business-service/config-service.yamlgit clone git@github.com:cectc/dbpack.gitcd dbpack# build on local envmake build-local# build on production envmake build./dist/dbpack start --config /path/to/your/config-aggregation.yaml./dist/dbpack start --config /path/to/your/config-service.yaml以Nginx为例,配置如下
server { listen 3001; # 暴露的服务端口 index index.php index.html; root /var/www/code/; # 业务代码根目录 location / { try_files $uri /index.php?$args; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass order-svc-app:9000; # php-fpm 端口 fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; }}<?phpclass AggregationSvc{ public function CreateSo(string $xid, bool $rollback): bool { $createSoSuccess = $this->createSoRequest($xid); if (!$createSoSuccess) { return false; } $allocateInventorySuccess = $this->allocateInventoryRequest($xid); if (!$allocateInventorySuccess) { return false; } if ($rollback) { return false; } return true; } // private function createSoRequest(string $xid) ... // private function allocateInventoryRequest(string $xid) ...}$reqPath = strtok($_SERVER["REQUEST_URI"], '?');$reaHeaders = getallheaders();$xid = $reaHeaders['X-Dbpack-Xid'] ?? '';if (empty($xid)) { die('xid is not provided!');}$aggregationSvc = new AggregationSvc();if ($_SERVER['REQUEST_METHOD'] === 'POST') { switch ($reqPath) { case '/v1/order/create': if ($aggregationSvc->CreateOrder($xid, false)) { responseOK(); } else { responseError(); } case '/v1/order/create2': if ($aggregationSvc->CreateSo($xid, true)) { responseOK(); } else { responseError(); } break; default: die('api not found'); }}function responseOK() { http_response_code(200); echo json_encode([ 'success' => true, 'message' => 'success', ]);}function responseError() { http_response_code(400); echo json_encode([ 'success' => false, 'message' => 'fail', ]);}<?phpclass OrderDB{ private PDO $_connection; private static OrderDB $_instance; private string $_host = 'dbpack-order'; private int $_port = 13308; private string $_username = 'dksl'; private string $_password = '123456'; private string $_database = 'order'; const insertSoMaster = "INSERT /*+ XID('%s') */ INTO order.so_master (sysno, so_id, buyer_user_sysno, seller_company_code, receive_division_sysno, receive_address, receive_zip, receive_contact, receive_contact_phone, stock_sysno, payment_type, so_amt, status, order_date, appid, memo) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,now(),?,?)"; const insertSoItem = "INSERT /*+ XID('%s') */ INTO order.so_item(sysno, so_sysno, product_sysno, product_name, cost_price, original_price, deal_price, quantity) VALUES (?,?,?,?,?,?,?,?)"; public static function getInstance(): OrderDB { if (empty(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } private function __construct() { try { $this->_connection = new PDO( "mysql:host=$this->_host;port=$this->_port;dbname=$this->_database;charset=utf8", $this->_username, $this->_password, [ PDO::ATTR_PERSISTENT => true, PDO::ATTR_EMULATE_PREPARES => false, // to let DBPack handle prepread sql ] ); } catch (PDOException $e) { die($e->getMessage()); } } private function __clone() { } public function getConnection(): PDO { return $this->_connection; } public function createSo(string $xid, array $soMasters): bool { $this->getConnection()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $this->getConnection()->beginTransaction(); foreach ($soMasters as $master) { if (!$this->insertSo($xid, $master)) { throw new PDOException("failed to insert soMaster"); } } $this->getConnection()->commit(); } catch (PDOException $e) { $this->getConnection()->rollBack(); return false; } return true; } private function insertSo(string $xid, array $soMaster): bool { // insert into so_master, so_item ... }}$reqPath = strtok($_SERVER["REQUEST_URI"], '?');$reqHeaders = getallheaders();$xid = $reqHeaders['Xid'] ?? '';if (empty($xid)) { die('xid is not provided!');}if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($reqPath === '/createSo') { $reqBody = file_get_contents('php://input'); $soMasters = json_decode($reqBody, true); $orderDB = OrderDB::getInstance(); $result = $orderDB->createSo($xid, $soMasters); if ($result) { responseOK(); } else { responseError(); } }}function responseOK() { http_response_code(200); echo json_encode([ 'success' => true, 'message' => 'success', ]);}function responseError() { http_response_code(400); echo json_encode([ 'success' => false, 'message' => 'fail', ]);}curl -X{HTTP Method} http://localhost:{DBPack监听的聚合层服务端口}/{聚合层服务的API endpoint}mysql_connect()连接数据库(<=php5.4),在start transaction;开始之后,后续的业务操作必须在同一个数据库连接上进行。insert /*+ XID('%s') */ into xx ...;卜贺贺。就职于日本楽天Rakuten CNTD,任Application Engineer,熟悉AT事务、Seata-golang和DBPack。GitHub:https://github.com/bohehe