SEATA distributed transaction practical application


       During project development, distributed transaction control was required, and Alibaba’s open source distributed transaction processing solution (SEATA) was selected. I encountered a lot of problems when using SEATA. Here they are for everyone to learn and correct.

 1. Install seata
         in Windows environment and download the seata installation package. After the installation package is downloaded, just unzip it!

     After installing seata in the Linux environment, all configurations are the same as in the Windows environment.

 2. Version selection
         During project use, I chose two SEATA versions. Both versions 1.6.1 and 1.5.2 were deployed and used. There are certain differences between the two major versions.

the difference:

  •  When using version 1.6.1, the primary key auto-increment field will be verified. When using mybatis or mybatis-plus, you need to set the auto-increment primary key in the code yourself. You cannot use the auto-increment (mysql) or sequence of the fields in the database. (pgsql).

        When version 1.5.2 is used, the primary key auto-increment field will not be verified.

  • Version 1.6.1 supports JDK17 and SpringBoot3.0.
  • After version 1.6.1, multiple registration center service exposures are supported.

 3. SEATA aims to create a one-stop distributed transaction solution and will eventually provide four transaction modes: AT, TCC, SAGA, and XA. If you are interested, you can delve into the implementation principles of the four modes.

4. Precautions during use

  • I am using AT mode, and the registration center uses NACOS. The seata configuration file is configured as shown below:
  • The nacos configuration file is as follows:

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
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
# 自定义 vgroupMapping 名称
service.vgroupMapping.common-group=default
service.vgroupMapping.ccfw-supervise-sys-group=default
service.vgroupMapping.ccfw-operator-sys-group=default

# grouplist 只适用文件模式
# service.default.grouplist=127.0.0.1:8091
# service.enableDegrade=false
# service.disableGlobalTransaction=false
# client.rm.asyncCommitBufferLimit=10000
# client.rm.lock.retryInterval=10
# client.rm.lock.retryTimes=30

# Seata 提供了一个“全局锁重试”功能,默认未开启(遇到全局锁冲突时是否回滚,默认为 true)
client.rm.lock.retryPolicyBranchRollbackOnConflict=false
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.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
# 更改db模式
store.mode=db
# store.publicKey=
# 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=postgresql
store.db.driverClassName=org.postgresql.Driver
store.db.url=jdbc:postgresql://173.18.160.70:5432/seata?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
store.db.user=root
store.db.password=Hetong_2022@SC020
store.db.minConn=5
store.db.maxConn=20
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
# store.redis.mode=single
# store.redis.single.host=127.0.0.1
# store.redis.single.port=6379
# store.redis.sentinel.masterName=
# store.redis.sentinel.sentinelHosts=
# store.redis.maxConn=10
# store.redis.minConn=1
# store.redis.maxTotal=100
# store.redis.database=0
# store.redis.password=
# store.redis.queryLimit=100
default.grouplist=173.18.160.161:8091
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
client.undo.dataValidation=true
client.undo.logSerialization=Kryo
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=true
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
  • Add transaction default grouping in nacos
  •  Introduce seata dependency into pom:
  •  Code calling method

 Transaction initiation calling method

 Sub-transaction party transaction call.

5. Distributed transaction control is one-way transaction control. The transaction initiator reports an error and the sub-transaction is rolled back. If you need to roll back the transaction after the sub-transaction reports an error, you need to make your own judgment and processing.

Guess you like

Origin blog.csdn.net/weixin_38863607/article/details/130080382