其他分享
首页 > 其他分享> > Seata AT 事物方案

Seata AT 事物方案

作者:互联网

一、事务协调器

下面各个分模块告诉事务协调器成功与失败,来执行是否回滚。

当update语句执行时,底层会自动执行查询,修改完之后,会再次查询。新旧数据合并起来,插入到一个日志表(新数据+旧数据)

第二阶段:控制全局事务最终提交

TM 报告 事物协调器 ,事务协调器 下发RM 。RM 自行回滚 

回滚从事物的日志表,恢复到之前的数据,从日志回复完成后,删除日志表(AT事物执行,不需要再回复)。

流程

启动TC(事务协调器):Seata Server

       1.课前资料/分布式事物/seata-server-1.3.0Zip

           解压缩

       2.修改三个配置文件

             registry.conf

                  向eureka注册

               file.conf配置

           seata server运行过程中产生的日志数据,存储到什么位置(db)

         

   seate-server.bat(苹果电脑用seate-server.sh) 

   修改虚拟机使用的内寸大小(改为256)

        3.运行seate-server.bat 启动服务

                 看一下环境变量 JAVA_HOME 、PATH

                 JDK 必须用 1.8   

         命令行窗口不能关闭,窗口中的内容不能选中,应用会被选中,暂停执行。

 

添加Seata At事物

1.添加依赖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>

2.修改三个配置文件(在业务模块里面配置)

       application.yml

       设置事务组的组名

cloud:
  alibaba:
    seata:
      tx-service-group:

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

修改当前事务组对应使用哪个协调器  

 3.  新建自动配置类,创建数据源代理

 

 4.在业务方法上添加事物注解

            @transactional -- 控制本地事物

            @GlobalTransactional -- 启动全局事物(只在第一个模块添加)

                 

测试结果:

 

 

 

 

标签:方案,file,group,Seata,default,事物,server,serverAddr,seata
来源: https://blog.csdn.net/m0_60585248/article/details/121539157