seata 分布式事务
温馨提示:
本文最后更新于 2025年10月26日,已超过 229 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
运行seata服务
访问页面端口:7091 如:http://127.0.0.1:7091
事务连接端口:8091
项目中引入seata ,注意 启动的seata服务与项目引入的版本保持一致
com.alibaba.cloud
spring-cloud-starter-alibaba-seata
新建file.conf
在 项目的resources下新建file.conf ,指定 seata的服务端口。 不配置改文件 ,服务将启动失败
service {
#transaction service group mapping
vgroupMapping.default_tx_group = "default"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support
enableDegrade = false
#disable seata
disableGlobalTransaction = false
}
注解标注
标注 全局事务协调 :@GlobalTransactional
当方法中涉及到对下游多个微服务的调用时,将此注解标注在最顶层的方法上。
| seata中的名称 | 中文 | 职能 |
|---|---|---|
| TC | 事务协调者 | 维护全局和分支事务的状态,驱动全局事务提交或回滚 |
| TM | 事务管理者 | 定义全局事务的范围,开始全局事务、提交或回滚全局事务 |
| RM | 资源管理器 | 管理分支事务的资源,与TC交谈以注册分支事务或报告分支事务的状态,并驱动分支事务提交或回滚 |
事务的两阶段提交
一阶段:本地事务提交(业务数据+undo_log)
二阶段:成功或失败
成功:所有RM删除undo_log
失败:所有RM拿到自己的前镜像,恢复数据,删除undo_log
seata的四种模式
AT模式:最终一致性的两阶段提交协议,通过自动补偿机制实现数据回滚,无业务侵入,是Seata的默认模式。
TCC模式:最终一致性的两阶段提交协议,需要业务实现Try、Confirm和Cancel三个操作,有业务侵入,灵活度高。
Saga模式:通过将长事务拆分为多个短事务来实现,适合于长时间运行的业务场景。
测试seata的建表sql 每一个资源管理器所操作的表对应的数据库下都需要 undo_log 表
create table account_db.account_tbl ( id int auto_increment primary key, user_id int not null, money decimal(10, 2) not null ) engine = InnoDB; create table order_db.order_tbl ( id int auto_increment primary key, user_id int not null, commodity_code varchar(50) not null, count int not null, money decimal(10, 2) not null ) engine = InnoDB; create table storage_db.storage_tbl ( id int auto_increment primary key, commodity_code varchar(50) not null, count int not null ) engine = InnoDB; create table account_db.undo_log ( branch_id bigint not null comment '分支事务ID', xid varchar(100) not null comment '全局事务ID', context varchar(128) not null comment '上下文', rollback_info longblob not null comment '回滚信息', log_status int not null comment '状态,0正常,1全局已完成', log_created datetime(6) not null comment '创建时间', log_modified datetime(6) not null comment '修改时间', constraint ux_undo_log unique (xid, branch_id) ) comment 'AT transaction mode undo table' engine = InnoDB charset = utf8 row_format = COMPACT; create table order_db.undo_log ( branch_id bigint not null comment '分支事务ID', xid varchar(100) not null comment '全局事务ID', context varchar(128) not null comment '上下文', rollback_info longblob not null comment '回滚信息', log_status int not null comment '状态,0正常,1全局已完成', log_created datetime(6) not null comment '创建时间', log_modified datetime(6) not null comment '修改时间', constraint ux_undo_log unique (xid, branch_id) ) comment 'AT transaction mode undo table' engine = InnoDB charset = utf8 row_format = COMPACT; create table storage_db.undo_log ( branch_id bigint not null comment '分支事务ID', xid varchar(100) not null comment '全局事务ID', context varchar(128) not null comment '上下文', rollback_info longblob not null comment '回滚信息', log_status int not null comment '状态,0正常,1全局已完成', log_created datetime(6) not null comment '创建时间', log_modified datetime(6) not null comment '修改时间', constraint ux_undo_log unique (xid, branch_id) ) comment 'AT transaction mode undo table' engine = InnoDB charset = utf8 row_format = COMPACT;
正文到此结束
- 本文标签: SpringCloud
- 本文链接: http://119.91.109.247:8443//article/155
- 版权声明: 本文由张亚东原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权