分布式事务Seata集成案例
最后更新于:2022-04-02 07:21:27
# seata整合案例
[TOC]
## **准备环境**
1. 数据库:jeecg_order(订单数据库),jeecg_account(账户数据库),jeecg_product(商品数据库),seata(seata数据库),以上数据库脚步已存放至jeecg-cloud-test-seata示例中,文件位置如下图所示
https://gitee.com/herodotus/vuetify-form-generator.git
2. 示例代码:
jeecg-cloud-test-seata-order(下单服务)
jeecg-cloud-test-seata-product(库存服务)
jeecg-cloud-test-seata-account(账户服务)
该示例场景用于下单时检测库存是否车充足,余额是否充足,有一个不满足是则回滚事务
3. 配置中心: Nacos(1.3.0+),非必须,可使用nacos作为seata的配置中心
新建seataServer.properties配置文件内容如下
```
# 数据存储方式,db代表数据库
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://localhost:3300/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
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.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=9898
```
4. seata服务端:本次实践的seata版本为v1.4.2,下载地址 [http://seata.io/zh-cn/blog/download.html](http://seata.io/zh-cn/blog/download.html)
安装服务端
下载下来进行解压,目录结构如下:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/cb/cd/cbcd27fb8c9723d49c8e527bc914852e_722x257.png)
2. 进入bin目录启动seata,seata默认端口是8091
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/f0/9e/f09ea5dc45376fe5581b6b5a027d3eb0_814x144.png)
3. window下运行seata-server.bat
linux下运行seata-server.sh
## **测试分布式事务**
1. 修改file.conf文件(如果需要使用nacos则修改type=nacos),如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/9f/9a/9f9a09181ace41f4d8eb996d1d202878_923x512.png)
2. 修registry.conf(如果需要使用nacos则修改type=nacos),如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/21/04/2104b12bc69448e6e20e343092f55e8a_1086x700.png)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/9b/98/9b983803cfbda90901472b1f80c90837_1160x756.png)
3. 启动seata,如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/bd/83/bd833b147f7dd4228ba4d1e43e220476_849x308.png)
输入如下信息后表示启动成功
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/64/f0/64f068a3a48e7f8f755868fb9a282be3_1321x743.png)
4.微服务模块添加依赖,具体参考示例代码
~~~
org.jeecgframework.boot
jeecg-boot-starter-cloud
3.1.0
org.jeecgframework.boot
jeecg-boot-starter-seata
3.1.0
~~~
5.微服务模块添加配置,具体参考示例代码
```
seata:
enable-auto-data-source-proxy: false
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
springboot-seata-group: default
# seata 事务组编号 用于TC集群名
tx-service-group: springboot-seata-group
```
6.启动测试
依次启动
jeecg-cloud-test-seata-order(下单服务)
jeecg-cloud-test-seata-product(库存服务)
jeecg-cloud-test-seata-account(账户服务)
启动完成如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2d/2b/2d2bf2b543ab1083299f0abb8af7c233_744x193.png)
浏览器输入http://localhost:5001 即可进行测试,如下图所示
测试正常下参数见截图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2f/75/2f7529ae27d9319c3766ef93877a8c44_1077x653.png)
测试余额不足,测试库存不足,然后观察数据库订单表插入数据是否回滚
注:本示例不通过网关所以需要排除认证权限ShiroConfig.java添加如下代码
~~~
//测试模块排除
filterChainDefinitionMap.put("/test/seata/**", "anon");
~~~
';