• 95--分布式事务六-Seata TCC模式-Spring Cloud微服务案例(添加TCC事务)


    分布式事务(六)Seata TCC模式-介绍以及案例

    TCC 基本原理

    TCC 与 Seata AT 事务一样都是两阶段事务,它与 AT 事务的主要区别为:

    • TCC 对业务代码侵入严重
      每个阶段的数据操作都要自己进行编码来实现,事务框架无法自动处理。
    • TCC 效率更高
      不必对数据加全局锁,允许多个事务同时操作数据。

    a

    准备订单项目案例

    新建 seata-tcc 工程

    新建 Empty Project:

    导入订单项目,无事务版本

    下载项目代码

    1. 访问 git 仓库 https://gitee.com/benwang6/seata-samples
    2. 访问项目标签
    3. 下载无事务版本的,然后解压到seta-tcc工程下

    导入项目

    • 在 idea 中按两下 shift 键,搜索 add maven projects,打开 maven 工具:
    • 然后选择 seata-tcc 工程目录下的 7 个项目的 pom.xml 导入:

    order启动全局事务,添加“保存订单”分支事务

    在订单项目中执行添加订单:

    a

    添加以下 TCC 事务操作的代码:

    • Try - 第一阶,冻结数据阶段,向订单表直接插入订单,订单状态设置为0(冻结状态)。

    a

    • Confirm - 第二阶段,提交事务,将订单状态修改成1(正常状态)。

    a

    • Cancel - 第二阶段,回滚事务,删除订单。

    a

    order-parent 添加 seata 依赖

    <!-- 打开 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>
    
    

    配置

    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: 1234
    
      # 事务组设置
      cloud:
        alibaba:
          seata:
            tx-service-group: order_tx_group
    
    ......
    
    
    

    registry.conf 和 file.conf

    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
      }
    }
    
    

    OrderMapper 添加更新订单状态、删除订单

    根据前面的分析,订单数据操作有以下三项:

    • 插入订单 ---新插入的订单是冻结状态的。
    • 修改订单状态 ----二阶段提交订单的时候,将订单的状态修改为正常。
    • 删除订单 ---当事务回滚的时候,删除已经创建的订单。

    在 OrderMapper 中已经有插入订单的方法了,现在需要添加修改订单和删除订单的方法(删除方法从BaseMapper继承):

    package cn.tedu.order.mapper;
    
    import cn.tedu.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);
        //删除订单的方法通过BaseMapper中的方法
    }
    

    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.tedu.order.mapper.OrderMapper" >
        <resultMap id="BaseResultMap" type="cn.tedu.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>
    
    

    Seata 实现订单的 TCC 操作方法

    • 第一阶段 Try
    • 第二阶段
      • Confirm
      • Cancel

    第二阶段为了处理幂等性问题这里首先添加一个工具类 ResultHolder

    这个工具也可以在第二阶段 Confirm 或 Cancel 阶段对第一阶段的成功与否进行判断,在第一阶段成功时需要保存一个标识。

    幂等性:就是用户对于同一操作发起的一次请求或者多次请求的结果是一致的,不会因为多次点击而产生了了副作用。举个最简单的例子,那就是支付,用户购买商品后支付,支付扣款成功,但是返回结果的时候网络异常,此时钱已经扣了,用户再次点击按钮,此时会进行第二次扣款,返回结果成功,用户查询余额发现多扣了钱,流水记录也变成了两条。这就是幂等性问题
    

    ResultHolder可以为每一个全局事务保存一个标识:

    package cn.tedu.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);
            }
        }
    }
    
    
    

    Seata 实现 TCC 操作需要定义一个接口,在接口中添加以下方法:

    • Try - prepareCreateOrder() --方法的名称可以根据实际业务指定
    • Confirm - commit()
    • Cancel - rollback()
    package cn.tedu.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 {
    
        /*
        第一阶段的方法
        通过注解指定第二阶段的两个方法名
    
        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.tedu.order.tcc;
    
    import cn.tedu.order.entity.Order;
    import cn.tedu.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) {
            //插入订单 设置状态为0 --冻结
            orderMapper.create(new Order(orderId,userId,productId,count,money,0));
            log.info("创建订单第一阶段 冻结订单成功");
            //第一阶段成功 添加一个标识
            ResultHolder.setResult(OrderTccAction.class,businessActionContext.getXid(),"p");
            return true;
        }
        @Transactional
        @Override
        public boolean commit(BusinessActionContext businessActionContext) {
           //判断第一阶段的成功标记,没有标记则不执行提交操作
            if (ResultHolder.getResult(OrderTccAction.class,businessActionContext.getXid())==null){
                return true;
            }
    
    
            //修改订单状态 0---》1 正常状态的转变
            //通过订单id
            Long orderId = Long.parseLong(businessActionContext.getActionContext("orderId").toString());
            orderMapper.updateStatus(orderId,1);
            log.info("创建订单第二阶段 提交订单 解冻成功");
    
            //删除标识 防止一直重复提交
            ResultHolder.removeResult(OrderTccAction.class,businessActionContext.getXid());
    
            return true;
        }
        @Transactional
        @Override
        public boolean rollback(BusinessActionContext businessActionContext) {
            //第一阶段没有完成的情况下,不必执行回滚
            //因为第一阶段有本地事务,事务失败时已经进行了回滚。
            //如果这里第一阶段成功,而其他全局事务参与者失败,这里会执行回滚
            //幂等性控制:如果重复执行回滚则直接返回
            log.info("创建 order 第二阶段回滚,删除订单 - "+businessActionContext.getXid());
            //判断第一阶段的成功标记,没有标记则不执行提交操作
            if (ResultHolder.getResult(OrderTccAction.class,businessActionContext.getXid())==null){
                return true;
            }
    
            //通过订单id
            Long orderId = Long.parseLong(businessActionContext.getActionContext("orderId").toString());
            orderMapper.deleteById(orderId);
            log.info("创建订单第二阶段 回滚订单 删除成功");
            //删除标识 防止一直重复提交
            ResultHolder.removeResult(OrderTccAction.class,businessActionContext.getXid());
    
            return true;
        }
    }
    
    

    在业务代码中调用 Try 阶段方法

    业务代码中不再直接保存订单数据,而是调用 TCC 第一阶段方法prepareCreateOrder(),并添加全局事务注解 @GlobalTransactional

    package cn.tedu.order.service;
    
    import cn.tedu.order.entity.Order;
    import cn.tedu.order.feign.AccountClient;
    import cn.tedu.order.feign.EasyIdGeneratorClient;
    import cn.tedu.order.feign.StorageClient;
    import cn.tedu.order.mapper.OrderMapper;
    import cn.tedu.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
        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 第一阶段方法
            // JDK代理调用的 AOP 生成动态代理对象,拦截器会自动创建context 对象
            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());
    
        }
    }
    

    启动 order 进行测试

    按顺序启动服务:

    1. Eureka
    2. Seata Server ---该服务需要单独启动
    3. Easy Id Generator
    4. Order

    调用保存订单,地址:
    http://localhost:8083/create?userId=1&productId=1&count=10&money=100

    观察控制台日志:

    a

    查看数据库表中的订单数据:

    a

    storage添加“减少库存”分支事务

    在库存项目中执行减少库存:

    a

    我们要添加以下 TCC 事务操作的代码:

    • Try - 第一阶,冻结数据阶段,将要减少的库存量先冻结:

    a

    • Confirm - 第二阶段,提交事务,使用冻结的库存完成业务数据处理:

    a

    • Cancel - 第二阶段,回滚事务,冻结的库存解冻,恢复以前的库存量:

    a

    配置

    有三个文件需要配置:

    • application.yml
    • registry.conf
    • file.conf

    以上三个配置文件和order相应的配置文件完全相同

    StorageMapper 添加冻结库存相关方法

    根据前面的分析,库存数据操作有以下三项:

    • 冻结库存
    • 冻结库存量修改为已售出量
    • 解冻库存

    在 StorageMapper 中添加三个方法:

    package cn.tedu.storage.mapper;
    
    import cn.tedu.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);
    
        // 冻结库存
        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文件的修改配置

    <?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.tedu.storage.mapper.StorageMapper" >
        <resultMap id="BaseResultMap" type="cn.tedu.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>
     
    

    Seata 实现库存的 TCC 操作方法

    工具类 ResultHolder

    复制order服务中的ResultHolder方法即可

    添加 TCC 接口,在接口中添加以下方法:

    • Try - prepareDecreaseStorage()
    • Confirm - commit()
    • Cancel - rollback()
    package cn.tedu.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")
        boolean prepareDecreaseStorage(BusinessActionContext businessActionContext,
                @BusinessActionContextParameter(paramName = "productId") Long productId,
                @BusinessActionContextParameter(paramName = "count") Integer count
        );
        boolean commit(BusinessActionContext businessActionContext);
        boolean rollback(BusinessActionContext businessActionContext);
    
    
    }
    
    

    实现类

    package cn.tedu.storage.tcc;
    
    import cn.tedu.storage.entity.Storage;
    import cn.tedu.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("减少库存第一阶段 开始执行冻结操作");
          //先查询库存 看是否可以冻结
            Storage storage = storageMapper.selectById(productId);
            if (storage.getResidue()<count){
                throw new  RuntimeException("库存量不足,无法完成冻结操作");
            }
            //冻结操作
            storageMapper.updateFrozen(storage.getProductId(),storage.getResidue()-count,storage.getFrozen()+count);
    
            //添加标记
            ResultHolder.setResult(StorageTccAction.class,businessActionContext.getXid(),"p");
            log.info("减少库存第一阶段 执行冻结操作成功");
    
            return true;
        }
        @Transactional
        @Override
        public boolean commit(BusinessActionContext businessActionContext) {
            if (ResultHolder.getResult(StorageTccAction.class,businessActionContext.getXid())==null){
                return  true;
            }
            log.info("减少库存第二阶段 开始执行提交操作");
    
            long productId = Long.parseLong(businessActionContext.getActionContext("productId").toString());
            int count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
    
            storageMapper.updateFrozenToUsed(productId,count);
            log.info("减少库存第二阶段 执行提交操作完毕");
    
            //删除标识
            ResultHolder.removeResult(StorageTccAction.class, businessActionContext.getXid());
            return false;
        }
    
        @Override
        public boolean rollback(BusinessActionContext businessActionContext) {
            //防止重复的回滚
            if (ResultHolder.getResult(StorageTccAction.class,businessActionContext.getXid())==null){
                return  true;
            }
            log.info("减少库存第二阶段 执行回滚操作");
            long productId = Long.parseLong(businessActionContext.getActionContext("productId").toString());
            int count = Integer.parseInt(businessActionContext.getActionContext("count").toString());
            storageMapper.updateFrozenToResidue(productId,count);
            log.info("减少库存第二阶段 执行回滚操作完毕");
            //删除标识
            ResultHolder.removeResult(StorageTccAction.class, businessActionContext.getXid());
            return true;
        }
    }
    
    

    在业务代码中调用 Try 阶段方法

    业务代码中调用 TCC 第一阶段方法prepareDecreaseStorage(),并添加全局事务注解 @GlobalTransactional

    package cn.tedu.storage.service;
    
    import cn.tedu.storage.mapper.StorageMapper;
    import cn.tedu.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);
            //调用TCC的第一阶段方法
            storageTccAction.prepareDecreaseStorage(null,productId,count);
        }
    }
    

    启动 storage 进行测试

    按顺序启动服务:

    1. Eureka
    2. Seata Server
    3. Easy Id Generator
    4. Storage
    5. Order

    调用保存订单,地址:
    http://localhost:8083/create?userId=1&productId=1&count=10&money=100

    观察 storage 的控制台日志:
    a

    查看数据库表中的库存数据:

    a

    account添加“扣减金额”分支事务

    第一阶段 Try

    以账户服务为例,当下订单时要扣减用户账户金额:

    a

    假如用户购买 100 元商品,要扣减 100 元。

    TCC 事务首先对这100元的扣减金额进行预留,或者说是先冻结这100元:

    a

    第二阶段 Confirm

    如果第一阶段能够顺利完成,那么说明“扣减金额”业务(分支事务)最终肯定是可以成功的。当全局事务提交时, TC会控制当前分支事务进行提交,如果提交失败,TC 会反复尝试,直到提交成功为止。

    当全局事务提交时,就可以使用冻结的金额来最终实现业务数据操作:
    a

    第二阶段 Cancel

    如果全局事务回滚,就把冻结的金额进行解冻,恢复到以前的状态,TC 会控制当前分支事务回滚,如果回滚失败,TC 会反复尝试,直到回滚完成为止。

    a

    多个事务并发的情况

    多个TCC全局事务允许并发,它们执行扣减金额时,只需要冻结各自的金额即可:

    a

    配置

    有三个文件需要配置:

    • application.yml
    • registry.conf
    • file.conf

    以上三个文件的设置与上面 order 项目的配置完全相同。

    AccountMapper 添加冻结金额相关方法

    根据前面的分析,库存数据操作有以下三项:

    • 冻结金额
    • 冻结金额修改为已使用量
    • 解冻金额

    在 AccountMapper 中添加三个方法:

    package cn.tedu.account.mapper;
    
    import cn.tedu.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文件编写

    <?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.tedu.account.mapper.AccountMapper">
        <resultMap id="BaseResultMap" type="cn.tedu.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>
    

    Seata 实现库存的 TCC 操作方法

    工具类 ResultHolder

    复制order服务中的ResultHolder方法即可

    添加 TCC 接口,在接口中添加以下方法:

    • Try - prepareDecreaseAccount()
    • Confirm - commit()
    • Cancel - rollback()
    package cn.tedu.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")
        boolean prepareDecreaseAccount(BusinessActionContext businessActionContext,
                   @BusinessActionContextParameter(paramName = "userId") Long userId,
                   @BusinessActionContextParameter(paramName = "money")BigDecimal money
    
                   );
    
        boolean commit(BusinessActionContext businessActionContext);
    
        boolean rollback(BusinessActionContext businessActionContext);
    
    }
    

    实现类

    package cn.tedu.account.tcc;
    
    import cn.tedu.account.entity.Account;
    import cn.tedu.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("扣减金额第一阶段 ,开始执行冻结金额操作");
            Account account = accountMapper.selectById(userId);
            if (account.getResidue().compareTo(money)<0){
                throw  new  RuntimeException("可用金额不足,金额冻结失败");
            }
            //执行冻结操作
            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");
            log.info("扣减金额第一阶段 ,执行冻结金额完成");
            return false;
        }
        @Transactional
        @Override
        public boolean commit(BusinessActionContext businessActionContext) {
            log.info("扣减金额第二阶段 ,开始执行提交操作");
            //防止重复提交
            if (ResultHolder.getResult(getClass(),businessActionContext.getXid())==null){
                return  true;
            }
    
            long userId = Long.parseLong(businessActionContext.getActionContext("userId").toString());
    
            BigDecimal money = new BigDecimal(businessActionContext.getActionContext("money").toString());
    
    
            accountMapper.updateFrozenToUsed(userId,money);
            //删除标识
            ResultHolder.removeResult(getClass(),businessActionContext.getXid());
            return false;
        }
        @Transactional
        @Override
        public boolean rollback(BusinessActionContext businessActionContext) {
            log.info("扣减金额第二阶段 ,开始执行回滚操作");
            //防止重复回滚
            if (ResultHolder.getResult(getClass(),businessActionContext.getXid())==null){
                return  true;
            }
            long userId = Long.parseLong(businessActionContext.getActionContext("userId").toString());
    
            BigDecimal money = new BigDecimal(businessActionContext.getActionContext("money").toString());
    
            accountMapper.updateFrozenToResidue(userId,money);
            //删除标识
            ResultHolder.removeResult(getClass(),businessActionContext.getXid());
            log.info("扣减金额第二阶段 ,执行回滚操作完成");
            return false;
        }
    }
    
    

    在业务代码中调用 Try 阶段方法

    业务代码中调用 TCC 第一阶段方法prepareDecreaseAccount(),并添加全局事务注解 @GlobalTransactional

    package cn.tedu.account.service;
    
    import cn.tedu.account.mapper.AccountMapper;
    import cn.tedu.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);
            //调用TCC 第一阶段的方法
            accountTccAction.prepareDecreaseAccount(null,userId,money);
        }
    }
    

    启动 account 进行测试

    按顺序启动服务:

    1. Eureka
    2. Seata Server ---需要单独启动服务器
    3. Easy Id Generator
    4. Storage
    5. Account
    6. Order

    调用保存订单,地址:
    http://localhost:8083/create?userId=1&productId=1&count=10&money=100

    观察 account 的控制台日志:
    a

    查看数据库表中的账户数据:

    a

    全局事务回滚

    首先在 account 的第一阶段代码中添加模拟异常:

    AccountTccActionImplprepareDecreaseAccount 方法

    @Transactional
        @Override
        public boolean prepareDecreaseAccount(BusinessActionContext businessActionContext, Long userId, BigDecimal money) {
            log.info("扣减金额第一阶段 ,开始执行冻结金额操作");
            Account account = accountMapper.selectById(userId);
            if (account.getResidue().compareTo(money)<0){
                throw  new  RuntimeException("可用金额不足,金额冻结失败");
            }
            //执行冻结操作
            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");
            log.info("扣减金额第一阶段 ,执行冻结金额完成");
            return false;
        }
    

    重启 account 后,访问订单:
    http://localhost:8083/create?userId=1&productId=1&count=10&money=100

    查看控制台,可以看到 storage 和 order 的回滚日志,order 的回滚日志如下:

    a

  • 相关阅读:
    台达PLC开发笔记(二):台达PLC设置主机通讯参数为RTU并成功通讯
    台达PLC开发笔记(一):台达PLC连接介绍,分别使用485、网口与台达PLC建立连接
    Qt开发技术:图形视图框架(二)场景QGraphicsScene、QGraphicsItem与QGraphicsView详解
    Qt开发笔记:OpenSSL库介绍、windows上mingw32版本的OpenSSL编译模块化
    案例分享:Qt modbus485调试工具(读写Byte、Int、DInt、Real、DReal)(当前v1.3.0)
    案例分享:某品牌音响系列协议调试工具(搜寻主机,查询通道,基本控制API,云音乐API,语言节目API等,可增删改指令)
    stm32开发笔记(三):stm32系列的GPIO基本功能之输出驱动LED灯、输入按键KEY以及Demo
    stm32开发笔记(二):stm32系列使用V3.5固件库的帮助文件以及GPIO基本功能(一)
    Qt三方库开发技术(一):QuaZIP介绍、编译和使用
    NSIS制作安装包笔记(二):NSIS使用NSIS+Qt界面制作安装包流程
  • 原文地址:https://www.cnblogs.com/liqbk/p/13643790.html
Copyright © 2020-2023  润新知