手把手教你Spring Cloud集成Seata TCC模式(下)

简介: 手把手教你Spring Cloud集成Seata TCC模式

1.在预创建订单服务中,我们使用了一个status字段来表示订单的中间状态,还未生效的订单状态为预创建;

2.当收到提交请求时,我们将订单状态置为成功

3.当收到回滚请求时,我们删除之前预创建的订单即可;

Storage服务

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;
/**
 * @author zouwei
 * @className IStorageTccAction
 * @date: 2022/10/11 13:57
 * @description:
 */
@LocalTCC
public interface IStorageTccAction {
  /**
   * 预扣库存
   *
   * @param businessActionContext
   * @param commodityCode
   * @param count
   * @return
   */
  @TwoPhaseBusinessAction(name = "prepareDeductStock", commitMethod = "commitDeductStock", rollbackMethod = "rollbackDeductStock")
  boolean prepareDeductStock(BusinessActionContext businessActionContext,
                             @BusinessActionContextParameter("commodityCode") String commodityCode,
                             @BusinessActionContextParameter("count") int count);
  /**
   * 提交被扣库存
   *
   * @param businessActionContext
   * @return
   */
  boolean commitDeductStock(BusinessActionContext businessActionContext);
  /**
   * 回滚被扣库存
   *
   * @param businessActionContext
   * @return
   */
  boolean rollbackDeductStock(BusinessActionContext businessActionContext);
}
复制代码
import com.example.awesomestorage.dao.mapper.StockTccEnhanceMapper;
import com.example.awesomestorage.tcc.IStorageTccAction;
import com.example.awesomestorage.tcc.TccActionResultWrap;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Map;
/**
 * @author zouwei
 * @className StorageTccActionImpl
 * @date: 2022/10/11 14:00
 * @description:
 */
@Component
public class StorageTccActionImpl implements IStorageTccAction {
  @Resource
  private StockTccEnhanceMapper stockTccEnhanceMapper;
  @Override
  public boolean prepareDeductStock(BusinessActionContext businessActionContext, String commodityCode, int count) {
    String xid = businessActionContext.getXid();
    // 幂等性判断
    if (TccActionResultWrap.hasPrepareResult(xid)) {
      return true;
    }
    // 避免空悬挂,已经执行过回滚了就不能再预留资源
    if (TccActionResultWrap.hasRollbackResult(xid) || TccActionResultWrap.hasCommitResult(xid)) {
      return false;
    }
    // 预扣库存
    // 相关sql: update stock_tcc_tbl set count = count - #{count,jdbcType=INTEGER}, pre_deduct_count = pre_deduct_count + #{count,jdbcType=INTEGER} where commodity_code = #{commodityCode,jdbcType=VARCHAR} and count <![CDATA[ >= ]]> #{count,jdbcType=INTEGER}
    boolean result = stockTccEnhanceMapper.prepareDeductStock(commodityCode, count) > 0;
    // 记录执行结果:xid:result
    // 以便回滚时判断是否是空回滚
    TccActionResultWrap.prepareSuccess(xid);
    return result;
  }
  @Transactional
  @Override
  public boolean commitDeductStock(BusinessActionContext businessActionContext) {
    String xid = businessActionContext.getXid();
    // 幂等性判断
    if (TccActionResultWrap.hasCommitResult(xid)) {
      return true;
    }
    Map<String, Object> actionContext = businessActionContext.getActionContext();
    String commodityCode = (String) actionContext.get("commodityCode");
    int count = (int) actionContext.get("count");
    // 执行提交操作,扣除库存
    // 相关sql:update stock_tcc_tbl set pre_deduct_count = pre_deduct_count - #{count,jdbcType=INTEGER} where commodity_code = #{commodityCode,jdbcType=VARCHAR} and pre_deduct_count <![CDATA[ >= ]]> #{count,jdbcType=INTEGER}
    boolean result = stockTccEnhanceMapper.commitDeductStock(commodityCode, count) > 0;
    // 清除预留结果
    TccActionResultWrap.removePrepareResult(xid);
    // 设置提交结果
    TccActionResultWrap.commitSuccess(xid);
    return result;
  }
  @Transactional
  @Override
  public boolean rollbackDeductStock(BusinessActionContext businessActionContext) {
    String xid = businessActionContext.getXid();
    // 幂等性判断
    if (TccActionResultWrap.hasRollbackResult(xid)) {
      return true;
    }
    // 没有预留资源结果,回滚不做任何处理;
    if (!TccActionResultWrap.hasPrepareResult(xid)) {
      // 设置回滚结果,防止空悬挂
      TccActionResultWrap.rollbackResult(xid);
      return true;
    }
    // 执行回滚
    Map<String, Object> actionContext = businessActionContext.getActionContext();
    String commodityCode = (String) actionContext.get("commodityCode");
    int count = (int) actionContext.get("count");
    // 相关sql: update stock_tcc_tbl set count = count + #{count,jdbcType=INTEGER}, pre_deduct_count = pre_deduct_count - #{count,jdbcType=INTEGER} where commodity_code = #{commodityCode,jdbcType=VARCHAR} and pre_deduct_count <![CDATA[ >= ]]> #{count,jdbcType=INTEGER}
    boolean result = stockTccEnhanceMapper.rollbackDeductStock(commodityCode, count) > 0;
    // 清除预留结果
    TccActionResultWrap.removePrepareResult(xid);
    // 设置回滚结果
    TccActionResultWrap.rollbackResult(xid);
    return result;
  }
}
复制代码

