Docker容器化部署:轻松安装与配置Seata分布式事务解决方案
Seata(Simple Extensible Autonomous Transaction Architecture)是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
文章目录
前言
Seata(Simple Extensible Autonomous Transaction Architecture)是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
一、Seata
官网地址:https://seata.apache.org/
Github地址:https://github.com/apache/incubator-seata
随着微服务架构的普及,业务系统被拆分成多个独立的服务,这些服务之间往往需要进行数据交互和事务处理。然而,传统的单体应用事务处理机制在微服务架构下不再适用,分布式事务成为了一个重要的挑战。Seata应运而生,为微服务架构下的分布式事务提供了解决方案。
Seata的目标是提供一个简单易用的分布式事务框架,帮助开发者在微服务架构下轻松实现分布式事务的管理,确保数据的一致性和可靠性。
核心组件:
- Transaction Coordinator (TC):事务协调器,负责维护全局事务的状态,协调并驱动全局事务的提交和回滚。
- Transaction Manager ™:事务管理器,负责开启全局事务,提交或回滚全局事务。
- Resource Manager (RM):资源管理器,负责管理分支事务,注册分支事务,并最终驱动分支事务提交或回滚。
事务模式:
-
AT模式(Automatic Transaction):提供无侵入自动补偿的事务模式,适用于需要强一致性的分布式事务场景。通过记录和管理各个分支事务的状态,确保全局事务的一致性。
-
TCC模式(Try-Confirm-Cancel):尝试、确认、取消模式,通过三个阶段来管理分布式事务。首先执行Try操作进行业务尝试,如果所有分支事务的Try操作都成功,则执行Confirm操作进行提交;如果有分支事务失败,则执行Cancel操作进行回滚。
-
Saga模式:基于补偿事务的模式,适用于长事务场景。每个子事务都有对应的补偿事务,当某个子事务失败时,会触发相应的补偿事务来撤销已完成的操作。
-
XA模式:基于XA协议的两阶段提交模式,是传统的分布式事务解决方案。但在微服务架构下,由于网络延迟和服务间调用的复杂性,XA模式的性能可能受到影响。
Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。我们接下来主要讲的是TC(Server端)在Docker上如何部署。
二、TC(Server)部署
1.Docker启动Seata容器
参考:https://seata.apache.org/zh-cn/docs/ops/deploy-by-docker/
命令:
docker run --name seata-server -d -p 8091:8091 -p 7091:7091 seataio/seata-server
2.创建存储配置文件目录
mkdir -p /home/docker_home/seata/seata-data
3.将容器内默认配置文件拷贝到Linux服务器上
docker cp seata-server:/seata-server/resources /home/docker_home/seata/seata-data
4.删除容器
docker rm -f seata-server
三、Seata配置到Nacos
由于我们需要使用nacos作为seata服务的配置中心和注册中心,其中,配置中心的配置,我们需要先行导入,先访问以下网站,下载config.txt文件
配置地址:https://github.com/apache/incubator-seata/tree/develop/script/config-center
config.txt内容太多,我们配置MySQL方式,所以下面内容中我们只展示MySQL配置如下:
...
#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
store.publicKey=
#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.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=username
store.db.password=password
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
...
将上述配置导入nacos,命名seata-server.properties,如下图
修改application.yml配置文件
在上面从容器中拷贝出来的resources文件夹中找到application.yml文件
根据你实际的nacos等配置信息,设置相应的application.yml配置项
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 192.168.xxx.xxx:8848
username: nacos
password: nacos
namespace: 7307bce9-3698-45c2-b721-38acea281e7b
data-id: seata-server.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 192.168.xxx.xxx:8848
username: nacos
password: nacos
namespace: b4d0832b-a7b0-44c2-8ce5-1abe676a4736
生成seata所需mysql表,地址:https://github.com/apache/incubator-seata/tree/develop/script/server/db
在MySQL数据库中执行mysql.sql脚本
- global_table:全局事务表,每当有一个全局事务发起后,就会在该表中记录全局事务的ID
- branch_table:分支事务表,记录每一个分支事务的ID,分支事务操作的哪个数据库等信息
- lock_table:全局锁
- distributed_lock:分布式锁
重新启动Docker容器:
docker run --name seata-server -d -p 8091:8091 -p 7091:7091 -v /home/docker_home/seata/seata-data/resources:/seata-server/resources seataio/seata-server
“笑对人生,智慧同行!博客新文出炉,微信订阅号更新更实时,等你笑纳~”
更多推荐
所有评论(0)