自学内容网 自学内容网

分布式事务seata(AT)与nacos整合-笔记2

本文属于b站图灵课堂springcloud笔记系列。讲的好还不要钱,值得推荐。

接上篇:分布式事务seata(AT)与nacos整合-笔记1-CSDN博客

用户下单,订单服务调用库存服务扣减库存,调用账户服务扣减账户余额
事务发起者:订单服务
事务参与者:库存服务,账户服务

工程改造:

订单服务(事务发起者)

1 pom引入seata依赖

<!-- seata 依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

2 订单服务库增加undo_log 表

CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';
ALTER TABLE `undo_log` ADD INDEX `ix_log_created` (`log_created`);

 3 订单服务application.yml 增加seata配置

seata:
  # seata 服务分组,要与服务端配置service.vgroup_mapping的后缀对应
  tx-service-group: default_tx_group
  registry:
    # 指定nacos作为注册中心
    type: nacos
    nacos:
      application: seata-server
      server-addr: tlmall-nacos-server:8848
      namespace: 
      group: SEATA_GROUP

  config:
    # 指定nacos作为配置中心
    type: nacos
    nacos:
      server-addr: tlmall-nacos-server:8848
      namespace: 
      group: SEATA_GROUP
      data-id: seataServer.properties

推荐在 Nacos配置中心创建一个dataId为seata-client.yml的配置,导入seata的配置
微服务的application.yml引入seata-client.yml

这里注意:client与server的注册、配置中namespace、group 保持一致。

4 使用一个 @GlobalTransactional 注解在下单方法上:

    @Override
    @GlobalTransactional(name="createOrder",rollbackFor=Exception.class)
    public Result<?> createOrder(String userId, String commodityCode, Integer count) {

        logger.info("[createOrder] current XID: {}", RootContext.getXID());

账务服务、库存服务前3步一样,第四步改为增加注解@Transactional

检测:

重启服务,进行下单测试。

2024-12-13T17:21:27.925+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] i.seata.tm.api.DefaultGlobalTransaction  : Begin new global transaction [192.168.192.96:8091:7566643934839149108]
2024-12-13T17:21:27.926+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] o.t.t.service.impl.OrderServiceImpl      : [createOrder] current XID: 192.168.192.96:8091:7566643934839149108
2024-12-13T17:21:29.480+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] io.seata.rm.AbstractResourceManager      : branch register success, xid:192.168.192.96:8091:7566643934839149108, branchId:7566643934839149116, lockKeys:order:61
2024-12-13T17:21:29.592+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] o.t.t.service.impl.OrderServiceImpl      : [createOrder] orderId: 61
2024-12-13T17:21:29.592+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] i.seata.tm.api.DefaultGlobalTransaction  : transaction 192.168.192.96:8091:7566643934839149108 will be commit
2024-12-13T17:21:29.729+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] i.seata.tm.api.DefaultGlobalTransaction  : transaction end, xid = 192.168.192.96:8091:7566643934839149108
2024-12-13T17:21:29.729+08:00  INFO 28792 --- [tlmall-order] [io-8030-exec-10] i.seata.tm.api.DefaultGlobalTransaction  : [192.168.192.96:8091:7566643934839149108] commit status: Committed
2024-12-13T17:21:30.393+08:00  INFO 28792 --- [tlmall-order] [h_RMROLE_1_3_16] i.s.c.r.p.c.RmBranchCommitProcessor      : rm client handle branch commit process:BranchCommitRequest{xid='192.168.192.96:8091:7566643934839149108', branchId=7566643934839149116, branchType=AT, resourceId='jdbc:mysql://rm-cn-8ly41gxcw000160o.rwlb.rds.aliyuncs.com:3306/tlmall_order', applicationData='{"skipCheckLock":true}'}
2024-12-13T17:21:30.394+08:00  INFO 28792 --- [tlmall-order] [h_RMROLE_1_3_16] io.seata.rm.AbstractRMHandler            : Branch committing: 192.168.192.96:8091:7566643934839149108 7566643934839149116 jdbc:mysql://XX.rds.aliyuncs.com:3306/tlmall_order {"skipCheckLock":true}
2024-12-13T17:21:30.394+08:00  INFO 28792 --- [tlmall-order] [h_RMROLE_1_3_16] io.seata.rm.AbstractRMHandler            : Branch commit result: PhaseTwo_Committed

成功的日志显示出:PhaseTwo_Committed

模拟扣库存成功,扣约失败,触发事务回滚的场景。看数据库的数据,还是会回滚回去。只是调用会出现异常。这是2.0的兼容性问题:

2024-12-13T17:23:26.025+08:00  INFO 28792 --- [tlmall-order] [nio-8030-exec-5] i.seata.tm.api.DefaultGlobalTransaction  : Begin new global transaction [192.168.192.96:8091:7566643934839149119]
2024-12-13T17:23:26.025+08:00  INFO 28792 --- [tlmall-order] [nio-8030-exec-5] o.t.t.service.impl.OrderServiceImpl      : [createOrder] current XID: 192.168.192.96:8091:7566643934839149119
2024-12-13T17:23:26.740+08:00  INFO 28792 --- [tlmall-order] [nio-8030-exec-5] i.seata.tm.api.DefaultGlobalTransaction  : transaction 192.168.192.96:8091:7566643934839149119 will be rollback
2024-12-13T17:23:27.167+08:00  INFO 28792 --- [tlmall-order] [nio-8030-exec-5] i.seata.tm.api.DefaultGlobalTransaction  : transaction end, xid = 192.168.192.96:8091:7566643934839149119
2024-12-13T17:23:27.168+08:00  INFO 28792 --- [tlmall-order] [nio-8030-exec-5] i.seata.tm.api.DefaultGlobalTransaction  : [192.168.192.96:8091:7566643934839149119] rollback status: Rollbacked

此处seata2.0 会吞异常,自定义异常等原始异常被重新包装转成RuntimeException无法捕获。

至此,可以验证通过seata解决分布式事务问题。

至于2.0版本选择,还是要慎重。目前推荐1.8。


原文地址:https://blog.csdn.net/bohu83/article/details/144454099

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!