springcloud3 Seata分布式事务解决方案以及服务搭建1
创始人
2025-05-31 12:09:29
0

一 分布式事务

1.1 分布式事务

一次业务操作需要跨多个数据源或者需要跨多个系统进行远程调用,就会产生分布式事务问题。

分布式系统环境下由不同的服务之间通过网络远程协作完成事务称之为分布式事务。如创建订单减库存事务,银行转账事务等都是分布式事务

案例:用户下订单操作,设计订单库新增订单数据,库存库减库存信息;账户库减金额操作,涉及3张数据库,2次远程调用,具有很明显的分布式事务问题。

二   seta的组件以及作用

2.1 seata的作用以及组件

1.seata是一款解决分布式事务的解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

官方文档:https://seata.io/zh-cn/index.html

2.seata的几种术语:

一个中心:全局事务id

TC(Transaction Coordinator):事务协调者。管理全局的分支事务的状态,用于全局性事务的提交和回滚。
TM(Transaction Manager):事务管理者。用于开启、提交或回滚事务。
RM(Resource Manager):资源管理器。用于分支事务上的资源管理,向 TC 注册分支事务,上报分支事务的状态,接收 TC 的命令来提交或者回滚分支事务。

3.一个典型的分布式事务过程:

TM 向 TC 申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的 XID;
XID 在微服务调用链路的上下文中传播;
RM 向 TC 注册分支事务,将其纳入 XID 对应全局事务的管辖;
TM 向 TC 发起针对 XID 的全局提交或回滚决议;
TC 调度 XID 下管辖的全部分支事务完成提交或回滚请求。

三  seta的安装

3.1 seata的安装步骤

1.下载地址:https://github.com/seata/seata/releases

这里使用版本为1.4.2 版本。

 nacos为1.4.4版本

3.1.1 启动nacos,新建命名空间和group

1.新建namespance

 2.新建group和dataid

   3.1.2  seata-server.properties配置文件的配置

去这个地址下载配置内容:  https://github.com/seata/seata/blob/1.4.2/script/config-center/config.txt

1.配置内容

#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
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
transport.serialization=seata
transport.compressor=none#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false#Transaction rule configuration, only for the client
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=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
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
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
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h#Log rule configuration, for client and server
log.exceptionRate=100#Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=db
store.lock.mode=file
store.session.mode=file
#Used for password encryption
store.publicKey=#If `store.mode,store.lock.mode,store.session.mode` are not equal to `file`, you can remove the configuration block.
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#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=cloudiip
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000#These configurations are required if the `store mode` is `redis`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `redis`, you can remove the configuration block.
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#Transaction rule configuration, only for the server
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
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

   2.内容如下: 修改地方如下,数据连接信息

3.选择配置:配置服务信息内容,将上面修改的内容复制到弹框的下面,.配置好进行发布:配置文件类型一定要选properties; 注意名称为seata-server.properties 后面配置会使用到。

1.列表,选中具体配置内容

 2.进行修改

 3.1.3 修改conf目录下file.conf

1.mode改为db,下面对应的mysql数据库连接信息,根据实际情况进行修改,如下:

 3.1.4 修改conf目录下registry.conf

1.type模块

 2.config模块

 3.1.5 附加数据库表

1.现在mysql下,新建seata数据库,然后新建4张表,如下图所示

2.脚本

