一、概述
本文,我们将纯 Spring Boot应用接入 Seata 来实现分布式事务,后续介绍Spring Cloud方式接入 Seata。本文的源代码可从Gitee获取。
二、AT 模式 + 多数据源
2.1 背景
在 Spring Boot 单体项目中,如果使用了多个数据源,我们就需要考虑多个数据源的一致性,面临分布式事务的问题。本小节,我们将使用 Seata 的 AT 模式,解决该问题。
我们以用户购买商品的业务逻辑,来作为具体示例,一共会有三个模块的 Service,分别对应不同的数据库。整体如下图所示:
2.2 初始化数据库
使用 data.sql
脚本,创建 seata_order
、seata_storage
、seata_amount
三个库。脚本内容如下:
# Order
DROP DATABASE IF EXISTS seata_order;
CREATE DATABASE seata_order;
CREATE TABLE seata_order.orders
(
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) DEFAULT NULL,
product_id INT(11) DEFAULT NULL,
pay_amount DECIMAL(10, 0) DEFAULT NULL,
add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
CREATE TABLE seata_order.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,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
# Storage
DROP DATABASE IF EXISTS seata_storage;
CREATE DATABASE seata_storage;
CREATE TABLE seata_storage.product
(
id INT(11) NOT NULL AUTO_INCREMENT,
stock INT(11) DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_storage.product (id, stock) VALUES (1, 10); # 插入一条产品的库存
CREATE TABLE seata_storage.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,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
# Amount
DROP DATABASE IF EXISTS seata_amount;
CREATE DATABASE seata_amount;
CREATE TABLE seata_amount.account
(
id INT(11) NOT NULL AUTO_INCREMENT,
balance DOUBLE DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
CREATE TABLE seata_amount.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,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
INSERT INTO seata_amount.account (id, balance) VALUES (1, 1);
其中,每个库中的 undo_log
表,是 Seata AT 模式必须创建的表,主要用于分支事务的回滚。
另外,考虑到测试方便,我们插入了一条 id = 1
的 account
记录,和一条 id = 1
的 product
记录。
2.3 引入依赖
创建 [pom.xml
]文件,引入相关的依赖。内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>guoxiuzhi-multiple-datasource</artifactId>
<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 实现对数据库连接池的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency> <!-- 本示例,我们使用 MySQL -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!-- 实现对 MyBatis 的自动化配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- 实现对 dynamic-datasource 的自动化配置 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 实现对 Seata 的自动化配置 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
2.3.1 引入 dynamic-datasource-spring-boot-starter
依赖,实现对 dynamic-datasource
的自动配置,用于多数据源的切换功能。
提示:关于数据源的切换功能,可以阅读《芋道 Spring Boot 多数据源(读写分离)入门》文章。
2.3.2 引入 seata-spring-boot-starter
依赖 (与spring cloud不同),实现对 Seata 的自动配置。
2.4 配置文件
创建 application.yaml
配置文件,添加相关的配置项。内容如下:
server:
port: 8081 # 端口
spring:
application:
name: multi-datasource-service # 应用名
datasource:
# dynamic-datasource-spring-boot-starter 动态数据源的配配项,对应 DynamicDataSourceProperties 类
dynamic:
primary: order-ds # 设置默认的数据源或者数据源组,默认值即为 master
datasource:
# 订单 order 数据源配置
order-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
# 账户 pay 数据源配置
amount-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_pay?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
# 库存 storage 数据源配置
storage-ds:
url: jdbc:mysql://127.0.0.1:3306/seata_storage?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:
seata: true # 是否启动对 Seata 的集成
# Seata 配置项,对应 SeataProperties 类
seata:
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
# 服务配置项,对应 ServiceProperties 类
service:
# 虚拟组和分组的映射
vgroup-mapping:
multi-datasource-service-group: default
# 分组和 Seata 服务的映射
grouplist:
default: 127.0.0.1:8091
① spring.datasource.dynamic
配置项,设置 dynamic-datasource-spring-boot-starter
动态数据源的配置项,对应 DynamicDataSourceProperties 类。
注意,一定要设置 spring.datasource.dynamic.seata
配置项为 true
,开启对 Seata 的集成!!!忘记配置,导致 Seata 全局事务回滚失败。
② seata
配置项,设置 Seata 的配置项目,对应 SeataProperties 类。
-
application-id
配置项,对应 Seata 应用编号,默认为${spring.application.name}
。实际上,可以不进行设置。 -
tx-service-group
配置项,Seata 事务组编号,用于 TC 集群名。
③ seata.service
配置项,Seata 服务配置项,对应 ServiceProperties 类。它主要用于 Seata 在事务分组的特殊设计,可见《Seata 文档 —— 事务分组专题》。如果不能理解,可以见如下图:
简单来说,就是多了一层虚拟映射。这里,我们直接设置 TC Server 的地址,为 127.0.0.1:8091
。
2.5 订单模块
2.5.1 OrderController
创建 OrderController 类,提供 order/create
下单 HTTP API。代码如下:
@RestController
@RequestMapping("/order")
public class OrderController {
private Logger logger = LoggerFactory.getLogger(OrderController.class);
@Autowired
private OrderService orderService;
@PostMapping("/create")
public Integer createOrder(@RequestParam("userId") Long userId,
@RequestParam("productId") Long productId,
@RequestParam("price") Integer price) throws Exception {
logger.info("[createOrder] 收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price);
return orderService.createOrder(userId, productId, price);
}
}
- 该 API 中,会调用 OrderService 进行下单。
提示:因为这个是示例项目,所以直接传入
price
金额参数,作为订单的金额,实际肯定不是这样。
2.5.2 OrderService
创建 OrderService 接口,定义了创建订单的方法。代码如下:
/**
* 订单 Service
*/
public interface OrderService {
/**
* 创建订单
*
* @param userId 用户编号
* @param productId 产品编号
* @param price 价格
* @return 订单编号
* @throws Exception 创建订单失败,抛出异常
*/
Integer createOrder(Long userId, Long productId, Integer price) throws Exception;
}
2.5.3 OrderServiceImpl
创建 [OrderServiceImpl]类,实现创建订单的方法。代码如下:
@Service
public class OrderServiceImpl implements OrderService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private OrderDao orderDao;
@Autowired
private AccountService accountService;
@Autowired
private ProductService productService;
@Override
@DS(value = "order-ds") // <1>
@GlobalTransactional // <2>
public Integer createOrder(Long userId, Long productId, Integer price) throws Exception {
Integer amount = 1; // 购买数量,暂时设置为 1。
logger.info("[createOrder] 当前 XID: {}", RootContext.getXID());
// <3> 扣减库存
productService.reduceStock(productId, amount);
// <4> 扣减余额
accountService.reduceBalance(userId, price);
// <5> 保存订单
OrderDO order = new OrderDO().setUserId(userId).setProductId(productId).setPayAmount(amount * price);
orderDao.saveOrder(order);
logger.info("[createOrder] 保存订单: {}", order.getId());
// 返回订单编号
return order.getId();
}
}
<1>
处,在类上,添加了 @DS
注解,设置使用 order-ds
订单数据源。
<2>
处,在类上,添加 Seata @GlobalTransactional
注解,声明全局事务。
<3>
和 <4>
处,在该方法中,调用 ProductService 扣除商品的库存,调用 AccountService 扣除账户的余额。虽然说,调用是 JVM 进程内的,但是 ProductService 操作的是 product-ds
商品数据源,AccountService 操作的是 account-ds
账户数据源。
<5>
处,在全部调用成功后,调用 OrderDao 保存订单。
2.5.4 OrderDao
创建 [OrderDao]接口,定义保存订单的操作。代码如下:
@Mapper
@Repository
public interface OrderDao {
/**
* 插入订单记录
*
* @param order 订单
* @return 影响记录数量
*/
@Insert("INSERT INTO orders (user_id, product_id, pay_amount) VALUES (#{userId}, #{productId}, #{payAmount})")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
int saveOrder(OrderDO order);
}
其中,[OrderDO]实体类,对应 orders
表。代码如下:
/**
* 订单实体
*/
public class OrderDO {
/** 订单编号 **/
private Integer id;
/** 用户编号 **/
private Long userId;
/** 产品编号 **/
private Long productId;
/** 支付金额 **/
private Integer payAmount;
// ... 省略 setter/getter 方法
}
2.6 商品模块
2.6.1 ProductService
创建 [ProductService]接口,定义了扣除库存的方法。代码如下:
/**
* 商品 Service
*/
public interface ProductService {
/**
* 扣减库存
*
* @param productId 商品 ID
* @param amount 扣减数量
* @throws Exception 扣减失败时抛出异常
*/
void reduceStock(Long productId, Integer amount) throws Exception;
}
2.6.2 ProductServiceImpl
创建 [ProductServiceImpl]类,实现扣减库存的方法。代码如下:
@Service
public class ProductServiceImpl implements ProductService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private ProductDao productDao;
@Override
@DS(value = "product-ds") // <1>
@Transactional(propagation = Propagation.REQUIRES_NEW) // <2> 开启新事物
public void reduceStock(Long productId, Integer amount) throws Exception {
logger.info("[reduceStock] 当前 XID: {}", RootContext.getXID());
// <3> 检查库存
checkStock(productId, amount);
logger.info("[reduceStock] 开始扣减 {} 库存", productId);
// <4> 扣减库存
int updateCount = productDao.reduceStock(productId, amount);
// 扣除成功
if (updateCount == 0) {
logger.warn("[reduceStock] 扣除 {} 库存失败", productId);
throw new Exception("库存不足");
}
// 扣除失败
logger.info("[reduceStock] 扣除 {} 库存成功", productId);
}
private void checkStock(Long productId, Integer requiredAmount) throws Exception {
logger.info("[checkStock] 检查 {} 库存", productId);
Integer stock = productDao.getStock(productId);
if (stock < requiredAmount) {
logger.warn("[checkStock] {} 库存不足,当前库存: {}", productId, stock);
throw new Exception("库存不足");
}
}
}
<1>
处,在类上,添加了 @DS
注解,设置使用 product-ds
商品数据源。
<2>
处,在类上,添加了 Spring @Transactional
注解,声明本地事务。也就是说,此处会开启一个 seata_product
库的数据库事务。
<3>
处,检查库存是否足够,如果不够则抛出 Exception 异常。因为我们需要通过异常,回滚全局异常。
<4>
处,进行扣除库存,如果扣除失败则抛出 Exception 异常。
2.6.3 ProductDao
创建 [ProductDao]接口,定义获取和扣除库存的操作。代码如下:
@Mapper
@Repository
public interface ProductDao {
/**
* 获取库存
*
* @param productId 商品编号
* @return 库存
*/
@Select("SELECT stock FROM product WHERE id = #{productId}")
Integer getStock(@Param("productId") Long productId);
/**
* 扣减库存
*
* @param productId 商品编号
* @param amount 扣减数量
* @return 影响记录行数
*/
@Update("UPDATE product SET stock = stock - #{amount} WHERE id = #{productId} AND stock >= #{amount}")
int reduceStock(@Param("productId") Long productId, @Param("amount") Integer amount);
}
2.7 账户模块
友情提示:逻辑和[「2.5 商品模块」]基本一致,也是扣减逻辑。
2.7.1 AccountService
创建 [AccountService]类,定义扣除余额的方法。代码如下:
/**
* 账户 Service
*/
public interface AccountService {
/**
* 扣除余额
*
* @param userId 用户编号
* @param price 扣减金额
* @throws Exception 失败时抛出异常
*/
void reduceBalance(Long userId, Integer price) throws Exception;
}
2.7.2 AccountServiceImpl
创建 [AccountServiceImpl]类,实现扣除余额的方法。代码如下:
@Service
public class AccountServiceImpl implements AccountService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private AccountDao accountDao;
@Override
@DS(value = "account-ds") // <1>
@Transactional(propagation = Propagation.REQUIRES_NEW) // <2> 开启新事物
public void reduceBalance(Long userId, Integer price) throws Exception {
logger.info("[reduceBalance] 当前 XID: {}", RootContext.getXID());
// <3> 检查余额
checkBalance(userId, price);
logger.info("[reduceBalance] 开始扣减用户 {} 余额", userId);
// <4> 扣除余额
int updateCount = accountDao.reduceBalance(price);
// 扣除成功
if (updateCount == 0) {
logger.warn("[reduceBalance] 扣除用户 {} 余额失败", userId);
throw new Exception("余额不足");
}
logger.info("[reduceBalance] 扣除用户 {} 余额成功", userId);
}
private void checkBalance(Long userId, Integer price) throws Exception {
logger.info("[checkBalance] 检查用户 {} 余额", userId);
Integer balance = accountDao.getBalance(userId);
if (balance < price) {
logger.warn("[checkBalance] 用户 {} 余额不足,当前余额:{}", userId, balance);
throw new Exception("余额不足");
}
}
}
<1>
处,在类上,添加了 @DS
注解,设置使用 account-ds
账户数据源。
<2>
处,在类上,添加了 Spring @Transactional
注解,声明本地事务。也就是说,此处会开启一个 seata_account
库的数据库事务。
<3>
处,检查余额是否足够,如果不够则抛出 Exception 异常。因为我们需要通过异常,回滚全局异常。
<4>
处,进行扣除余额,如果扣除失败则抛出 Exception 异常。
2.7.3 AccountDao
创建 [AccountDao]接口,定义查询和扣除余额的操作。代码如下:
@Mapper
@Repository
public interface AccountDao {
/**
* 获取账户余额
*
* @param userId 用户 ID
* @return 账户余额
*/
@Select("SELECT balance FROM account WHERE id = #{userId}")
Integer getBalance(@Param("userId") Long userId);
/**
* 扣减余额
*
* @param price 需要扣减的数目
* @return 影响记录行数
*/
@Update("UPDATE account SET balance = balance - #{price} WHERE id = 1 AND balance >= ${price}")
int reduceBalance(@Param("price") Integer price);
}
2.8 MultipleDatasourceApplication
创建 [MultipleDatasourceApplication] 类,用于启动项目。代码如下:
@SpringBootApplication
public class MultipleDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleDatasourceApplication.class, args);
}
}
2.9 测试
下面,我们将测试两种情况:
- 分布式事务正常提交
- 分布式事务异常回滚
启动Nacos,启动Seata,Debug 执行MultipleDatasourceApplication
启动 Spring Boot 应用。此时,我们可以看到 Seata 相关日志如下:
# `dynamic-datasource` 初始化动态数据源
2020-06-19 08:53:59.988 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource detect ALIBABA SEATA and enabled it
2020-06-19 08:54:00.023 INFO 996 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1,order-ds} inited
2020-06-19 08:54:00.024 INFO 996 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-2,account-ds} inited
2020-06-19 08:54:00.025 INFO 996 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-3,product-ds} inited
# 加载 Druid 提供的 SQL 解析器
2020-06-19 08:54:01.008 INFO 996 --- [ main] i.s.common.loader.EnhancedServiceLoader : load DbTypeParser[druid] extension by class[io.seata.sqlparser.druid.DruidDelegatingDbTypeParser]
# 连接到 Seata TC Server 服务器
2020-06-19 08:54:01.016 INFO 996 --- [ main] i.s.c.r.netty.NettyClientChannelManager : will connect to 127.0.0.1:8091
# 注册 Seata Resource Manager 到 Seata TC Server 成功
2020-06-19 08:54:01.016 INFO 996 --- [ main] io.seata.core.rpc.netty.RmRpcClient : RM will register :jdbc:mysql://101.133.227.13:3306/seata_product
2020-06-19 08:54:01.018 INFO 996 --- [ main] i.s.core.rpc.netty.NettyPoolableFactory : NettyPool create channel to transactionRole:RMROLE,address:127.0.0.1:8091,msg:< RegisterRMRequest{resourceIds='jdbc:mysql://101.133.227.13:3306/seata_product', applicationId='multi-datasource-service', transactionServiceGroup='multi-datasource-service-group'} >
# 加载 Seata 序列化器
2020-06-19 08:54:01.448 INFO 996 --- [lector_RMROLE_1] i.s.common.loader.EnhancedServiceLoader : load Serializer[SEATA] extension by class[io.seata.serializer.seata.SeataSerializer]
2020-06-19 08:54:01.509 INFO 996 --- [ main] io.seata.core.rpc.netty.RmRpcClient : register RM success. server version:1.2.0,channel:[id: 0x57f1f121, L:/127.0.0.1:54800 - R:/127.0.0.1:8091]
2020-06-19 08:54:01.515 INFO 996 --- [ main] i.s.core.rpc.netty.NettyPoolableFactory : register success, cost 91 ms, version:1.2.0,role:RMROLE,channel:[id: 0x57f1f121, L:/127.0.0.1:54800 - R:/127.0.0.1:8091]
2020-06-19 08:54:01.515 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource [product-ds] wrap seata plugin
2020-06-19 08:54:01.515 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - load a datasource named [product-ds] success
2020-06-19 08:54:02.076 INFO 996 --- [ main] io.seata.core.rpc.netty.RmRpcClient : will register resourceId:jdbc:mysql://101.133.227.13:3306/seata_account
2020-06-19 08:54:02.077 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource [account-ds] wrap seata plugin
2020-06-19 08:54:02.077 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - load a datasource named [account-ds] success
2020-06-19 08:54:02.671 INFO 996 --- [ main] io.seata.core.rpc.netty.RmRpcClient : will register resourceId:jdbc:mysql://101.133.227.13:3306/seata_order
2020-06-19 08:54:02.671 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource [order-ds] wrap seata plugin
2020-06-19 08:54:02.671 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource - load a datasource named [order-ds] success
2020-06-19 08:54:02.671 INFO 996 --- [ main] c.b.d.d.DynamicRoutingDataSource : dynamic-datasource initial loaded [3] datasource,primary datasource named [order-ds]
# 给数据源增加 Seata 的数据源代理
2020-06-19 08:54:02.681 INFO 996 --- [ main] s.s.a.d.SeataDataSourceBeanPostProcessor : Auto proxy of [dataSource]
2020-06-19 08:54:02.685 INFO 996 --- [ main] io.seata.core.rpc.netty.RmRpcClient : will register resourceId:jdbc:mysql://101.133.227.13:3306/seata_order
# 因为 OrderServiceImpl 添加了 `@GlobalTransactional` 注解,所以创建其代理,用于全局事务。
2020-06-19 08:54:02.788 INFO 996 --- [ main] i.s.s.a.GlobalTransactionScanner : Bean[cn.iocoder.springboot.lab52.seatademo.service.impl.OrderServiceImpl$$EnhancerBySpringCGLIB$$a6c7b7c2] with name [orderServiceImpl] would use interceptor [io.seata.spring.annotation.GlobalTransactionalInterceptor]
2020-06-19 08:54:02.874 INFO 996 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-19 08:54:03.043 INFO 996 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2020-06-19 08:54:03.045 INFO 996 --- [ main] c.i.s.l.s.MultipleDatasourceApplication : Started MultipleDatasourceApplication in 4.409 seconds (JVM running for 5.095)
2.9.1 正常流程
使用 Postman 模拟调用 http://127.0.0.1:8081/order/create 创建订单的接口,如下图所示,返回的2为订单号。
在控制台打印日志如下图所示:
2020-06-19 08:54:23.039 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.controller.OrderController : [createOrder] 收到下单请求,用户:1, 商品:1, 价格:2
2020-06-19 08:54:23.045 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load TransactionManager[null] extension by class[io.seata.tm.DefaultTransactionManager]
2020-06-19 08:54:23.046 INFO 996 --- [nio-8081-exec-1] io.seata.tm.TransactionManagerHolder : TransactionManager Singleton io.seata.tm.DefaultTransactionManager@172790e3
2020-06-19 08:54:23.052 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load LoadBalance[null] extension by class[io.seata.discovery.loadbalance.RandomLoadBalance]
2020-06-19 08:54:23.052 INFO 996 --- [nio-8081-exec-1] i.s.c.r.netty.NettyClientChannelManager : will connect to 127.0.0.1:8091
2020-06-19 08:54:23.052 INFO 996 --- [nio-8081-exec-1] i.s.core.rpc.netty.NettyPoolableFactory : NettyPool create channel to transactionRole:TMROLE,address:127.0.0.1:8091,msg:< RegisterTMRequest{applicationId='multi-datasource-service', transactionServiceGroup='multi-datasource-service-group'} >
2020-06-19 08:54:23.058 INFO 996 --- [nio-8081-exec-1] i.s.core.rpc.netty.NettyPoolableFactory : register success, cost 4 ms, version:1.2.0,role:TMROLE,channel:[id: 0x0f08c4ac, L:/127.0.0.1:54827 - R:/127.0.0.1:8091]
2020-06-19 08:54:23.220 INFO 996 --- [nio-8081-exec-1] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [192.168.1.104:8091:2014720014]
2020-06-19 08:54:23.228 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.service.impl.OrderServiceImpl : [createOrder] 当前 XID: 192.168.1.104:8091:2014720014
2020-06-19 08:54:23.333 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 当前 XID: 192.168.1.104:8091:2014720014
2020-06-19 08:54:23.333 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [checkStock] 检查 1 库存
2020-06-19 08:54:23.359 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load SQLRecognizerFactory[druid] extension by class[io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory]
2020-06-19 08:54:23.389 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load SQLOperateRecognizerHolder[mysql] extension by class[io.seata.sqlparser.druid.mysql.MySQLOperateRecognizerHolder]
2020-06-19 08:54:23.579 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 开始扣减 1 库存
2020-06-19 08:54:23.698 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load KeywordChecker[mysql] extension by class[io.seata.rm.datasource.undo.mysql.keyword.MySQLKeywordChecker]
2020-06-19 08:54:23.699 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load TableMetaCache[mysql] extension by class[io.seata.rm.datasource.sql.struct.cache.MysqlTableMetaCache]
2020-06-19 08:54:24.788 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 扣除 1 库存成功
2020-06-19 08:54:28.734 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load UndoLogManager[mysql] extension by class[io.seata.rm.datasource.undo.mysql.MySQLUndoLogManager]
2020-06-19 08:54:28.750 WARN 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load [io.seata.rm.datasource.undo.parser.ProtostuffUndoLogParser] class fail. io/protostuff/runtime/RuntimeEnv
2020-06-19 08:54:28.750 INFO 996 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load UndoLogParser[jackson] extension by class[io.seata.rm.datasource.undo.parser.JacksonUndoLogParser]
2020-06-19 08:54:30.605 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.AccountServiceImpl : [reduceBalance] 当前 XID: 192.168.1.104:8091:2014720014
2020-06-19 08:54:30.605 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.AccountServiceImpl : [checkBalance] 检查用户 1 余额
2020-06-19 08:54:30.677 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.AccountServiceImpl : [reduceBalance] 开始扣减用户 1 余额
2020-06-19 08:54:31.653 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.AccountServiceImpl : [reduceBalance] 扣除用户 1 余额成功
2020-06-19 08:54:36.785 INFO 996 --- [nio-8081-exec-1] c.i.s.l.s.service.impl.OrderServiceImpl : [createOrder] 保存订单: 2
2020-06-19 08:54:37.252 INFO 996 --- [nio-8081-exec-1] i.seata.tm.api.DefaultGlobalTransaction : [192.168.1.104:8091:2014720014] commit status: Committed
2020-06-19 08:54:38.224 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720018,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_order,applicationData=null
2020-06-19 08:54:38.226 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720018 jdbc:mysql://101.133.227.13:3306/seata_order null
2020-06-19 08:54:38.227 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2020-06-19 08:54:38.351 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720025,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_product,applicationData=null
2020-06-19 08:54:38.352 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720025 jdbc:mysql://101.133.227.13:3306/seata_product null
2020-06-19 08:54:38.352 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2020-06-19 08:54:38.467 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720029,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_order,applicationData=null
2020-06-19 08:54:38.467 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720029 jdbc:mysql://101.133.227.13:3306/seata_order null
2020-06-19 08:54:38.467 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2020-06-19 08:54:38.578 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720032,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_account,applicationData=null
2020-06-19 08:54:38.578 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720032 jdbc:mysql://101.133.227.13:3306/seata_account null
2020-06-19 08:54:38.578 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2020-06-19 08:54:38.690 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720035,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_order,applicationData=null
2020-06-19 08:54:38.690 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720035 jdbc:mysql://101.133.227.13:3306/seata_order null
2020-06-19 08:54:38.690 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2020-06-19 08:54:38.800 INFO 996 --- [tch_RMROLE_1_16] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.1.104:8091:2014720014,branchId=2014720038,branchType=AT,resourceId=jdbc:mysql://101.133.227.13:3306/seata_order,applicationData=null
2020-06-19 08:54:38.800 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.1.104:8091:2014720014 2014720038 jdbc:mysql://101.133.227.13:3306/seata_order null
2020-06-19 08:54:38.800 INFO 996 --- [tch_RMROLE_1_16] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
说明:
DefaultGlobalTransaction : [192.168.1.104:8091:2014720014] commit status: Committed为开始全局提交。下图部分为分支提交:
查询下目前数据库的数据情况。如下图所示:
2.9.2 异常流程
在 OrderServiceImpl 的 createOrder(...)
方法打上断点如下图,方便我们看到 product 表的 stock 被减少:
现在的
product
表的 stock
是8 个。使用 Postman 模拟调用 http://127.0.0.1:8081/order/create 创建订单的接口,如下图所示:
扣减了库存,并回滚
2020-06-19 09:45:43.334 INFO 11664 --- [nio-8081-exec-1] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [192.168.1.104:8091:2014720090]
2020-06-19 09:45:43.338 INFO 11664 --- [nio-8081-exec-1] c.i.s.l.s.service.impl.OrderServiceImpl : [createOrder] 当前 XID: 192.168.1.104:8091:2014720090
2020-06-19 09:45:43.406 INFO 11664 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 当前 XID: 192.168.1.104:8091:2014720090
2020-06-19 09:45:43.406 INFO 11664 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [checkStock] 检查 1 库存
2020-06-19 09:45:43.420 INFO 11664 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load SQLRecognizerFactory[druid] extension by class[io.seata.sqlparser.druid.DruidDelegatingSQLRecognizerFactory]
2020-06-19 09:45:43.450 INFO 11664 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load SQLOperateRecognizerHolder[mysql] extension by class[io.seata.sqlparser.druid.mysql.MySQLOperateRecognizerHolder]
2020-06-19 09:45:43.642 INFO 11664 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 开始扣减 1 库存
2020-06-19 09:45:43.689 INFO 11664 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load KeywordChecker[mysql] extension by class[io.seata.rm.datasource.undo.mysql.keyword.MySQLKeywordChecker]
2020-06-19 09:45:43.690 INFO 11664 --- [nio-8081-exec-1] i.s.common.loader.EnhancedServiceLoader : load TableMetaCache[mysql] extension by class[io.seata.rm.datasource.sql.struct.cache.MysqlTableMetaCache]
2020-06-19 09:45:44.661 INFO 11664 --- [nio-8081-exec-1] c.i.s.l.s.s.impl.ProductServiceImpl : [reduceStock] 扣除 1 库存成功
2020-06-19 09:45:46.182 INFO 11664 --- [nio-8081-exec-1] i.seata.tm.api.DefaultGlobalTransaction : [192.168.1.104:8091:2014720090] rollback status: Rollbacked
库存没有减少,但是更新时间变化了