微服务-11 阿里巴巴分布式事务Seata&注册配置到nacos,XA案例&解决Feign冲突
本次涉及到的源码地址: https://gitee.com/langjunnan/nacos-parent-seata
阿里巴巴分布式事务Seata&注册配置到nacos,XA案例
前言:
在上一节中,我们讲到 事务 具有四大特性, 原子性,一致性 ,隔离性,持久性,这些特性在关系形数据库中都为我们做好了,但是也只限于单体架构中实用,如果是分布式架构就需要引入分布式事务,那么在上一节我们也介绍了 2pc 3pc(性能要求超级大) 强一致性 和 TCC(开发代码资源大)弱一致性 最终一致性 等解决方案,最终得出来得结果都是各有各得优点和缺点,但是整体我们还是不满意,本次我们引入阿里巴巴给我们提供了分布式框架 Seata,一款对代码无入侵得分布式事务框架
Seata是什么?
官网定义:是一款开源的分布式解决方案。致力于提供高性能和简单易用分布式事务服务,Seata将为用户提供了 AT(推荐 ) TCC SAGA 和 XA 事务模式 ,为用户打造一站式的分布式事务解决方案。
Seata术语
在Seata的架构中,一共有三个角色:
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190315839-2126269459.png)
TC (Transaction Coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚;
TM (Transaction Manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务;
RM (Resource Manager) - 资源管理器
其中TC为单独部署的 Server 服务端,TM和RM为嵌入到应用中的 Client 客户端;
在Seata中,一个分布式事务的生命周期如下:
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316293-201358518.png)
TM请求TC开启一个全局事务,TC会生成一个XID作为该全局事务的编号,XID会在微服务的调用链路中传播,保证将多个微服务的子事务关联在一起;
RM请求TC将本地事务注册为全局事务的分支事务,通过全局事务的XID进行关联;
TM请求TC告诉XID对应的全局事务是进行提交还是回滚;
TC驱动RM将XID对应的自己的本地事务进行提交还是回滚;
TC Server运行环境部署
我们先部署单机环境的 Seata TC Server,用于学习或测试,在生产环境中要部署集群环境;
因为TC需要进行全局事务和分支事务的记录,所以需要对应的存储,目前,TC有三种存储模式( store.mode ):
file模式:适合单机模式,全局事务会话信息在内存中读写,并持久化本地文件 root.data,性能较高;
db模式:适合集群模式,全局事务会话信息通过 db 共享,相对性能差点;
redis模式:解决db存储的性能问题;
AT模式事务案例
单体应用多数据源分布式事务
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316338-698410534.png)
AT模式 (at模式是推荐的模式,因为对代码没有入侵性 我们本次集成的也是at模式)阿里巴巴分布式事务Seate&注册配置到nacos,XA案例
需要创建数据库数据表,因为AT模式中每一个TM都会记录回滚日志信息,而且上报给TC,最终由TC来决定是否让TM进行全局事务的提交或者回滚,这是官方给的描述:
搭建Seata环境
1下载seata 官网地址 这就是下载页面 打开页面后等一会就会弹窗:https://sourceforge.net/projects/seata.mirror/files/latest/download
2解压seata压缩包到本地如下:
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316395-1722157790.png)
3配置seata的Server服务端:我们需要修改seata目录中的配置文件,让seata的服务注册到nacos中,并且在nacos的配置中心创建一个文件,配置上seata的配置信息
第一步 修改seata目录下的conf/registry.conf文件里面的信息,大致分为两部分内容, 参考如下:
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316339-1084529595.png)
copy出来的文件如下:
# 注册与发现
registry {
# type= "" seata 注册到哪里,可以选择{file 、nacos 、eureka、redis、zk、consul、etcd3、sofa}本次我们选择nacos
type = "nacos"
# nacos中的信息
nacos {
# seata 注册到nacos中的服务名称
application = "seata-server"
#nacos 的地址和端口
serverAddr = "127.0.0.1:8848"
#nacos 注册到nacos中的那个分组 默认 DEFAULT_GROUP
group = "DEFAULT_GROUP"
# nacos中的命名空间 默认public
namespace = "public"
cluster = "default"
# nacos 的登录名 和密码 如果有就配置 ,没有就算了 默认 nacos,nacos
username = "nacos"
password = "nacos"
}
}
# 配置中心 去哪里读取文件
config {
# type= "" seata 配置读去哪里读取,可以选择{file、nacos 、apollo、zk、consul、etcd3}本次我们选择nacos
type = "nacos"
nacos {
# naocs 地址 端口
serverAddr = "127.0.0.1:8848"
#默认的命名空间
namespace = "public"
# 默认分组
group = "DEFAULT_GROUP"
username = "nacos"
password = "nacos"
# nacos中创建文件的 dataId 配置文件信息
dataId = "nacos-xxxx-dev.properties"
}
}
第二步: 在nacos的配置中心 创建一个dataId为nacos-xxxx-dev.properties的文件 默认命名空间,默认分组,
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316324-1444750880.png)
这个文件需要和 conf/registry.conf 里面配置的dataId 同名 如下图:
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316134-178723034.png)
第三步 : seata在 github上提供了一些 程序启动,程序执行过程中的一些参数配置,我们都需要把这些文件配置到 远程nacos的nacos-xxxx-dev.properties 文件里,这样无论是 seata的server端启动,还是我们项目集成的client端启动,都能读取到 这些配置信息
nacos-xxxx-dev.properties 的信息具体如下: 把信息copy到 文件中 ,下一步我们会修改里面的部分信息
message=你好devdev
userLocalCache=true
#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
service.vgroupMapping.nacos-xxxx_tx_group=default
service.vgroupMapping.nacos-yyyy_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=false
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.rm.sqlParserType=druid
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=db
store.session.mode=db
#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.jdbc.Driver mysql5.*版本 使用的是8.*版本com.mysql.cj.jdbc.Driver
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=root
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.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=true
#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
第四步:我们需要修改nacos-xxxx-dev.properties文件中的一些内容
第一处内容:seata服务端存储的方式 我们选择使用db存储 并且需要在mysql中创建相关的事务存储表,需要修改的信息如下: 需要注意的是 如下标注粗体的部分:mysql链接信息需要填写你自己数据的信息,并且需要在数据中 创建名称为 seata 的数据库,而且该数据库下还要创建三张表,global_table,branch_table,lock_table
store.mode=db
store.lock.mode=db
store.session.mode=db
store.db.datasource=druid
store.db.dbType=mysql
#store.db.driverClassName=com.mysql.jdbc.Driver mysql5.*版本 使用的是8.*版本com.mysql.cj.jdbc.Driver
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=root
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
给配置的数据库 添加如下三张表: 这三张标识seata在每一个事务运行时 用的临时存储数据表
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190315839-1370706061.png)
sql:
/*
Navicat Premium Data Transfer
Source Server : tianyiyun
Source Server Type : MySQL
Source Server Version : 80017
Source Host : 61.171.3.140:3306
Source Schema : seata
Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001
Date: 06/03/2022 17:38:09
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for lock_table
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table` (
`row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`transaction_id` bigint(20) NULL DEFAULT NULL,
`branch_id` bigint(20) NOT NULL,
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gmt_create` datetime(0) NULL DEFAULT NULL,
`gmt_modified` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`row_key`) USING BTREE,
INDEX `idx_branch_id`(`branch_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
/*
Navicat Premium Data Transfer
Source Server : tianyiyun
Source Server Type : MySQL
Source Server Version : 80017
Source Host : 61.171.3.140:3306
Source Schema : seata
Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001
Date: 06/03/2022 17:36:42
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for branch_table
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table` (
`branch_id` bigint(20) NOT NULL,
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`transaction_id` bigint(20) NULL DEFAULT NULL,
`resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`status` tinyint(4) NULL DEFAULT NULL,
`client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gmt_create` datetime(6) NULL DEFAULT NULL,
`gmt_modified` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`branch_id`) USING BTREE,
INDEX `idx_xid`(`xid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
/*
Navicat Premium Data Transfer
Source Server : tianyiyun
Source Server Type : MySQL
Source Server Version : 80017
Source Host : 61.171.3.140:3306
Source Schema : seata
Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001
Date: 06/03/2022 17:36:52
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for global_table
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table` (
`xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`transaction_id` bigint(20) NULL DEFAULT NULL,
`status` tinyint(4) NOT NULL,
`application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`timeout` int(11) NULL DEFAULT NULL,
`begin_time` bigint(20) NULL DEFAULT NULL,
`application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gmt_create` datetime(0) NULL DEFAULT NULL,
`gmt_modified` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`xid`) USING BTREE,
INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
第二处内容:
微服务集成的时候在讲
第五步:
启动本地seata的server服务,切换到bin目录 启动 seata 如下图 启动成功,证明我们的配置信息都没有什么问题
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190315854-838924638.png)
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316173-560606449.png)
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190315942-439731254.png)
第六步:
Seata 服务端已经配置完整后,接下来我们在微服务中 具体集成seata分布式事务操作,
6.1 首先要引入 seata与SpringCloud集成的客户端jar包, 注意:不要乱换版本哦,我找了三个小时的bug是因为版本不对
<!--Seata依赖 rm tm-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
6.2 接下来我们需要在当前服务节点链接的数据库中创建 回滚日志表,而且每一个服务节点库只要涉及到分布式事务都必须创建该表,undo_log表是seata来管理的,
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316060-260447270.png)
undo_log表结果如下:
/*
Navicat Premium Data Transfer
Source Server : tianyiyun
Source Server Type : MySQL
Source Server Version : 80017
Source Host : 61.171.3.140:3306
Source Schema : y
Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001
Date: 06/03/2022 17:58:47
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NULL,
`log_modified` datetime(0) NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
6.3 我们在每个服务节点的resource目录下创建 application.yml 或者 bootstrap.yml 并且需要进行配置文件的添加,Seata涉及到的配置信息 如下: 分为两个部分 一个是 读取配置中心的信息,还有读取nacos高可用的注册与发现服务的信息
下面的 ${spring.application.name} 就是我们在之前配置文件中配置的 当前服务的应用程序服务名,
seata:
enabled: true
application-id: ${spring.application.name}
tx-service-group: ${spring.application.name}_tx_group #此处配置自定义的seata事务分组名称
config:
type: nacos
nacos:
serverAddr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: public
dataId: "nacos-xxxx-dev.properties"
username: "nacos"
password: "nacos"
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: public
username: "nacos"
password: "nacos"
注意: 这里注意上面标红的配置 tx-service-group: ${spring.application.name}_tx_group
,tx-service-group: 值是我们拼接出来的 ,使用当前应用服务名称作为前缀 ,而后缀则是 _tx_group , 你要注意的是 nacos配置中心也必须有一个同样的 ${spring.application.name}_tx_group 值 而 远程nacos中的${spring.application.name} 则应该是具体的 服务名称值,
我本地的spring.application.name 值是nacos-xxxx 远程nacos中的nacos-xxxx-dev.properties配置是 service.vgroupMapping.nacos-xxxx_tx_group=default
1 service.vgroupMapping. 固定不变
2 nacos-xxxx 服务名称
3 _tx_group 后缀
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316239-1469790205.png)
6.4 具体业务中使用 只需要在方法添加 @GlobalTransactional 注解 ,代表开启全局分布式事务,可以看到在一个服务内进行添加操作 并且 使用feign 调用另一个服务的添加 ,如果分布式事务成立 那么无论两个 服务中那个方法报错则这两个服务的数据库 表中都不会有数据,如果某个服务报错 另一个服务没有报错且表中插入数据信息了,那么就说明我们的分布式事务没有产生效果
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316421-135434680.png)
6.5
注意 Seata 与Feign 客户端调用的服务降级是有冲突的。因为服务降级会导致 内部消化异常,Seata就不会回滚了,所以我们需要注意的是 服务降级方法里 回滚Seata的分布式事物或者 也抛出一个异常测试
![](https://img2022.cnblogs.com/blog/975292/202203/975292-20220306190316423-303441620.png)