CREATE TABLE `branch_table` (`branch_id` bigint(20) NOT NULL,`xid` varchar(128) NOT NULL,`transaction_id` bigint(20) DEFAULT NULL,`resource_group_id` varchar(32) DEFAULT NULL,`resource_id` varchar(256) DEFAULT NULL,`branch_type` varchar(8) DEFAULT NULL,`status` tinyint(4) DEFAULT NULL,`client_id` varchar(64) DEFAULT NULL,`application_data` varchar(2000) DEFAULT NULL,`gmt_create` datetime(6) DEFAULT NULL,`gmt_modified` datetime(6) DEFAULT NULL,PRIMARY KEY (`branch_id`),KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `distributed_lock` (`lock_key` char(20) NOT NULL,`lock_value` varchar(20) NOT NULL,`expire` bigint(20) DEFAULT NULL,PRIMARY KEY (`lock_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `global_table` (`xid` varchar(128) NOT NULL,`transaction_id` bigint(20) DEFAULT NULL,`status` tinyint(4) NOT NULL,`application_id` varchar(32) DEFAULT NULL,`transaction_service_group` varchar(32) DEFAULT NULL,`transaction_name` varchar(128) DEFAULT NULL,`timeout` int(11) DEFAULT NULL,`begin_time` bigint(20) DEFAULT NULL,`application_data` varchar(2000) DEFAULT NULL,`gmt_create` datetime DEFAULT NULL,`gmt_modified` datetime DEFAULT NULL,PRIMARY KEY (`xid`),KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `lock_table` (`row_key` varchar(128) NOT NULL,`xid` varchar(128) DEFAULT NULL,`transaction_id` bigint(20) DEFAULT NULL,`branch_id` bigint(20) NOT NULL,`resource_id` varchar(256) DEFAULT NULL,`table_name` varchar(32) DEFAULT NULL,`pk` varchar(36) DEFAULT NULL,`gmt_create` datetime DEFAULT NULL,`gmt_modified` datetime DEFAULT NULL,PRIMARY KEY (`row_key`),KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

 3.1.6   启动seata

 

查看nacos中的服务:

相关内容

热门资讯

MySQL高级篇_第19章_数... 在任何数据库环境中,总会有 不确定的意外 情况发生,比如例外的停电、计算...
“剖肝沥胆”的意思 “剖肝沥胆”的意思 成语拼音: [pōu gān lì dǎn] ...
“母老虎”的意思 “母老虎”的意思 成语拼音: [mǔ lǎo hǔ] ...
“哽哽咽咽”的意思 “哽哽咽咽”的意思 成语拼音: [gěng gěng yè yè] ...
“一渊不两蛟”的意思 “一渊不两蛟”的意思 成语拼音: [yī yuān bù liǎng jiāo] ...
LVS负载均衡与keepali... 目录 一、LVS 负载均衡的结构 LVS三种工作模式 LVS调度算法 ipvsadm工具 二、KE...
数据结构与算法——堆的基本存储 目录 一、概念及其介绍 二、适用说明 三、结构图示 四、Java 实例代码 五.堆和栈的区别 一、...
Vue.js语法详解:从入门到... Vue.js是一个流行的JavaScript框架,用于构建用户界面。它的核心特性包括数...
“江淮河汉”的意思 “江淮河汉”的意思 成语拼音: [jiāng huái hé hàn] ...
“英雄气短”的意思 “英雄气短”的意思 成语拼音: [yīng xióng qì duǎn] ...
“神龙见首不见尾”的意思 “神龙见首不见尾”的意思 成语拼音: [shén lóng jiàn shǒu bù j...
“老生常谈”的意思 “老生常谈”的意思 成语拼音: [lǎo shēng cháng tán] ...
Application 初始化... Application 的 onCreate 和 attachBaseContextApplicat...
Unity 热更新技术 | (... 🎬 博客主页:https://xiaoy.blog.csdn.net ...
01分布式电源接入对配电网影响... 说明书 MATLAB代码:分布式电源接入对配电网影响分析 关键词:分布式...
“付诸一炬”的意思 “付诸一炬”的意思 成语拼音: [fù zhū yī jù] ...
四字开头的成语 四字开头的成语四字开头的成语1  四通五达  四通八达  四停八当  四体不勤  四体不勤  四体百...
“过目成诵”的意思 “过目成诵”的意思 成语拼音: [guò mù chéng sòng] ...
“年复一年”的意思 “年复一年”的意思 成语拼音: [nián fù yī nián] ...
2023-第十四届蓝桥杯冲刺计... 💬前言 💡本文以目录形式列举大纲,可根据题目点击跳转 dz...