分布式事务Seata实战-AT模式(注册中心为Eureka)

大致记录Seata的AT模式下创建项目过程中需要注意的点和可能遇到的问题。

本项目是以官网的给的示例(即下图)进行创建的,以Eureka为注册中心。

官网:Seata AT 模式 | Apache Seata™

官方代码示例:

快速启动 | Apache Seata™

此文章涉及的项目代码链接:seata-at: 分布式事务解决方案Seata 的AT模式

前期准备

1、下载seata-server (下载地址:https://seata.io/zh-cn/unversioned/download/seata-server ),本项目用的当前最新版seata-server-2.0.0.zip

2、JDK版本需要JDK8或者更高,本项目使用的JDK11

3、此项目SpringBoot的创建采用的阿里的初始化连接(https://start.aliyun.com/ ),修改方式可见附录

注:本项目采用eureka为注册中心;

项目搭建

数据库准备

根据官方示例建立三个数据库:storage、order、account;

账户库:seata_account

sql 复制代码
DROP TABLE IF EXISTS `account_tbl`;
CREATE TABLE `account_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`money` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- SEATA AT 模式需要 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`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

订单库:seata_order

sql 复制代码
DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
`money` int(11) DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- SEATA AT 模式需要 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`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

仓库库:seata_storage

sql 复制代码
DROP TABLE IF EXISTS `storage_tbl`;
CREATE TABLE `storage_tbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) DEFAULT NULL,
`count` int(11) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- SEATA AT 模式需要 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`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

项目创建

创建项目 seata-at,依次创建子模块 account、order、 storage、business、eureka、common,项目结构如图:

需要修改seata-server的eureka注册配置(seata-server-2.0.0\conf\application.yml),如下:

复制代码
seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: file
  registry:
    type: eureka
    eureka:
      service-url: http://localhost:8761/eureka
      application: default
      weight: 1

项目中的 account、order、storage 模块中的resource/file.conf 文件中:修改 service 的内容由:

复制代码
vgroup_mapping.business-service-seata-service-group = "default"

改为

复制代码
vgroup_mapping.my_test_tx_group = "default"

项目中的 account、order、storage 模块中的resource/registry.conf文件 :修改registry的内容

复制代码
type = "eureka"

application.properties 文件注意 seata.tx-service-group 无效,改用spring.cloud.alibaba.seata.tx-service-group,如下:

复制代码
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group
#使用无效 #seata.tx-service-group=my_test_tx_group
seata.service.disable-global-transaction=false

启动和测试

启动顺序:eureka 、seata-server(外部服务)、 account、storage、 order、 business

测试采用的Postman。

附录

Spring项目初始化链接修改

点击Server URL 的设置按钮,修改链接为:https://start.aliyun.com/ ,点击确认即可。

相关推荐
这个昵称不容易2 天前
交流论坛网站设计与实现:SpringBoot 论坛 130 个接口,附论文与运行截图
java·springboot·毕设
喵个咪3 天前
RushWind Shop — 一笔订单的穿越:电商买路径上的每一道闸门
后端·微服务·rust
月光船幽幽3 天前
基线漂移破解:微服务测试一致性方案
python·科技·微服务
这个昵称不容易3 天前
靓车汽车销售网站设计与实现:SpringBoot 汽车销售 203 个接口,附论文与运行截图
java·springboot·毕设
威嵌神州3 天前
NXP P2020核心板技术解析:PowerPC架构与VxWorks存量项目迁移指南
嵌入式硬件·微服务·云原生·架构
底层玩家老张4 天前
分布式事务复盘:协调者超时,237笔单边账与XA悬挂事务
innodb·分布式事务·两阶段提交·最终一致性·架构选型·数据库内核
8486981195 天前
AI 简历生成器怎么做?双模式降级设计(AI+模板)
springboot·langchain4j·ai 应用·降级设计·简历生成
这个昵称不容易5 天前
在线考试系统设计与实现:14 张表 158 个接口,组卷、答题、判分一条线走通
java·springboot·毕设
千寻技术帮7 天前
50055_基于AI的互帮互助小程序(源码+文档+数据库脚本+部署教程)
数据库·人工智能·小程序·毕业设计·springboot·课程设计
YOU OU7 天前
项目实战知识点—在线oj项目
微服务