Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。
Seata Server 部署
步骤一:下载启动包
Tags · seata/seata · GitHub
下载最新版本的release包或者tag包,解压后会看到有bin、conf、script等目录,其中script目录中存放了各种有用的脚本,如图所示 ![](https://img-blog.csdnimg.cn/ce6f223e63384d5e8ee714ae388ed23d.png)
步骤二:上传配置至Nacos配置中心
进入seata-->script-->config-center目录
![](https://img-blog.csdnimg.cn/cd77ee32ff7c4b99a62b7aa058471c74.png)
按需修改config.txt,config.txt中包含了Server端和Client端的配置,主要主要修改store.mode="db",以及数据库相关配置。
事务分组(service.vgroupMapping)可以修改,也可保持默认
更多配置可参见官方文档 Seata 参数配置
接下来,在nacos中新建一个dataId为seataServer.properties的配置项,并将修改后的config.txt的内容复制到其中,然后保存并发布
![](https://img-blog.csdnimg.cn/1e28bedd537c4afaa1c85d06cdc6c4f8.png)
![](https://img-blog.csdnimg.cn/4ff3feb2ed29430f81e816db05043026.png)
步骤三:修改配置
seata-->conf-->application.example.yml 是一个样例配置,可以参考它,从中选取需要的配置将其复制到 seata-->conf-->application.yml
这里我们主要修改以下属性:
- seata.store.mode="db"
- seata.store.db
- seata.config
- seata.registry
seata有自己的配置中心和注册中心,这里我们用nacos作为注册中心,同时也作为配置中心
由于有些配置已经在nacos中配了,所以application.yml就只保留了nacos相关配置,完整配置如下:
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
type: nacos
nacos:
server-addr: 172.16.209.85:8848
namespace:
group: SEATA_GROUP
username:
password:
data-id: seataServer.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: 172.16.209.85:8848
group: SEATA_GROUP
namespace:
cluster: default
username:
password:
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
步骤四:建表
全局事务会话信息由3块内容构成,全局事务-->分支事务-->全局锁,对应表global_table、branch_table、lock_table
注:默认数据为seata,因此可以先创建一个名为seata的数据库
sql脚本在 script-->server-->db-->mysql.sql
-- -------------------------------- 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_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- 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 = utf8mb4;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(128),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
步骤五:启动
在 Linux/Mac 下
sh ./bin/seata-server.sh
在 Windows 下
bin\seata-server.bat
支持的参数如下
- -h: 注册到注册中心的ip
- -p: Server rpc 监听端口
- -m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数
- -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
- -e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
完整命令
seata-server.sh -h 172.16.209.85 -p 8091 -m db
业务系统集成Client
步骤一:添加依赖
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
引入seata-spring-boot-starter后,默认开启自动代理数据源,并且默认初始化GlobalTransactionScanner,所以保持默认即可
注意:目前,最新版本的Seata与最新版本的Dubbo有一些兼容性问题,因此建议降低Seata版本或降低Dubbo版本,可选的组合如下:
- Seata 1.5.2 + Dubbo 2.7.18
- Seata 1.4.2 + Dubbo 3.1.1
步骤二:undo_log建表、配置参数(仅AT模式)
AT模式下,需要在各自业务数据库中加一张undo_log表,脚本在源码包中
seata/script/client/at/db at 1.5.2 · seata/seata · GitHub
注意:不同版本的表结构可能稍有不同,建议去对应版本的源码中找
-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
`branch_id` BIGINT NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(128) 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 = utf8mb4 COMMENT ='AT transaction mode undo table';
Seata 参数配置
步骤三:给事务方法加上 @GlobalTransactional 注解
代码演示
这里采用官网提供的案例
![](https://img-blog.csdnimg.cn/16bba3c6a2d7415980a2301e52140e1f.png)
项目结构如图
![](https://img-blog.csdnimg.cn/481bd217aabe4006bd71509008e72232.png)
父pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cjs.example</groupId>
<artifactId>seata-spring-boot-starter-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>seata-spring-boot-starter-samples</name>
<modules>
<module>samples-common-service</module>
<module>samples-stock-service</module>
<module>samples-order-service</module>
<module>samples-account-service</module>
<module>samples-business-service</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.7.5</spring-boot.version>
<dubbo.version>2.7.18</dubbo.version>
<seata.version>1.5.2</seata.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cjs.example</groupId>
<artifactId>seata-spring-boot-starter-samples</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.cjs.example</groupId>
<artifactId>samples-order-service</artifactId>
<version>${parent.version}</version>
<name>samples-order-service</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.cjs.example</groupId>
<artifactId>samples-common-service</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中之一application.yml
server:
port: 8083
servlet:
context-path: /order
spring:
application:
name: samples-order-service
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/order?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
dubbo:
protocol:
name: dubbo
port: 20883
registry:
address: nacos://127.0.0.1:8848
config-center:
address: nacos://127.0.0.1:8848
metadata-report:
address: nacos://127.0.0.1:8848
seata:
# tx-service-group: default_tx_group
config:
type: nacos
nacos:
server-addr: 172.16.209.85:8848
group: SEATA_GROUP
data-id: seataServer.properties
registry:
type: nacos
nacos:
server-addr: 172.16.209.85:8848
group: SEATA_GROUP
事务方法
![](https://img-blog.csdnimg.cn/0b23501c8ede4407bd8d9be3c45f20ff.png)
项目都启动后,用Postman访问
![](https://img-blog.csdnimg.cn/fc6f29987b38477cb5c0b9cb0a1717c6.png)
观察Server端日志
![](https://img-blog.csdnimg.cn/6aac751fbe604bbe981ad9d8c754eefb.png)
观察Client端日志
![](https://img-blog.csdnimg.cn/3715e2000d4d4d7db94ff10acdfc7f20.png)
![](https://img-blog.csdnimg.cn/35ecb53de2c74d7888edd93cb01c811a.png)
![](https://img-blog.csdnimg.cn/b99f702a62c34eb18e90f10029ba3b60.png)
![](https://img-blog.csdnimg.cn/707f8410dd0c4b5e871e64598caeaf92.png)
观察表数据变化
![](https://img-blog.csdnimg.cn/256caa1cf7ed4cf2ba36b1d2f9749314.png)
![](https://img-blog.csdnimg.cn/8e632002658b466b95dd996d8dc7e10f.png)
![](https://img-blog.csdnimg.cn/105628ba3e17434cb40d1c2634b113bc.png)
![](https://img-blog.csdnimg.cn/e7f22ccbea7545f986bf686ec37bc280.png)
![](https://img-blog.csdnimg.cn/23107f46fef8454ca1299cbce185d481.png)
![](https://img-blog.csdnimg.cn/74673d61b6cf4c258efb485c78b3d995.png)
回滚
![](https://img-blog.csdnimg.cn/40c80b637126459caf37608954aec83c.png)
观察Server端日志
![](https://img-blog.csdnimg.cn/c6e0c75e0635431b854eb83fa827a46a.png)
观察Client端日志
![](https://img-blog.csdnimg.cn/86be224b7ae040a6b1451eddf162eca0.png)
![](https://img-blog.csdnimg.cn/b359003e8fb942859b4c6a42651afd90.png)
![](https://img-blog.csdnimg.cn/81b27f73022341f197a8c8dfd1bc9cb1.png)
![](https://img-blog.csdnimg.cn/30be2d8b07b2449090f016d9124281cd.png)
附:
- gitee地址:seata-spring-boot-starter-samples: Seata入门案例
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)