• SpringCloudAlibaba 微服务整合分布式事务Seata


    基于AT模式

    创建undo_log(回滚日志表)表, 每个数据库需要创建

    注意:UNDO_LOG Table:不同数据库在类型上会略有差别。详见官网

    -- 注意此处0.7.0+ 增加字段 context
    CREATE TABLE `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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    下载部署Seata的TC服务端(本次教程下载的版本是 1.5.1)

    • 下载地址:https://seata.io/zh-cn/blog/download.html

    • Linux/Mac 服务器安装

      • 解压
        在这里插入图片描述 - /seata-server.sh 启动,默认是8091端口(记得防火墙开放端口,也可以守护进程启动)
    • TC需要存储全局事务和分支事务的记录,支持三种存储模式

      • file模式 (默认):性能高, 适合单机模式,在内存中读写,并持久化到本地文件中

        • bin/sessionStore/root.data文件在这里插入图片描述
      • db模式 :性能较差,适合tc集群模式

      • redis模式:性能较高,适合tc集群模式

    常见问题

    seata 在 JDK11下运行报错

    解决: 下载下来的seata 默认没有存放日志文件的目录, 手动创建seata/logs/seata_gc.log 目录和文件

    SpringCloudAlibaba整合分布式事务seata

    各项目添加依赖

    出现的问题:no available service 'null' found, please make sure registry config correct
    安装的服务端版本必须要和你客户端的版本保持一样

    • 不可行导入依赖方式
    		
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-seataartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 合理的依赖方式(seata-spring-boot-starter 版本 与 部署的TC服务端版本一致
    		
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-seataartifactId>
                <exclusions>
                    <exclusion>
                        <groupId>io.seatagroupId>
                        <artifactId>seata-spring-boot-starterartifactId>
                    exclusion>
                exclusions>
            dependency>
            <dependency>
                <groupId>io.seatagroupId>
                <artifactId>seata-spring-boot-starterartifactId>
                <version>1.5.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    各微服务配置文件修改

    #seata配置
    seata:
      tx-service-group: ${spring.application.name}-group
      service:
        grouplist:
          testdemo: 127.0.0.1:8091
        vgroup-mapping:
          testdemo-user-service-group: testdemo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    启动事务

    在开始业务方法上添加 @GlobalTransactional 注解
    在这里插入图片描述

  • 相关阅读:
    Callable接口(类似于Runnable)
    数据结构之带头双向循环链表的实现
    华为OD机试真题 Java 实现【简易内存池】【2023 B卷 200分 考生抽中题】
    代码随想录 Day-44|#70 爬楼梯(进阶)|#322 零钱兑换|#279 完全平方数
    C# 图解教程 第5版 —— 第10章 语句
    sonarqube的docker安装
    JSP SSH 产品质量追溯管理统myeclipse开发mysql数据库MVC模式java编程网页设计
    【吃瓜之旅】第五章吃瓜学习
    <SQL>《SQL命令(含例句)精心整理版(4)》
    Golang Gorm 一对多 关联模式 Association + Find 查询关联
  • 原文地址:https://blog.csdn.net/laow1314/article/details/125863143