- Seata 是一款阿里开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案
- 这里演示学习使用SpringCloud/SpringBoot集成配置了Seata,并使用AT模式实现分布式事务回滚
- Seata GitHub
- Seata 官方文档
- Seata Demo GitHub
环境准备
- Mac OS
- Mysql8
- Eureka注册中心
GitHub 下载v1.1.0 Seata v1.1.0
- unzip seata-server-1.1.0.zip
- cd /seata/conf
- 由于seatev1.1.0默认驱动是mysql5,在seata/lib目录下,将mysql-connector-java-8.0.19.jar版本的jar包替换原来的mysq5;修改file.conf中的driverClassName = “com.mysql.cj.jdbc.Driver”
- 修改file.conf如下;修改为db模式,并且配置自己的mysql数据库链接;注意service配置中的vgroupMapping.licaibo_tx_group,其中licaibo_tx_group为自己定义,到时SpringBoot集成seata的时候需要保持一致
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "dbcp"
## mysql/oracle/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/seat-server"
user = "root"
password = "root"
minConn = 1
maxConn = 10
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
service {
#transaction service group mapping
vgroupMapping.licaibo_tx_group = "default"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support
enableDegrade = false
#disable seata
disableGlobalTransaction = false
}
}
在自己的seat-server数据库需要创建三张表,具体脚本在 Seata-mysql-script
-- -------------------------------- 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