说明:本记录主要是记录下自己对于公司框架集成seata的一个过程,不一定适应所有场景。

1、安装和配置 Seata 服务器

1.1、下载seata 服务

链接:https://seata.apache.org/download/seata-server
在这里插入图片描述

1.2、配置seata

解压下载的 Seata 压缩包,接口如下,进入 conf 目录:
在这里插入图片描述
编辑application.yml配置文件,主要是配置下注册中心,这里用的eureka,存储选择的是db数据库;
所有配置项conf文件夹中给了示例配置文件(application.example.yml),可以参考配置,
在这里插入图片描述
对应数据库的初始化sql在script/server/db文件中,如图:
在这里插入图片描述

1.3、启动 Seata 服务器

进入bin文件夹,执行启动脚本:

sh seata-server.sh

2、项目中集成seata

2.1、添加依赖

在 pom.xml 文件中添加 Seata 和 Spring Cloud Alibaba 的依赖。Spring Cloud Alibaba 提供了对 Seata 的集成支持。

		<dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2021.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

2.2、配置 Seata

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: basic_tx_group
  service:
    vgroup-mapping:
      basic_tx_group: "seata-server"
    grouplist:
      default: "127.0.0.1:8091"
  registry:
    type: eureka
    eureka:
      service-url: http://127.0.0.1:8080/eureka/
      application: seata-server
      weight: 1

2.3、配置数据库

确保你的数据库中有一个 undo_log 表,Seata 使用该表来存储事务回滚日志。你可以使用 Seata 提供的 SQL 脚本来创建该表。

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,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2.4、启用分布式事务

在需要开启分布式事务的方法上使用 @GlobalTransactional 注解。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