简介
- TC (Transaction Coordinator) 事务协调者 :维护全局和分支事务的状态,驱动全局事务提交或回滚。
- TM (Transaction Manager) 事务管理器 :定义全局事务的范围:开始全局事务、提交或回滚全局事务。
RM (Resource Manager) 资源管理器 :管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
docker-compose方式
因为我的boot版本是2.25对应spring cloud版本是Hoxton.SR3,alibaba cloud是2.2.1.RELEASE,具体的版本对应说明可参考:地址
<!-- 当前依赖版本为:1.1.0-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
version: "3.2"
services:
seata-server:
image: seataio/seata-server:1.1.0
container_name: seata-server_1.1.0
ports:
- 8091:8091
restart: always
environment:
#宿主机ip
- SEATA_IP=192.168.1.148
- SEATA_PORT=8091
- STORE_MODE=db
volumes:
# 注册nacos的配置文件,修改registry.conf 配置nacos相关参数
- ./volume/conf/registry.conf:/seata-server/resources/registry.conf
- ./volume/logs:/root/logs/seata
```conf registry {
file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = “nacos”
nacos { application = “simone-seata” serverAddr = “192.168.1.148:8848” group = “DEFAULT_GROUP” namespace = “simone-server-sit” cluster = “default” username = “nacos” password = “nacos” } }
config { type = “nacos”
nacos { serverAddr = “192.168.1.148:8848” namespace = “simone-server-sit” group = “SEATA_GROUP” username = “nacos” password = “nacos” } }
<a name="mJkJi"></a>
### 集成nacos配置中心
首先我的seata依赖的版本是1.1.0,采用的是nacos充当配置中心(mysql等一些配置)和服务发现(发现seata-server)
1. 下载 config <br />`https://github.com/seata/seata/blob/develop/script/config-center/config.txt`,并修改对应的参数
1. 下载 sh脚本<br />`https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh`<br />本地修改config.txt 相关参数,修改nacos-config.sh 相关配置,并执行脚本,将配置推送到nacos,默认推送的是public,这里不建议手动一个个敲,因为nacos提供了对应的api接口,所以可以通过脚本推送到指定的命名空间,执行下面脚本即可
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=true
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
备注:tx_group:自定义名字,很重要,与项目配置文件的tx-service-group一一对应
service.vgroupMapping.service-simone-app-group=default
service.vgroupMapping.service-simone-user-group=default
service.vgroupMapping.service-simone-order-group=default
service.default.grouplist=192.168.1.148:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
备注:修改存储模式为数据库存储
store.mode=db
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.148:3307/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=admin
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
```sh
#!/bin/bash
urlencode() {
for ((i=0; i < ${#1}; i++))
do
char="${1:$i:1}"
case $char in
[a-zA-Z0-9.~_-]) printf $char ;;
*) printf '%%%02X' "'$char" ;;
esac
done
}
host=192.168.1.148
port=8848
group="SEATA_GROUP"
tenant="simone-server-sit"
password="nacos"
username="nacos"
nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"
echo " USAGE OPTION: $0 [-$host] [$port] [$group] [$tenant] [$username] [$password] "
failCount=0
tempLog=$(mktemp -u)
function addConfig() {
curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
if [[ -z $(cat "${tempLog}") ]]; then
echo " Please check the cluster status. "
exit 1
fi
#如果接口返回的是true则推送成功,否则失败,开放文档地址:https://nacos.io/zh-cn/docs/open-api.html
if [[ $(cat "${tempLog}") =~ "true" ]]; then
echo "Set $1=$2 successfully "
else
echo "Set $1=$2 failure "
(( failCount++ ))
fi
}
count=0
for line in $(cat $(dirname "$PWD")/seata/config.txt | sed s/[[:space:]]//g); do
(( count++ ))
key=${line%%=*}
value=${line#*=}
addConfig "${key}" "${value}"
done
echo "========================================================================="
echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
echo "========================================================================="
建立相关表
在seata数据库中新建表branch_table,global_table,lock_table,在业务数据库中新建表undo_log,用于回滚,我这里是使用java自动引入依赖时注入了undo_log脚本,seata库中sql脚本地址:https://github.com/seata/seata/blob/develop/script/server/db/mysql.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