文章目录
1.准备订单项目案例
1.1 准备工程目录
新建 seata-tcc 工程
工程命名为 seata-tcc,存放到 seata-samples 文件夹下,与 seata-at 工程存放在一起:
1.2 导入订单项目-无事务版本
将我们此前准备的无事务版本压缩包解压到 seata-tcc 目录
压缩文件中的 7 个项目目录解压缩到 seata-tcc 目录:
导入项目
在 idea 中按两下 shift 键,搜索 add maven projects,打开 maven 工具:
然后选择 seata-tcc 工程目录下的 7 个项目的 pom.xml 导入:
2.order启动全局事务,添加“保存订单”分支事务
在订单项目中执行添加订单:
我们要添加以下 TCC 事务操作的代码:
- Try - 第一阶,冻结数据阶段,向订单表直接插入订单,订单状态设置为0(冻结状态)。
- Confirm - 第二阶段,提交事务,将订单状态修改成1(正常状态)。
- Cancel - 第二阶段,回滚事务,删除订单。
2.1 order-parent 添加 seata 依赖
打开 order-parent 中注释掉的 seata 依赖:
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.study</groupId>
<artifactId>order-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>order-parent</name>
<properties>
<mybatis-plus.version>3.3.2</mybatis-plus.version>
<druid-spring-boot-starter.version>1.1.23</druid-spring-boot-starter.version>
<seata.version>1.3.0</seata.version>
<spring-cloud-alibaba-seata.version>2.0.0.RELEASE</spring-cloud-alibaba-seata.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
<!-- 打开 seata 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>${spring-cloud-alibaba-seata.version}</version>
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 配置
2.2.1 application.yml
设置全局事务组的组名:
spring:
application:
name: order
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/seata_order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
# 事务组设置
cloud:
alibaba:
seata:
tx-service-group: order_tx_group
......
2.2.2 registry.conf 和 file.conf
与 AT 事务中的配置完全相同:
registry.conf:
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
serverAddr = "localhost"
namespace = ""
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
# application = "default"
# weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = "0"
password = ""
cluster = "default"
timeout = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3、springCloudConfig
type = "file"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
app.id = "seata-server"
apollo.meta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
file.conf:
transport {
# tcp udt unix-domain-socket
type = "TCP"
#NIO NATIVE
server = "NIO"
#enable heartbeat
heartbeat = true
# the client batch send request enable
enableClientBatchSendRequest = true
#thread factory for netty
threadFactory {
bossThreadPrefix = "NettyBoss"
workerThreadPrefix = "NettyServerNIOWorker"
serverExecutorThread-prefix = "NettyServerBizHandler"
shareBossWorker = false
clientSelectorThreadPrefix = "NettyClientSelector"
clientSelectorThreadSize = 1
clientWorkerThreadPrefix = "NettyClientWorkerThread"
# netty boss thread size,will not be used for UDT
bossThreadSize = 1
#auto default pin or 8
workerThreadSize = "default"
}
shutdown {
# when destroy server, wait seconds
wait = 3
}
serialization = "seata"
compressor = "none"
}
service {
#transaction service group mapping
# order_tx_group 与 yml 中的 “tx-service-group: order_tx_group” 配置一致
# “seata-server” 与 TC 服务器的注册名一致
# 从eureka获取seata-server的地址,再向seata-server注册自己,设置group
vgroupMapping.order_tx_group = "seata-server"
#only support when registry.type=file, please don't set multiple addresses
order_tx_group.grouplist = "127.0.0.1:8091"
#degrade, current not support
enableDegrade = false
#disable seata
disableGlobalTransaction = false
}
client {
rm {
asyncCommitBufferLimit = 10000
lock {
retryInterval = 10
retryTimes = 30
retryPolicyBranchRollbackOnConflict = true
}
reportRetryCount = 5
tableMetaCheckEnable = false
reportSuccessEnable = false
}
tm {
commitRetryCount = 5
rollbackRetryCount = 5
}
undo {
dataValidation = true
logSerialization = "jackson"
logTable = "undo_log"
}
log {
exceptionRate = 100
}
}
2.3 OrderMapper 添加更新订单状态、删除订单
根据前面的分析,订单数据操作有以下三项:
- 插入订单
- 修改订单状态
- 删除订单
在 OrderMapper 中已经有插入订单的方法了,现在需要添加修改订单和删除订单的方法(删除方法从BaseMapper继承):
package cn.study.order.mapper;
import cn.study.order.entity.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
public interface OrderMapper extends BaseMapper {
void create(Order order);
void updateStatus(@Param("orderId") Long orderId, @Param("status") Integer status);
}
那么对应的 OrderMapper.xml 中也要添加 sql:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.study.order.mapper.OrderMapper" >
<resultMap id="BaseResultMap" type="cn.study.order.entity.Order" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="user_id" property="userId" jdbcType="BIGINT" />
<result column="product_id" property="productId" jdbcType="BIGINT" />
<result column="count" property="count" jdbcType="INTEGER" />
<result column="money" property="money" jdbcType="DECIMAL" />
<result column="status" property="status" jdbcType="INTEGER" />
</resultMap>
<insert id="create">
INSERT INTO `order` (`id`,`user_id`,`product_id`,`count`,`money`,`status`)
VALUES(#{id}, #{userId}, #{productId}, #{count}, #{money}, ${status});
</insert>
<update id="updateStatus" >
UPDATE `order` SET `status`=#{status} WHERE `id`=#{orderId};
</update>
<delete id="deleteById">
DELETE FROM `order` WHERE `id`=#{orderId}
</delete>
</mapper>
2.4 Seata 实现订单的 TCC 操作方法
- 第一阶段 Try
- 第二阶段
Confirm
Cancel
2.4.1 添加工具类-ResultHolder
第二阶段为了处理幂等性问题这里首先添加一个工具类 ResultHolder
。
这个工具也可以在第二阶段 Confirm
或 Cancel
阶段对第一阶段的成功与否进行判断,在第一阶段成功时需要保存一个标识。
ResultHolder
可以为每一个全局事务保存一个标识:
package cn.study.order.tcc;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ResultHolder {
private static Map<Class<?>, Map<String, String>> map = new ConcurrentHashMap<Class<?>, Map<String, String>>();
public static void setResult(Class<?> actionClass, String xid, String v) {
Map<String, String> results = map.get(actionClass);
if (results == null) {
synchronized (map) {
if (results == null) {
results = new ConcurrentHashMap<>();
map.put(actionClass, results);
}
}
}
results.put(xid, v);
}
public static String getResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
return results.get(xid);
}
return null;
}
public static void removeResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
results.remove(xid);
}
}
}
2.4.2 定义接口
Seata 实现 TCC 操作需要定义一个接口,我们在接口中添加以下方法:
Try - prepareCreateOrder()
Confirm - commit()
Cancel - rollback()
package cn.study.order.tcc;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import java.math.BigDecimal;
@LocalTCC
public interface OrderTccAction {
/**
*第一阶段的方法
* 通过注解表明这是第一阶段的方法
* 其通过name值来区分多个AccAction
* 通过commitMethod来指定第二阶段提交的方法
* 通过rollbackMethod来指定第二阶段回滚的方法
*
*BusinessActionContext 指上下文对象,用来在两个阶段之间传递数据
*@BusinessActionContextParameter 注解的参数数据会被存入 BusinessActionContext
*/
@TwoPhaseBusinessAction(name = "orderTccAction", commitMethod = "commit", rollbackMethod = "rollback")
boolean prepareCreateOrder(BusinessActionContext businessActionContext,
@BusinessActionContextParameter(paramName = "orderId") Long orderId,
@BusinessActionContextParameter(paramName = "userId") Long userId,
@BusinessActionContextParameter(paramName = "productId") Long productId,
@BusinessActionContextParameter(paramName = "count") Integer count,
@BusinessActionContextParameter(paramName = "money") BigDecimal money);
// 第二阶段 - 提交
boolean commit(BusinessActionContext businessActionContext);
// 第二阶段 - 回滚
boolean rollback(BusinessActionContext businessActionContext);
}
实现类:
package cn.study.order.tcc;
import cn.study.order.entity.Order;
import cn.study.order.mapper.OrderMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
@Component
@Slf4j
public class OrderTccActionImpl implements OrderTccAction {
@Autowired
private OrderMapper orderMapper;
@Transactional
@Override
public boolean prepareCreateOrder(BusinessActionContext businessActionContext, Long orderId, Long userId, Long productId, Integer count, BigDecimal money) {
log.info("创建 order 第一阶段,预留资源 - "+businessActionContext.getXid());
Order order = new Order(orderId, userId, productId, count, money, 0);
orderMapper.create(order);
//事务成功,保存一个标识,供第二阶段进行判断
ResultHolder.setResult(getClass(), businessActionContext.getXid(), "p");
return true;
}
@Transactional
@Override
public boolean commit(BusinessActionContext businessActionContext) {
log.info("创建 order 第二阶段提交,修改订单状态1 - "+businessActionContext.getXid());
//从上下文对象获取orderId
//Long orderId = (Long) businessActionContext.getActionContext("orderId");
long orderId = Long.parseLong(businessActionContext.getActionContext("orderId").toString());
orderMapper.updateStatus(orderId, 1);
//提交成功则删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
@Transactional
@Override
public boolean rollback(BusinessActionContext businessActionContext) {
log.info("创建 order 第二阶段回滚,删除订单 - "+businessActionContext.getXid());
//Long orderId = (Long) businessActionContext.getActionContext("orderId");
long orderId = Long.parseLong(businessActionContext.getActionContext("orderId").toString());
orderMapper.deleteById(orderId);
//回滚结束时,删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
}
2.4.3 在业务代码中调用 Try 阶段方法
业务代码中不再直接保存订单数据,而是调用 TCC 第一阶段方法prepareCreateOrder()
,并添加全局事务注解 @GlobalTransactional
:
package cn.study.order.service;
import cn.study.order.entity.Order;
import cn.study.order.feign.AccountClient;
import cn.study.order.feign.EasyIdGeneratorClient;
import cn.study.order.feign.StorageClient;
import cn.study.order.mapper.OrderMapper;
import cn.study.order.tcc.OrderTccAction;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class OrderServiceImpl implements OrderService {
// @Autowired
// private OrderMapper orderMapper;
@Autowired
private EasyIdGeneratorClient easyIdGeneratorClient;
@Autowired
private AccountClient accountClient;
@Autowired
private StorageClient storageClient;
@Autowired
private OrderTccAction orderTccAction;
@GlobalTransactional//在第一个模块上,启动全局事务
@Override
public void create(Order order) {
// 从全局唯一id发号器获得id
Long orderId = easyIdGeneratorClient.nextId("order_business");
order.setId(orderId);
//创建正常订单方法
// orderMapper.create(order);
/**
*这里修改成调用 TCC 第一阶段方法创建冻结订单
* tcc对象是AOP创建的动态代理对象
* 通过AOP的“通知”添加了前置代码,会创建上下文对象传入原始的方法中
* 所以方法中的参数即使给一个null值,也会被创建上下文对象传入正确的值
*/
orderTccAction.prepareCreateOrder(
null,
order.getId(),
order.getUserId(),
order.getProductId(),
order.getCount(),
order.getMoney());
// 修改库存
//storageClient.decrease(order.getProductId(), order.getCount());
// 修改账户余额
//accountClient.decrease(order.getUserId(), order.getMoney());
}
}
2.5 启动 order 进行测试
按顺序启动服务:
- Eureka
- Seata Server
- Easy Id Generator
- Order
调用保存订单,地址:http://localhost:8083/create?userId=1&productId=1&count=10&money=100
观察控制台日志:
查看数据库表中的订单数据:
3.storage添加“减少库存”分支事务
在库存项目中执行减少库存:
我们要添加以下 TCC 事务操作的代码:
- Try - 第一阶,冻结数据阶段,将要减少的库存量先冻结:
- Confirm - 第二阶段,提交事务,使用冻结的库存完成业务数据处理:
- Cancel - 第二阶段,回滚事务,冻结的库存解冻,恢复以前的库存量:
3.1 配置
有三个文件需要配置:
application.yml
registry.conf
file.conf
这三个文件的设置与上面 order 项目的配置完全相同,请参考上面订单配置一章进行配置。
3.2 StorageMapper 添加冻结库存相关方法
根据前面的分析,库存数据操作有以下三项:
- 查询是否有可用的库存
- 冻结库存
- 冻结库存量修改为已售出量
- 解冻库存
在 StorageMapper
中添加三个方法:
package cn.study.storage.mapper;
import cn.study.storage.entity.Storage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
public interface StorageMapper extends BaseMapper<Storage> {
void decrease(Long productId, Integer count);
//查询库存,用来判断是否有足够可用的库存
Storage selectById(Long productId);
// 将可用库存变更为冻结库存
void updateFrozen(@Param("productId") Long productId, @Param("residue") Integer residue, @Param("frozen") Integer frozen);
// 提交时,把冻结量修改到已售出
void updateFrozenToUsed(@Param("productId") Long productId, @Param("count") Integer count);
// 回滚时,把冻结量修改到可用库存
void updateFrozenToResidue(@Param("productId") Long productId, @Param("count") Integer count);
}
那么对应的 StorageMapper.xml
中也要添加 sql:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.study.storage.mapper.StorageMapper" >
<resultMap id="BaseResultMap" type="cn.study.storage.entity.Storage" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="product_id" property="productId" jdbcType="BIGINT" />
<result column="total" property="total" jdbcType="INTEGER" />
<result column="used" property="used" jdbcType="INTEGER" />
<result column="residue" property="residue" jdbcType="INTEGER" />
</resultMap>
<update id="decrease">
UPDATE storage SET used = used + #{count},residue = residue - #{count} WHERE product_id = #{productId}
</update>
<select id="selectById" resultMap="BaseResultMap">
SELECT * FROM storage WHERE `product_id`=#{productId}
</select>
<update id="updateFrozen">
UPDATE storage SET `residue`=#{residue},`frozen`=#{frozen} WHERE `product_id`=#{productId}
</update>
<update id="updateFrozenToUsed">
UPDATE storage SET `frozen`=`frozen`-#{count}, `used`=`used`+#{count} WHERE `product_id`=#{productId}
</update>
<update id="updateFrozenToResidue">
UPDATE storage SET `frozen`=`frozen`-#{count}, `residue`=`residue`+#{count} WHERE `product_id`=#{productId}
</update>
</mapper>
3.3 Seata 实现库存的 TCC 操作方法
3.3.1 添加工具类 ResultHolder
:
package cn.study.storage.tcc;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ResultHolder {
private static Map<Class<?>, Map<String, String>> map = new ConcurrentHashMap<Class<?>, Map<String, String>>();
public static void setResult(Class<?> actionClass, String xid, String v) {
Map<String, String> results = map.get(actionClass);
if (results == null) {
synchronized (map) {
if (results == null) {
results = new ConcurrentHashMap<>();
map.put(actionClass, results);
}
}
}
results.put(xid, v);
}
public static String getResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
return results.get(xid);
}
return null;
}
public static void removeResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
results.remove(xid);
}
}
}
3.3.2 定义接口
添加 TCC 接口,在接口中添加以下方法:
Try - prepareDecreaseStorage()
Confirm - commit()
Cancel - rollback()
package cn.study.storage.tcc;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
@LocalTCC
public interface StorageTccAction {
@TwoPhaseBusinessAction(name = "storageTccAction", commitMethod = "commit", rollbackMethod = "rollback")
boolean prepareDecreaseStorage(BusinessActionContext businessActionContext,
@BusinessActionContextParameter(paramName = "productId") Long productId,
@BusinessActionContextParameter(paramName = "count") Integer count);
boolean commit(BusinessActionContext businessActionContext);
boolean rollback(BusinessActionContext businessActionContext);
}
实现类:
package cn.study.storage.tcc;
import cn.study.storage.entity.Storage;
import cn.study.storage.mapper.StorageMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Slf4j
public class StorageTccActionImpl implements StorageTccAction {
@Autowired
private StorageMapper storageMapper;
@Transactional
@Override
public boolean prepareDecreaseStorage(BusinessActionContext businessActionContext, Long productId, Integer count) {
log.info("减少商品库存,第一阶段,锁定减少的库存量,productId="+productId+", count="+count);
Storage storage = storageMapper.selectById(productId);
if (storage.getResidue()-count<0) {
throw new RuntimeException("库存不足");
}
/*
库存减掉count, 冻结库存增加count
*/
storageMapper.updateFrozen(productId, storage.getResidue()-count, storage.getFrozen()+count);
//保存标识
ResultHolder.setResult(getClass(), businessActionContext.getXid(), "p");
return true;
}
@Transactional
@Override
public boolean commit(BusinessActionContext businessActionContext) {
long productId = Long.parseLong(businessActionContext.getActionContext("productId").toString());
int count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
log.info("减少商品库存,第二阶段提交,productId="+productId+", count="+count);
storageMapper.updateFrozenToUsed(productId, count);
//删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
@Transactional
@Override
public boolean rollback(BusinessActionContext businessActionContext) {
long productId = Long.parseLong(businessActionContext.getActionContext("productId").toString());
int count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
log.info("减少商品库存,第二阶段,回滚,productId="+productId+", count="+count);
storageMapper.updateFrozenToResidue(productId, count);
//删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
}
3.3.3 在业务代码中调用 Try 阶段方法
业务代码中调用 TCC 第一阶段方法prepareDecreaseStorage()
,并添加全局事务注解 @GlobalTransactional
:
package cn.study.storage.service;
import cn.study.storage.tcc.StorageTccAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StorageServiceImpl implements StorageService {
// @Autowired
// private StorageMapper storageMapper;
@Autowired
private StorageTccAction storageTccAction;
@Override
public void decrease(Long productId, Integer count) throws Exception {
// storageMapper.decrease(productId,count);
//不直接减少库存,而是冻结库存
storageTccAction.prepareDecreaseStorage(null, productId, count);
}
}
4.事务的幂等性原则
4.1 “减少库存”分支事务测试
打开OrderServiceImpl
类中修改库存的代码,然后按顺序启动服务进行测试:
- Eureka
- Seata Server
- Easy Id Generator
- Storage
- Order
调用保存订单,地址:http://localhost:8083/create?userId=1&productId=1&count=40&money=100
观察 storage 的控制台日志:
查看数据库表中的库存数据:
4.2 扣减失败事务回滚–错误回滚
当从订单order调用库存storage,订单order冻结成功,但是库存storage冻结失败的情况下,第二阶段怎么执行呢?
是在第二阶段删除冻结的订单,将其直接回滚,但是在回滚时,并不用恢复数据,因为根本没有冻结成功;
但是目前的问题是,第一阶段冻结失败后,第二阶段还会执行回滚操作,这样就会导致数据不一致;
我们在代码中增加了判断库存数量的if语句,如果当前库存不够扣减的情况下,将会直接抛出库存不足的异常,我们持续运行当前GEI情况,让其库存出现不够扣减的情况:
我们先确定一下数据库中的内容,目前已使用数量是80,剩余库存是20,冻结库存是0,然后我们执行消耗40件库存的GET请求:
观察日志可以看到,首先将会执行第一阶段命令减少库存数量,在执行减少时,会先判断当前库存是否足够,不足够的情况下会抛出库存不足的异常;
当库存不足抛出异常后,会因为执行冻结库存异常而导致错误的数据回滚问题:
此时数据库的内容已出现错误;
4.3 扣减失败事务回滚–重复回滚
我们在前边介绍过,第一阶段执行是需要我们手动调用的,然而第二阶段的执行是自动调用的,并且其会保证其成功性,当第二阶段提交失败的情况下,TC会反复的下达指令进行重复的执行,直到其执行成功;
我们将StorageTccActionImpl
类中的回滚方法返回值改为false,模拟回滚失败的情况:
然后执4.2小节
中的案例,模拟回滚失败的情况:
观察日志我们可以看到,其会重复一直执行回滚操作,数据库中的数据也会不断进错误的叠加:
4.4 事务的幂等性控制
幂等性控制是指多次操作的结果要和一次操作的结果相同;
我们通过对操作的结果添加一个是否成功的标志来实现幂等性的控制,当第一阶段冻结成功时,我们添加一个一阶段成功的标志,二阶段执行前首先检查有没有一阶段成功的标志,有标志执行没有标志就不执行;从而解决重复执行的问题;
并且在二阶段回滚完成后,需要删掉此标志,防止影响后续其他操作;并且通过删除标志,可以解决错误回滚问题;
首先我们需要在Order类和Storage类中添加工具类 ResultHolder
,具体可以参考第2小节和第3小节;
我们在OrderTccActionImpl
的回滚方法和StorageTccActionImpl
的提交和回滚方法中添加该方法:
我们通过封装类ResultHolder
来封装一个Map对象:
这个Map对象中,有多个TccAction对象名作为key,其value值为一个嵌套的Map对象;
这个嵌套的Map对象存放的是俩个字符串,key为事务ID,值为一个任意的不为null的值(我们只需要判断是否有这个标志即可,并不对值的内容做要求);
在多个TccAction中,可能会有多个事务同时执行,所以也会存在多个事务ID,我们通过对事务ID进行赋值来设置一个可以判断是否成功的标志来实现幂等性的控制;
并且,为了避免在删除标记前,其他请求同时请求产生错误,相关代码都需要添加synchronized
同步关键字;
4.account添加“扣减金额”分支事务
扣减金额 TCC 事务分析请见–3.6 分布式事务(六)Seata TCC模式-TCC模式介绍
4.1 配置
有三个文件需要配置:
application.yml
registry.conf
file.conf
这三个文件的设置与上面 order 项目的配置完全相同,请参考上面订单配置一章进行配置。
4.2 AccountMapper 添加冻结库存相关方法
根据前面的分析,库存数据操作有以下三项:
- 冻结库存
- 冻结库存量修改为已售出量
- 解冻库存
在 AccountMapper
中添加三个方法:
package cn.study.account.mapper;
import cn.study.account.entity.Account;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.math.BigDecimal;
public interface AccountMapper extends BaseMapper<Account> {
void decrease(Long userId, BigDecimal money);
void updateFrozen(@Param("userId") Long userId, @Param("residue") BigDecimal residue, @Param("frozen") BigDecimal frozen);
void updateFrozenToUsed(@Param("userId") Long userId, @Param("money") BigDecimal money);
void updateFrozenToResidue(@Param("userId") Long userId, @Param("money") BigDecimal money);
}
那么对应的 AccountMapper.xml
中添加 sql:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.study.account.mapper.AccountMapper" >
<resultMap id="BaseResultMap" type="cn.study.account.entity.Account" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="user_id" property="userId" jdbcType="BIGINT" />
<result column="total" property="total" jdbcType="DECIMAL" />
<result column="used" property="used" jdbcType="DECIMAL" />
<result column="residue" property="residue" jdbcType="DECIMAL"/>
<result column="frozen" property="frozen" jdbcType="DECIMAL"/>
</resultMap>
<update id="decrease">
UPDATE account SET residue = residue - #{money},used = used + #{money} where user_id = #{userId};
</update>
<select id="selectById" resultMap="BaseResultMap">
SELECT * FROM account WHERE `user_id`=#{userId}
</select>
<update id="updateFrozen">
UPDATE account SET `residue`=#{residue},`frozen`=#{frozen} WHERE `user_id`=#{userId}
</update>
<update id="updateFrozenToUsed">
UPDATE account SET `frozen`=`frozen`-#{money}, `used`=`used`+#{money} WHERE `user_id`=#{userId}
</update>
<update id="updateFrozenToResidue">
UPDATE account SET `frozen`=`frozen`-#{money}, `residue`=`residue`+#{money} WHERE `user_id`=#{userId}
</update>
</mapper>
4.3 Seata 实现库存的 TCC 操作方法
4.3.1 添加工具类 ResultHolder:
package cn.study.account.tcc;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ResultHolder {
private static Map<Class<?>, Map<String, String>> map = new ConcurrentHashMap<Class<?>, Map<String, String>>();
public static void setResult(Class<?> actionClass, String xid, String v) {
Map<String, String> results = map.get(actionClass);
if (results == null) {
synchronized (map) {
if (results == null) {
results = new ConcurrentHashMap<>();
map.put(actionClass, results);
}
}
}
results.put(xid, v);
}
public static String getResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
return results.get(xid);
}
return null;
}
public static void removeResult(Class<?> actionClass, String xid) {
Map<String, String> results = map.get(actionClass);
if (results != null) {
results.remove(xid);
}
}
}
4.3.2 定义接口
添加 TCC 接口,在接口中添加以下方法:
Try - prepareDecreaseAccount()
Confirm - commit()
Cancel - rollback()
package cn.study.account.tcc;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import java.math.BigDecimal;
@LocalTCC
public interface AccountTccAction {
@TwoPhaseBusinessAction(name = "accountTccAction", commitMethod = "commit", rollbackMethod = "rollback")
boolean prepareDecreaseAccount(BusinessActionContext businessActionContext,
@BusinessActionContextParameter(paramName = "userId") Long userId,
@BusinessActionContextParameter(paramName = "money") BigDecimal money);
boolean commit(BusinessActionContext businessActionContext);
boolean rollback(BusinessActionContext businessActionContext);
}
实现类:
package cn.study.account.tcc;
import cn.study.account.entity.Account;
import cn.study.account.mapper.AccountMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
@Component
@Slf4j
public class AccountTccActionImpl implements AccountTccAction {
@Autowired
private AccountMapper accountMapper;
@Transactional
@Override
public boolean prepareDecreaseAccount(BusinessActionContext businessActionContext, Long userId, BigDecimal money) {
log.info("减少账户金额,第一阶段锁定金额,userId="+userId+", money="+money);
Account account = accountMapper.selectById(userId);
if (account.getResidue().compareTo(money) < 0) {
throw new RuntimeException("账户金额不足");
}
/*
余额-money
冻结+money
*/
accountMapper.updateFrozen(userId, account.getResidue().subtract(money), account.getFrozen().add(money));
//保存标识
ResultHolder.setResult(getClass(), businessActionContext.getXid(), "p");
return true;
}
@Transactional
@Override
public boolean commit(BusinessActionContext businessActionContext) {
long userId = Long.parseLong(businessActionContext.getActionContext("userId").toString());
BigDecimal money = new BigDecimal(businessActionContext.getActionContext("money").toString());
log.info("减少账户金额,第二阶段,提交,userId="+userId+", money="+money);
//防止重复提交
if (ResultHolder.getResult(getClass(), businessActionContext.getXid()) == null) {
return true;
}
accountMapper.updateFrozenToUsed(userId, money);
//删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
@Transactional
@Override
public boolean rollback(BusinessActionContext businessActionContext) {
long userId = Long.parseLong(businessActionContext.getActionContext("userId").toString());
BigDecimal money = new BigDecimal(businessActionContext.getActionContext("money").toString());
//防止重复回滚
if (ResultHolder.getResult(getClass(), businessActionContext.getXid()) == null) {
return true;
}
log.info("减少账户金额,第二阶段,回滚,userId="+userId+", money="+money);
accountMapper.updateFrozenToResidue(userId, money);
//删除标识
ResultHolder.removeResult(getClass(), businessActionContext.getXid());
return true;
}
}
4.3.3 在业务代码中调用 Try 阶段方法
业务代码中调用 TCC 第一阶段方法prepareDecreaseAccount()
,并添加全局事务注解 @GlobalTransactional
:
package cn.study.account.service;
import cn.study.account.mapper.AccountMapper;
import cn.study.account.tcc.AccountTccAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class AccountServiceImpl implements AccountService {
// @Autowired
// private AccountMapper accountMapper;
@Autowired
private AccountTccAction accountTccAction;
@Override
public void decrease(Long userId, BigDecimal money) {
// accountMapper.decrease(userId,money);
accountTccAction.prepareDecreaseAccount(null, userId, money);
}
}
4.4 启动 account 进行测试
按顺序启动服务:
- Eureka
- Seata Server
- Easy Id Generator
- Storage
- Account
- Order
将此前订单类注释掉的调用账户类的代码取消注释后,调用保存订单,地址:http://localhost:8083/create?userId=1&productId=1&count=10&money=100
观察 account 的控制台日志:
查看数据库表中的账户数据:
5.全局事务回滚测试
下面来测试全局事务回滚的情况。
订单和库存第一阶段成功,而账户第一阶段失败了,这时会触发全局事务的回滚,如下图所示:
首先在 account 的第一阶段代码中添加模拟异常:
AccountTccActionImpl
的 prepareDecreaseAccount
方法
@Transactional
@Override
public boolean prepareDecreaseAccount(BusinessActionContext businessActionContext, Long userId, BigDecimal money) {
log.info("减少账户金额,第一阶段锁定金额,userId="+userId+", money="+money);
Account account = accountMapper.selectById(userId);
if (account.getResidue().compareTo(money) < 0) {
throw new RuntimeException("账户金额不足");
}
/*
余额-money
冻结+money
*/
accountMapper.updateFrozen(userId, account.getResidue().subtract(money), account.getFrozen().add(money));
if (Math.random() < 0.5) {
throw new RuntimeException("模拟异常");
}
//保存标识
ResultHolder.setResult(getClass(), businessActionContext.getXid(), "p");
return true;
}
重启 account 后,访问订单:http://localhost:8083/create?userId=1&productId=1&count=10&money=100
查看控制台,可以看到 storage 和 order 的回滚日志,order 的回滚日志如下: