seata-配置分布式事务

术语描述

TC (Transaction Coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。

TM (Transaction Manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。

RM (Resource Manager) - 资源管理器
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
复制代码

seata的TC配置(seata的服务端)

rancher上配置映射卷

-w1301

文件名称 registry.conf 不可以变动

registry {
    type = "nacos"
    loadBalance = "RandomLoadBalance"
    loadBalanceVirtualNodes = 10
    nacos {
      application = "seata-server"
      serverAddr = "nacos"
      group="SEATA_GROUP"
      namespace = "seata_namespace_id"
      cluster = "default"
      username: "nacos"
      password: "nacos"
    }
}
config {
  type = "nacos"
  nacos {
    serverAddr = "nacos"
    namespace = "seata_namespace_id"
    group = "SEATA_GROUP"
    username: "nacos"
    password: "nacos"
  }
}

ps:registry 注册信息 config配置信息, 使用nacos来注册配置

复制代码

配置镜像服务

该服务要与nacos在同一个命名空间下,否则无法注册到nacos

-w1309

端口映射,服务端默认使用8091 镜像docker.io/seataio/seata-server:1.4.0

-w1217

配置变量名,值是个文件 SEATA_CONFIG_NAME = file:/root/seata-config/registry

-w1299

配置映射卷 , 卷名就是第一步配置的映射卷
-w1299

启动服务查看日志(在启动前请先配置nacos)

-w1249

配置nacos

创建seata命名空间

命名空间id需要提供给 seata的配置文件 namespace = "seata_namespace_id", 两者必须保持一致

-w1083

启动seata服务端后查看配置信息

-w1303

nacos项目启动配置

#配置分布式事物  
seata:
  enabled: true
  tx-service-group: ${spring.application.name}
  application-id: ${spring.application.name}
  registry:
    type: nacos
    nacos:
      # 和server端中application一致
      application: "seata-server"
      serverAddr: ${spring.cloud.nacos.discovery.server-addr}
      group: "SEATA_GROUP"
      namespace: "seata_namespace_id"
      cluster: "default"
      username: "nacos"
      password: "nacos"
  config:
    type: nacos
    nacos:
      serverAddr: ${spring.cloud.nacos.discovery.server-addr}
      group: "SEATA_GROUP"
      namespace: "seata_namespace_id"
      username: "nacos"
      password: "nacos"
      
ps: ${spring.cloud.nacos.discovery.server-addr} 是nacos的注册地址127.0.0.1:8848
tx-service-group: ${spring.application.name}项目启动的配置文件,需要在另外配置相关的文件
      
复制代码

-w1304

-w1007

配置数据库

CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(100) 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 = utf8 COMMENT ='AT transaction mode undo table';
复制代码

项目配置

项目pom依赖添加

 <dependencies>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>
        <!--内嵌seata1.0.0版本-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
复制代码

代码事物添加

@GlobalTransactional(rollbackFor = Exception.class)

代码示例:
 @GlobalTransactional(rollbackFor = Exception.class)
 public void add(WorkerInfo workerInfo, UserDto userDto) {
    
 }
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享