springboot+nacos+seata1.4实现分布式事务

本文档详细介绍了如何使用SpringBoot、Nacos和Seata1.4实现分布式事务。首先,介绍了Seata的下载、配置及数据库准备;接着,展示了服务one调用服务two的过程,包括服务创建、依赖配置和Nacos配置;然后,通过成功和失败的事务操作,验证了分布式事务的回滚功能;最后,展示了成功完成分布式事务后的结果。
摘要由CSDN通过智能技术生成
版本:
 Springboot:2.2.0
 SpringCloud: 2020.0.3
 Seata: 1.4
复制代码

本文章demo地址:gitee.com/TZWw/spring…

1. 下载seata

1)地址:seata.io/zh-cn/blog/…

springboot+nacos+seata1.4实现分布式事务

2)在conf文件夹修改file.conf文件

springboot+nacos+seata1.4实现分布式事务

3)向本地数据库导入seata需要的表

  • 创建名字为seata的数据库
  • 新建表branch_table、global_table、lock_table
CREATE TABLE `branch_table` (
  `branch_id` bigint NOT NULL,
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint DEFAULT NULL,
  `resource_group_id` varchar(32) DEFAULT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `lock_key` varchar(128) DEFAULT NULL,
  `branch_type` varchar(8) DEFAULT NULL,
  `status` tinyint DEFAULT NULL,
  `client_id` varchar(64) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
复制代码
CREATE TABLE `global_table` (
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint DEFAULT NULL,
  `status` tinyint NOT NULL,
  `application_id` varchar(32) DEFAULT NULL,
  `transaction_service_group` varchar(32) DEFAULT NULL,
  `transaction_name` varchar(128) DEFAULT NULL,
  `timeout` int DEFAULT NULL,
  `begin_time` bigint DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`xid`),
  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
复制代码
CREATE TABLE `lock_table` (
  `row_key` varchar(128) NOT NULL,
  `xid` varchar(96) DEFAULT NULL,
  `transaction_id` mediumtext,
  `branch_id` mediumtext,
  `resource_id` varchar(256) DEFAULT NULL,
  `table_name` varchar(32) DEFAULT NULL,
  `pk` varchar(36) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`row_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
复制代码

springboot+nacos+seata1.4实现分布式事务

4)修改registry.conf文件

springboot+nacos+seata1.4实现分布式事务

springboot+nacos+seata1.4实现分布式事务

5)将seata配置信息添加到nacos配置中心

1.启动本地的nacos服务 2.下载config.txt和nacos-config.sh两个文件 github.com/seata/seata… github.com/seata/seata… 3.然后执行nacos-config.sh脚本

springboot+nacos+seata1.4实现分布式事务

4.查看nacos中添加成功的配置

springboot+nacos+seata1.4实现分布式事务

6)启动seata

进入bin目录下,执行 ./seata-server.sh 出现下图即为启动成功

springboot+nacos+seata1.4实现分布式事务

2. 服务间调用(服务one调用服务two)

1)创建one和two数据库,每个数据库都必须包含undo_log表

  • one数据库
CREATE TABLE `count` (
  `id` int NOT NULL AUTO_INCREMENT,
  `count` int DEFAULT NULL COMMENT '库存',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `undo_log` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `branch_id` bigint NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb3;
复制代码
  • two数据库
CREATE TABLE `order` (
  `id` int NOT NULL AUTO_INCREMENT,
  `order_count` int DEFAULT NULL,
  `product_id` bigint DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `undo_l
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值