简介

  • 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.1.0-->
    2. <dependency>
    3. <groupId>com.alibaba.cloud</groupId>
    4. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    5. </dependency>
    1. version: "3.2"
    2. services:
    3. seata-server:
    4. image: seataio/seata-server:1.1.0
    5. container_name: seata-server_1.1.0
    6. ports:
    7. - 8091:8091
    8. restart: always
    9. environment:
    10. #宿主机ip
    11. - SEATA_IP=192.168.1.148
    12. - SEATA_PORT=8091
    13. - STORE_MODE=db
    14. volumes:
    15. # 注册nacos的配置文件,修改registry.conf 配置nacos相关参数
    16. - ./volume/conf/registry.conf:/seata-server/resources/registry.conf
    17. - ./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” } }

  1. <a name="mJkJi"></a>
  2. ### 集成nacos配置中心
  3. 首先我的seata依赖的版本是1.1.0,采用的是nacos充当配置中心(mysql等一些配置)和服务发现(发现seata-server)
  4. 1. 下载 config <br />`https://github.com/seata/seata/blob/develop/script/config-center/config.txt`,并修改对应的参数
  5. 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

  1. ```sh
  2. #!/bin/bash
  3. urlencode() {
  4. for ((i=0; i < ${#1}; i++))
  5. do
  6. char="${1:$i:1}"
  7. case $char in
  8. [a-zA-Z0-9.~_-]) printf $char ;;
  9. *) printf '%%%02X' "'$char" ;;
  10. esac
  11. done
  12. }
  13. host=192.168.1.148
  14. port=8848
  15. group="SEATA_GROUP"
  16. tenant="simone-server-sit"
  17. password="nacos"
  18. username="nacos"
  19. nacosAddr=$host:$port
  20. contentType="content-type:application/json;charset=UTF-8"
  21. echo " USAGE OPTION: $0 [-$host] [$port] [$group] [$tenant] [$username] [$password] "
  22. failCount=0
  23. tempLog=$(mktemp -u)
  24. function addConfig() {
  25. 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
  26. if [[ -z $(cat "${tempLog}") ]]; then
  27. echo " Please check the cluster status. "
  28. exit 1
  29. fi
  30. #如果接口返回的是true则推送成功,否则失败,开放文档地址:https://nacos.io/zh-cn/docs/open-api.html
  31. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  32. echo "Set $1=$2 successfully "
  33. else
  34. echo "Set $1=$2 failure "
  35. (( failCount++ ))
  36. fi
  37. }
  38. count=0
  39. for line in $(cat $(dirname "$PWD")/seata/config.txt | sed s/[[:space:]]//g); do
  40. (( count++ ))
  41. key=${line%%=*}
  42. value=${line#*=}
  43. addConfig "${key}" "${value}"
  44. done
  45. echo "========================================================================="
  46. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  47. 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

  1. CREATE TABLE 'undo_log'
  2. (
  3. 'id' BIGINT(20) NOT NULL AUTO_INCREMENT,
  4. 'branch_id' BIGINT(20) NOT NULL,
  5. 'xid' VARCHAR(100) NOT NULL,
  6. 'context' VARCHAR(128) NOT NULL,
  7. 'rollback_info' LONGBLOB NOT NULL,
  8. 'log_status' INT(11) NOT NULL,
  9. 'log_created' DATETIME NOT NULL,
  10. 'log_modified' DATETIME NOT NULL,
  11. 'ext' VARCHAR(100) DEFAULT NULL,
  12. PRIMARY KEY ('id'),
  13. UNIQUE KEY 'ux_undo_log' ('xid', 'branch_id')
  14. ) ENGINE = InnoDB
  15. AUTO_INCREMENT = 1
  16. DEFAULT CHARSET = utf8