Seata 事务执行流程分析

全局事务的执行日志

修改配置:

bash 复制代码
logging.level.io.seata=debug
bash 复制代码
TransactionPropagationInterceptor - [preHandle,44] - xid in RootContext[null] xid in HttpContext[null]
AbstractNettyRemotingClient - [sendSyncRequest,164] - offer message: timeout=60000,transactionName=scorpios-create-order
AbstractNettyRemoting - [sendAsync,220] - write message:SeataMergeMessage timeout=60000,transactionName=scorpios-create-order
, channel:[id: 0x758aa084, L:/172.16.5.39:55534 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.TmNettyRemotingClient@2344121 msgId:18, body:MergeResultMessage xid=192.168.14.236:8091:6737814217289867327;extraData=;
RootContext - [bind,112] - bind 192.168.14.236:8091:6737814217289867327
DefaultGlobalTransaction - [begin,109] - Begin new global transaction [192.168.14.236:8091:6737814217289867327]
OrderServiceImpl - [create,35] - ----->开始新建订单
AbstractNettyRemotingClient - [sendSyncRequest,164] - offer message: xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_order:33
AbstractNettyRemoting - [sendAsync,220] - write message:SeataMergeMessage xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_order:33
, channel:[id: 0x4cd5c7f0, L:/172.16.5.39:55487 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.RmNettyRemotingClient@3077b8c6 msgId:19, body:MergeResultMessage BranchRegisterResponse: branchId=6737814217289867328,result code =Success,getMsg =null
AbstractUndoLogManager - [flushUndoLogs,239] - Flushing UNDO LOG: {"@class":"io.seata.rm.datasource.undo.BranchUndoLog","xid":"192.168.14.236:8091:6737814217289867327","branchId":6737814217289867328,"sqlUndoLogs":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.undo.SQLUndoLog","sqlType":"INSERT","tableName":"t_order","beforeImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords$EmptyTableRecords","tableName":"t_order","rows":["java.util.ArrayList",[]]},"afterImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","tableName":"t_order","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"id","keyType":"PRIMARY_KEY","type":-5,"value":["java.lang.Long",33]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"user_id","keyType":"NULL","type":-5,"value":["java.lang.Long",1]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"product_id","keyType":"NULL","type":-5,"value":["java.lang.Long",1]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"count","keyType":"NULL","type":4,"value":10},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"money","keyType":"NULL","type":3,"value":["java.math.BigDecimal",100]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"status","keyType":"NULL","type":4,"value":0}]]}]]}}]]}
OrderServiceImpl - [create,40] - ----->订单微服务开始调用库存,做扣减Count
OrderServiceImpl - [create,42] - ----->订单微服务开始调用库存,做扣减end
OrderServiceImpl - [create,45] - ----->订单微服务开始调用账户,做扣减Money
OrderServiceImpl - [create,47] - ----->订单微服务开始调用账户,做扣减end
OrderServiceImpl - [create,50] - ----->修改订单状态开始
AbstractNettyRemotingClient - [sendSyncRequest,164] - offer message: xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_order:33
AbstractNettyRemoting - [sendAsync,220] - write message:SeataMergeMessage xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_order:33
, channel:[id: 0x4cd5c7f0, L:/172.16.5.39:55487 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.RmNettyRemotingClient@3077b8c6 msgId:21, body:MergeResultMessage BranchRegisterResponse: branchId=6737814217289867331,result code =Success,getMsg =null
AbstractUndoLogManager - [flushUndoLogs,239] - Flushing UNDO LOG: {"@class":"io.seata.rm.datasource.undo.BranchUndoLog","xid":"192.168.14.236:8091:6737814217289867327","branchId":6737814217289867331,"sqlUndoLogs":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.undo.SQLUndoLog","sqlType":"UPDATE","tableName":"t_order","beforeImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","tableName":"t_order","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"id","keyType":"PRIMARY_KEY","type":-5,"value":["java.lang.Long",33]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"status","keyType":"NULL","type":4,"value":0}]]}]]},"afterImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","tableName":"t_order","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"id","keyType":"PRIMARY_KEY","type":-5,"value":["java.lang.Long",33]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"status","keyType":"NULL","type":4,"value":1}]]}]]}}]]}
OrderServiceImpl - [create,52] - ----->修改订单状态结束
OrderServiceImpl - [create,54] - ----->下订单结束了
AbstractNettyRemotingClient - [sendSyncRequest,164] - offer message: xid=192.168.14.236:8091:6737814217289867327,extraData=null
AbstractNettyRemoting - [sendAsync,220] - write message:SeataMergeMessage xid=192.168.14.236:8091:6737814217289867327,extraData=null
, channel:[id: 0x758aa084, L:/172.16.5.39:55534 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.TmNettyRemotingClient@2344121 msgId:20, body:MergeResultMessage globalStatus=Committed,ResultCode=Success,Msg=null
DefaultGlobalTransaction - [suspend,188] - Suspending current transaction, xid = 192.168.14.236:8091:6737814217289867327
RootContext - [unbind,140] - unbind 192.168.14.236:8091:6737814217289867327
DefaultGlobalTransaction - [commit,143] - [192.168.14.236:8091:6737814217289867327] commit status: Committed

库存模块日志:

bash 复制代码
RootContext - [bind,112] - bind 192.168.14.236:8091:6737814217289867327
TransactionPropagationInterceptor - [preHandle,44] - xid in RootContext[192.168.14.236:8091:6737814217289867327] xid in HttpContext[192.168.14.236:8091:6737814217289867327]
StorageServiceImpl - [decrease,22] - ------->storage-service中扣减库存开始
AbstractNettyRemotingClient - [sendSyncRequest,164] - offer message: xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_storage:1
AbstractNettyRemoting - [sendAsync,220] - write message:SeataMergeMessage xid=192.168.14.236:8091:6737814217289867327,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,lockKey=t_storage:1
, channel:[id: 0x0030614b, L:/172.16.5.39:55514 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.RmNettyRemotingClient@21aeb302 msgId:18, body:MergeResultMessage BranchRegisterResponse: branchId=6737814217289867329,result code =Success,getMsg =null
AbstractUndoLogManager - [flushUndoLogs,239] - Flushing UNDO LOG: {"@class":"io.seata.rm.datasource.undo.BranchUndoLog","xid":"192.168.14.236:8091:6737814217289867327","branchId":6737814217289867329,"sqlUndoLogs":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.undo.SQLUndoLog","sqlType":"UPDATE","tableName":"t_storage","beforeImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","tableName":"t_storage","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"id","keyType":"PRIMARY_KEY","type":-5,"value":["java.lang.Long",1]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"used","keyType":"NULL","type":4,"value":83},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"residue","keyType":"NULL","type":4,"value":17}]]}]]},"afterImage":{"@class":"io.seata.rm.datasource.sql.struct.TableRecords","tableName":"t_storage","rows":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Row","fields":["java.util.ArrayList",[{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"id","keyType":"PRIMARY_KEY","type":-5,"value":["java.lang.Long",1]},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"used","keyType":"NULL","type":4,"value":93},{"@class":"io.seata.rm.datasource.sql.struct.Field","name":"residue","keyType":"NULL","type":4,"value":7}]]}]]}}]]}
StorageServiceImpl - [decrease,24] - ------->storage-service中扣减库存结束
RootContext - [unbind,140] - unbind 192.168.14.236:8091:6737814217289867327 
XidResource - [cleanXid,38] - unbind[192.168.14.236:8091:6737814217289867327] from RootContext
AbstractNettyRemoting - [processMessage,268] - io.seata.core.rpc.netty.RmNettyRemotingClient@21aeb302 msgId:52, body:xid=192.168.14.236:8091:6737814217289867327,branchId=6737814217289867329,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,applicationData=null
RmBranchCommitProcessor - [process,56] - rm client handle branch commit process:xid=192.168.14.236:8091:6737814217289867327,branchId=6737814217289867329,branchType=AT,resourceId=jdbc:mysql://192.168.14.239:3306/ultra_sdc0516,applicationData=null
AbstractRMHandler - [doBranchCommit,96] - Branch committing: 192.168.14.236:8091:6737814217289867327 6737814217289867329 jdbc:mysql://192.168.14.239:3306/ultra_sdc0516 null
AbstractRMHandler - [doBranchCommit,104] - Branch commit result: PhaseTwo_Committed
RmBranchCommitProcessor - [handleBranchCommit,65] - branch commit result:xid=192.168.14.236:8091:6737814217289867327,branchId=6737814217289867329,branchStatus=PhaseTwo_Committed,result code =Success,getMsg =null
AbstractNettyRemoting - [sendAsync,220] - write message:xid=192.168.14.236:8091:6737814217289867327,branchId=6737814217289867329,branchStatus=PhaseTwo_Committed,result code =Success,getMsg =null, channel:[id: 0x0030614b, L:/172.16.5.39:55514 - R:/192.168.14.236:8091],active?true,writable?true,isopen?true
AbstractUndoLogManager - [batchDeleteUndoLog,162] - batch delete undo log size 1

服务调用过程中,全局事务方法会传递 TX_XID 给下一个服务调用的,然后通过 TransactionPropagationInterceptor 从请求头重获取该全局事务ID。

相关推荐
Stringzhua14 天前
【SpringCloud】Seata微服务事务
spring cloud·微服务·seata
君哥聊技术15 天前
Apache Seata 新版本集成了 RocketMQ 事务消息
rocketmq·seata
IT云清18 天前
Apache Seata Raft模式配置中心
java·分布式·apache·seata·分布式事务
灰色孤星A1 个月前
分布式事务学习笔记(四)微服务实现Stata AT模式、Stata Saga模式介绍
微服务·seata·分布式事务·at模式·saga模式·微服务实现seata
灰色孤星A2 个月前
分布式事务学习笔记(二)Seata架构、TC服务器部署、微服务集成Seata
分布式·微服务·架构·seata·分布式事务·tc服务器·微服务集成seata
江喜原2 个月前
Seata
数据库·分布式·mysql·seata
长安初雪2 个月前
微服务集成 Seata
微服务·云原生·架构·seata·分布式事务
马剑威(威哥爱编程)3 个月前
分布式事务Seata的4种模式详解
java·分布式·spring cloud·事务·seata·威哥爱编程
真我的风采i3 个月前
初始化seata在nacos的配置报错nacos-config.sh 127.0.0.1-nacos-config.txt-文件不存在
java·windows·git·nacos·seata·sh脚本
ideal-cs3 个月前
索引:SpringCloudAlibaba分布式组件全部框架笔记
nacos·gateway·sentinel·seata·springcloud·openfeign·cloudalibaba