一,分布式事务解决方案之seata
1,Seata是什么
2019年1月,阿里巴巴中间件团队发起了开源项目 Fescar(Fast & EaSy Commit And Rollback),其愿景是让分布式事务的使用像本地事务的使用一样,简单和高效,并逐步解决开发者们遇到的分布式事务方面的所有难题。后来更名为 Seata,意为:Simple Extensible Autonomous Transaction Architecture,是一套分布式事务解决方案。
2,Seata的分布式事务解决方案
Seata提供了四种不同的分布式事务解决方案:
- XA模式:强一致性分阶段事务模式,牺牲了一定的可用性,无业务侵入.
- TCC模式:最终一致的分阶段事务模式,有业务侵入
- AT(auto transaction)模式:最终一致的分阶段事务模式,无业务侵入,也是Seata的默认模式.
- SAGA模式:长事务模式,有业务侵入
3,Seata的核心组件
Seata事物管理中有三个重要的核心组件:
- TC(Transaction Coordinator)-事务协调者:维护分支事务的状态,协调全局事务提交或回滚。
- TM(Transaction Manager)-事务管理器:定义全局事务的范围,并开始全局事务、提交或回滚全局事务。
- RM(Resource Manager)-资源管理器:向TC注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
二,Seata安装和启动
1,下载:
下载地址:https://github.com/seata/seata/releases
2,安装:
上传并解压安装包:
[root@localhost ~]# cd /usr/upload
[root@localhost upload]# tar -zxvf seata-server-1.4.2.tar.gz -C /usr/local
3,修改配置文件
[root@localhost bin]# vim /usr/local/seata/seata-server-1.4.2/conf/registry.conf
registry {
#tc服务的注册中心类型,这里选择nacos,也可以是eureka、zookeeper等
type = "nacos"
nacos {
# seata tc服务注册到nacos的服务名称,可以自定义
application = "seata-server"
# nacos的地址
serverAddr = "192.168.122.131:8848"
# seata服务所在分组
group = "DEFAULT_GROUP"
# seata服务所在的名称空间,这里不填就是使用默认的"public"
namespace = ""
# TC集群名,默认是"default"
cluster = "default"
# 这个是nacos的用户名
username = ""
# 这个是nacos的密码
password = ""
}
}
config {
# tc服务的配置中心类型:file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.122.131:8848"
namespace = ""
group = "DEFAULT_GROUP"
username = ""
password = ""
dataId = "seataServer.properties"
}
}
4,在Nacos中添加配置信息
配置信息地址:https://gitee.com/seata-io/seata/blob/develop/script/config-center/config.txt
# 数据存储方式,db代表数据库
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.122.128:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=xxxx
store.db.password=xxxx
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.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=98