seata的官方文檔如下
http://seata.io/zh-cn/docs/overview/what-is-seata.html
seata-server的啟動和配置
-
從官方下載seata-server安裝包https://github.com/seata/seata/releases,下載完成后解壓該安裝包
tar -xvf seata-server-1.4.2.tar.gz
-
在配置文件中修改,設置部署模式為file模式
cd seata-server-1.4.2/conf
vim file.conf配置文件如下
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "file"
## rsa decryption public key
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "file"
## rsa decryption public key
publicKey = ""
## 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)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
user = "mysql"
password = "mysql"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
## redis mode: single、sentinel
mode = "single"
## single mode property
single {
host = "127.0.0.1"
port = "6379"
}
## sentinel mode property
sentinel {
masterName = ""
## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
sentinelHosts = ""
}
password = ""
database = "0"
minConn = 1
maxConn = 10
maxTotal = 100
queryLimit = 100
}
}單機版將store.mode改為file即可,這里的db為高可用配置,需要使用mysql數據庫,這里不做說明
-
啟動服務
cd bin
nohup sh seata-server.sh &這里由於我服務器內存不夠,於是刪除了官方啟動腳本中對java虛擬機的參數設置
啟動參數如下:
Usage: sh seata-server.sh(for linux and mac) or cmd seata-server.bat(for windows) [options]
Options:
--host, -h
The host to bind.
Default: 0.0.0.0
--port, -p
The port to listen.
Default: 8091
--storeMode, -m
log store mode : file、db
Default: file
--help
e.g.
sh seata-server.sh -p 8091 -h 127.0.0.1 -m file
在springboot項目中引入seata
-
在所有需要使用seata的項目的數據庫中引入undolog日志表,建表語句如下:
CREATE TABLE `undo_log` (
`id` bigint NOT NULL AUTO_INCREMENT,
`branch_id` bigint NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int 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=6 DEFAULT CHARSET=utf8mb3
-
為項目引入seata的starter
不知道為什么直接引入seata-spring-boot-starter會發生包沖突,因此這里另外引入了seata-all來配合starter
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.0</version>
</dependency>
-
yml配置文件
#====================================Seata Config===============================================
seata:
enabled: true
client:
rm-report-success-enable: true
rm-table-meta-check-enable: false # 自動刷新緩存中的表結構(默認false)
rm-report-retry-count: 5 # 一階段結果上報TC重試次數(默認5)
rm-async-commit-buffer-limit: 10000 # 異步提交緩存隊列長度(默認10000)
rm:
lock:
lock-retry-internal: 10 # 校驗或占用全局鎖重試間隔(默認10ms)
lock-retry-times: 30 # 校驗或占用全局鎖重試次數(默認30)
lock-retry-policy-branch-rollback-on-conflict: true # 分支事務與其它全局回滾事務沖突時鎖策略(優先釋放本地鎖讓回滾成功)
tm-commit-retry-count: 3 # 一階段全局提交結果上報TC重試次數(默認1次,建議大於1)
tm-rollback-retry-count: 3 # 一階段全局回滾結果上報TC重試次數(默認1次,建議大於1)
undo:
undo-data-validation: true # 二階段回滾鏡像校驗(默認true開啟)
undo-log-serialization: jackson # undo序列化方式(默認jackson)
undo-log-table: undo_log # 自定義undo表名(默認undo_log)
support:
spring:
datasource-autoproxy: true
service:
vgroup-mapping:
my_test_tx_group: default # TC 集群(必須與seata-server保持一致)
enable-degrade: false # 降級開關
disable-global-transaction: false # 禁用全局事務(默認false)
grouplist:
default: 1.15.113.171:8091
transport:
shutdown:
wait: 3
thread-factory:
boss-thread-prefix: NettyBoss
worker-thread-prefix: NettyServerNIOWorker
server-executor-thread-prefix: NettyServerBizHandler
share-boss-worker: false
client-selector-thread-prefix: NettyClientSelector
client-selector-thread-size: 1
client-worker-thread-prefix: NettyClientWorkerThread
type: TCP
server: NIO
heartbeat: true
serialization: seata
compressor: none
enable-client-batch-send-request: true # 客戶端事務消息請求是否批量合並發送(默認true)
registry:
file:
name: file.conf
type: file
config:
file:
name: file.conf
type: file
log:
exception-rate: 100
這里需要重點關注的配置如下
-
seata.service.vgourpmapping
隨便取個名字,同一個業務中該名稱保持相同
-
seata.service.grouplist
這里用來配置連接的seata-server ip與地址,spring.cloud.alibaba.seata.tx_service_group設定該group