1.预扣减库存的原理和预扣款原理一致,我们需要额外给出一个字段pre_deduct_count来放预扣的库存数量;通过减少总库存数count,把被扣库存预留到pre_deduct_count中达到预扣的效果;

2.当需要提交时,我们才真正地从pre_deduct_count中扣除我们需要的库存数;

3.当接收到回滚请求时,我们把已经预扣的数量还给总库存count

再补充一个TccActionResultWrap,这个类用来记录TCC Action的状态,用来临时解决幂等性空回滚空悬挂问题。

import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @author zouwei
 * @className TccActionResultWrap
 * @date: 2022/10/11 13:00
 * @description:
 */
public class TccActionResultWrap {
  private static final Map<String, String> prepareResult = new ConcurrentHashMap<>();
  private static final Map<String, String> commitResult = new ConcurrentHashMap<>();
  private static final Map<String, String> rollbackResult = new ConcurrentHashMap<>();
  public static void prepareSuccess(String xid) {
    prepareResult.put(xid, "success");
  }
  public static void commitSuccess(String xid) {
    commitResult.put(xid, "success");
  }
  public static void rollbackSuccess(String xid) {
    rollbackResult.put(xid, "success");
  }
  public static void removePrepareResult(String xid) {
    prepareResult.remove(xid);
  }
  public static void removeCommitResult(String xid) {
    commitResult.remove(xid);
  }
  public static void removeRollbackResult(String xid) {
    rollbackResult.remove(xid);
  }
  public static String prepareResult(String xid) {
    return prepareResult.get(xid);
  }
  public static String commitResult(String xid) {
    return commitResult.get(xid);
  }
  public static String rollbackResult(String xid) {
    return rollbackResult.get(xid);
  }
  public static boolean hasPrepareResult(String xid) {
    return StringUtils.isNotBlank(prepareResult(xid));
  }
  public static boolean hasCommitResult(String xid) {
    return StringUtils.isNotBlank(commitResult.get(xid));
  }
  public static boolean hasRollbackResult(String xid) {
    return StringUtils.isNotBlank(rollbackResult.get(xid));
  }
}
复制代码

以上我们就把所有服务的TCC Action全部处理完毕,我们下面来看看如何使用这些TCC Action:

Service如何使用TCC Action

Account服务

import com.example.awesomeaccount.service.IWalletService;
import com.example.awesomeaccount.tcc.IWalletTccAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
 * @author zouwei
 * @className WalletServiceImpl
 * @date: 2022/9/27 21:20
 * @description:
 */
@Service
public class WalletServiceImpl implements IWalletService {
  @Autowired
  private IWalletTccAction walletTccAction;
  @Transactional
  @Override
  public Boolean deductMoney(String userId, long amount) {
    return walletTccAction.prepareDeductMoney(null, userId, amount);
  }
}
复制代码

我们只要在service中直接调用IWalletTccAction接口中的prepareDeductMoney方法执行预扣款即可,这个prepareDeductMoney方法其实就代表一个真正扣款的功能,因为在所有的调用链都调用成功后,Seata会自动调用TCC Action中定义的提交逻辑,以达到一个最终的扣款目的。

注意:参数中的BusinessActionContext不需要开发人员自己传递,直接给null即可,Seata会自动处理。

Order服务

import com.example.awesomeorder.api.StockApiClient;
import com.example.awesomeorder.service.IOrderService;
import com.example.awesomeorder.tcc.IOrderTccAction;
import com.example.storageapi.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
 * @author zouwei
 * @className OrderServiceImpl
 * @date: 2022/9/27 22:47
 * @description:
 */
@Service
public class OrderServiceImpl implements IOrderService {
  @Autowired
  private IOrderTccAction orderTccAction;
  @Resource
  private StockApiClient stockApiClient;
  /**
   * 创建订单
   *
   * @param userId
   * @param commodityCode
   * @param count
   * @param unitPrice
   * @return
   */
  @Transactional
  @Override
  public Boolean createOrder(String userId, String commodityCode, int count, long unitPrice) {
    // 构建待扣减的库存信息
    OrderInfo orderInfo = new OrderInfo();
    // 设置商品编码
    orderInfo.setCommodityCode(commodityCode);
    // 设置需要扣减的数量
    orderInfo.setCount(count);
    // 先预扣减库存
    if (stockApiClient.deductStock(orderInfo)) {
      // 预创建订单
      return orderTccAction.prepareOrder(null, userId, commodityCode, count, unitPrice);
    }
    // 扣减库存失败,订单创建失败
    return Boolean.FALSE;
  }
}
复制代码

Order服务的Service实现和Account服务差不多,TCC Action也可以和Spring Bean一样使用;

Storage服务:

