• springcloud集成Seata AT 模式


    注意:
    1.seata版本1.4.1
    2.使用db配置,mysql
    3.nacos版本2.2.3.RELEASE
    4.spring-boot-starter-parent版本2.3.1.RELEASE
    版本匹配很关键,否则报奇奇怪怪的错!!!!

    seata库必要的表

    -- -------------------------------- The script used when storeMode is 'db' --------------------------------
    -- the table to store GlobalSession data
    CREATE TABLE IF NOT EXISTS `global_table`
    (
        `xid`                       VARCHAR(128) NOT NULL,
        `transaction_id`            BIGINT,
        `status`                    TINYINT      NOT NULL,
        `application_id`            VARCHAR(32),
        `transaction_service_group` VARCHAR(32),
        `transaction_name`          VARCHAR(128),
        `timeout`                   INT,
        `begin_time`                BIGINT,
        `application_data`          VARCHAR(2000),
        `gmt_create`                DATETIME,
        `gmt_modified`              DATETIME,
        PRIMARY KEY (`xid`),
        KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
        KEY `idx_transaction_id` (`transaction_id`)
    ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8;
    
    -- the table to store BranchSession data
    CREATE TABLE IF NOT EXISTS `branch_table`
    (
        `branch_id`         BIGINT       NOT NULL,
        `xid`               VARCHAR(128) NOT NULL,
        `transaction_id`    BIGINT,
        `resource_group_id` VARCHAR(32),
        `resource_id`       VARCHAR(256),
        `branch_type`       VARCHAR(8),
        `status`            TINYINT,
        `client_id`         VARCHAR(64),
        `application_data`  VARCHAR(2000),
        `gmt_create`        DATETIME(6),
        `gmt_modified`      DATETIME(6),
        PRIMARY KEY (`branch_id`),
        KEY `idx_xid` (`xid`)
    ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8;
    
    -- the table to store lock data
    CREATE TABLE IF NOT EXISTS `lock_table`
    (
        `row_key`        VARCHAR(128) NOT NULL,
        `xid`            VARCHAR(96),
        `transaction_id` BIGINT,
        `branch_id`      BIGINT       NOT NULL,
        `resource_id`    VARCHAR(256),
        `table_name`     VARCHAR(32),
        `pk`             VARCHAR(36),
        `gmt_create`     DATETIME,
        `gmt_modified`   DATETIME,
        PRIMARY KEY (`row_key`),
        KEY `idx_branch_id` (`branch_id`)
    ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    业务系统库必要表(每个业务库里都要添加)

    -- 注意此处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

    <dependency>
       <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-seata</artifactId>
        <version>2.2.0.RELEASE</version>
        <!-- 排除依赖,指定版本和服务端一致 -->
        <exclusions>
            <exclusion>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-spring-boot-starter</artifactId>
        <version>1.4.1</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    nacos

    <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>2.2.3.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.2.3.RELEASE</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-netflix-ribbon</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    下载必要的nacos里seata配置文件
    https://github.com/seata/seata/tree/1.4.1
    在这里插入图片描述
    配置好nacos命名空间后,自动加载seata配置
    1.找到/script/config-center/nacos/nacos-config.sh
    2.修改里面的mysql配置
    在这里插入图片描述
    3.在git bash里执行脚本(修改命名空间id,nacos账号,nacos密码)

    sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca -u username -w password
    
    • 1

    导入seata配置后效果图
    在这里插入图片描述
    下载必要的启动文件
    https://github.com/seata/seata/releases
    在这里插入图片描述
    windows启动seata
    在这里插入图片描述

    服务yml配置(服务提供方和调用方配置一样)

    seata:
      # 这里要特别注意和nacos中配置的要保持一致,建议配置成 ${spring.application.name}-tx-group
      tx-service-group: my_test_tx_group
      registry:
        type: nacos
        nacos:
          # 配置所在命名空间ID,如未配置默认public空间
          server-addr: 127.0.0.1:8848
          namespace: d173bb29-9a7c-4a01-aedd-056c63440ba8
          group: SEATA_GROUP
          application: seata-server
          userName: nacos
          password: nacos
      config:
        type: nacos
        nacos:
          server-addr: 127.0.0.1:8848
          namespace: d173bb29-9a7c-4a01-aedd-056c63440ba8
          group: SEATA_GROUP
          userName: nacos
          password: nacos
      service:
        vgroup-mapping:
          my_test_tx_group: default
        disable-global-transaction: false
      client:
        rm:
          report-success-enable: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    注意:测试时记得在sevice层加@GlobalTransactional,仅调用方添加就行。
    如果调用方没添加,被调用放添加,那么仅被调用方回滚。

  • 相关阅读:
    创建.NET程序Dump的几种姿势
    web学习笔记(六十六)项目总结
    Spring IOC - Bean的生命周期之依赖注入
    SpringBoot SpringSecurity 介绍(基于内存的验证)
    高性能 MySQL(十二):分区表
    SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器
    剑指YOLOv5改进主干RepViT系列: 最新重参数化结构|ICCV 2023 最新开源移动端网络架构 RepViT,1.3ms 延迟,速度贼快
    怎么把大视频发到微信上?关键时刻很实用!
    在Anaconda环境下创建django项目
    WebGIS系列(二):影像与切片
  • 原文地址:https://blog.csdn.net/qq_43639296/article/details/127884035