import com.example.awesomestorage.service.IStockService;
import com.example.awesomestorage.tcc.IStorageTccAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * @author zouwei
 * @className StockServiceImpl
 * @date: 2022/9/28 00:06
 * @description:
 */
@Service
public class StockServiceImpl implements IStockService {
  @Autowired
  private IStorageTccAction storageTccAction;
  @Transactional
  @Override
  public Boolean deductStock(String commodityCode, int count) {
    return storageTccAction.prepareDeductStock(null, commodityCode, count);
  }
}
复制代码

Business下单逻辑

import com.example.accountapi.model.AmountInfo;
import com.example.awesomebusiness.api.OrderApiClient;
import com.example.awesomebusiness.api.WalletApiClient;
import com.example.awesomebusiness.service.ShoppingCartService;
import com.example.orderapi.model.OrderInfo;
import io.seata.core.exception.GlobalTransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @author zouwei
 * @className ShoppingCartServiceImpl
 * @date: 2022/9/18 14:01
 * @description:
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
  // 钱包服务
  @Resource
  private WalletApiClient walletApiClient;
  // 订单服务
  @Resource
  private OrderApiClient orderApiClient;
  // 别忘记了这个注解,这是开启分布式事务的标记,前提是当前业务逻辑不处于任何的分布式事务当中才能开启新的分布式事务
  @GlobalTransactional
  public String placeOrder() throws GlobalTransactionException {
    // 模拟用户ID 123456,对应数据库初始化的用户ID
    String userId = "123456";
    // 构建订单数据
    OrderInfo orderInfo = new OrderInfo();
    // 数量15个
    orderInfo.setCount(15);
    // 商品编码,对应库存数据表的初始化数据
    orderInfo.setCommodityCode("CC-54321");
    // 单价299,默认是long类型,单位分;避免double精度丢失
    orderInfo.setUnitPrice(299);
    // 订单归属
    orderInfo.setUserId(userId);
    // 计算扣款金额,数量*单价
    long amount = orderInfo.getCount() * orderInfo.getUnitPrice();
    // 构建扣款数据
    AmountInfo amountInfo = new AmountInfo();
    // 设置扣款金额
    amountInfo.setAmount(amount);
    // 设置扣款主体
    amountInfo.setUserId(userId);
    // 先扣款,扣款成功就创建订单,扣减库存在创建订单的逻辑里面
    if (walletApiClient.deductMoney(amountInfo) && orderApiClient.createOrder(orderInfo)) {
      return "下单成功!";
    }
    // 1.扣款失败,抛异常,分布式事务回滚
    // 2.创建订单失败,抛异常,分布式事务回滚
    throw new GlobalTransactionException("下单失败!");
  }
}
复制代码
  • 在Business下单逻辑中,和之前AT模式完全没有变化,所有的RPC依然还是使用的FeignClient;
  • @GlobalTransactional还是放在方法上面,代表这是TM角色,负责分布式事务的发起、提交和回滚;
  • XID的传递依然是RPC来传递;

至此,我们已经把所有的思路及相关核心代码编写完毕,有兴趣的小伙伴可以在github上下载该项目:awesome-seata


相关文章
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
|
3月前
|
NoSQL Java Nacos
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
80 3
|
22天前
|
SQL NoSQL 数据库
SpringCloud基础6——分布式事务,Seata
分布式事务、ACID原则、CAP定理、Seata、Seata的四种分布式方案:XA、AT、TCC、SAGA模式
SpringCloud基础6——分布式事务,Seata
|
3月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14659 24
|
2月前
|
关系型数据库 MySQL 数据库
SpringCloud2023中使用Seata解决分布式事务
对于分布式系统而言,需要保证分布式系统中的数据一致性,保证数据在子系统中始终保持一致,避免业务出现问题。分布式系统中对数据的操作要么一起成功,要么一起失败,必须是一个整体性的事务。Seata简化了这个使用过程。
55 2
|
2月前
|
人工智能 Java API
JeecgBoot 低代码平台快速集成 Spring AI
Spring 通过 Spring AI 项目正式启用了 AI(人工智能)生成提示功能。本文将带你了解如何在 Jeecg Boot 应用中集成生成式 AI,以及 Spring AI 如何与模型互动,包含 RAG 功能。
101 3
|
2月前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
3月前
|
JSON 缓存 Java
Spring Boot集成 Swagger2 展现在线接口文档
本节课详细分析了 Swagger 的优点,以及 Spring Boot 如何集成 Swagger2,包括配置,相关注解的讲解,涉及到了实体类和接口类,以及如何使用。最后通过页面测试,体验了 Swagger 的强大之处,基本上是每个项目组中必备的工具之一,所以要掌握该工具的使用,也不难。
|
3月前
|
SQL Java 数据库
实时计算 Flink版产品使用问题之Spring Boot集成Flink可以通过什么方式实现通过接口启动和关闭Flink程序
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
3月前
|
开发者 Windows
三类代码协同模式问题之判断项目的协同规模决定采用集成分支问题如何解决
三类代码协同模式问题之判断项目的协同规模决定采用集成分支问题如何解决

热门文章

最新文章

下一篇
无影云桌面