# distributed-transaction-demo **Repository Path**: timfruit189/distributed-transaction-demo ## Basic Information - **Project Name**: distributed-transaction-demo - **Description**: 分布事务使用demo - seata - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2020-05-02 - **Last Updated**: 2023-03-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 分布式事务demo ## 一丶主要使用了Seata框架进行分布式事务处理 本demo使用了seata, dubbo, nacos 进行分布式事务演示, 目前使用了3种模式, AT模式, TCC模式和SAGA模式 - AT模式由Seata自动代理执行, 使用起来比较方便, 但性能不是特别好, 默认配合undo_log表使用 - 其中,TCC模式丶SAGA模式需要注意3个问题: 空回滚, 幂等性, 资源悬挂, 本demo通过添加额外的数据表解决 TCC模式使用t_[业务表]_transaction表, SAGA模式使用t_saga_transaction表 SAGA模式,还需要注意一个问题, 隔离性不好, 需要做到"宁可长款,不可短款", 即先对用户扣费, 补偿时,商家可以退款, 否则, 难以再次收费 > Seata 的定位是分布式事全场景解决方案,未来还会有 XA 模式的分布式事务实现,每种模式都有它的适用场景, AT 模式是无侵入的分布式事务解决方案,适用于不希望对业务进行改造的场景,几乎0学习成本。 TCC 模式是高性能分布式事务解决方案,适用于核心系统等对性能有很高要求的场景。 Saga 模式是长事务解决方案,适用于业务流程长且需要保证事务最终一致性的业务系统, Saga 模式一阶段就会提交本地事务,无锁,长流程情况下可以保证性能, 多用于渠道层、集成层业务系统。事务参与者可能是其它公司的服务或者是遗留系统的服务,无法进行改造和提供 TCC 要求的接口,也可以使用 Saga 模式。 [如何选择分布式事务解决方案?](https://mp.weixin.qq.com/s/2AL3uJ5BG2X3Y2Vxg0XqnQ) [分布式事务 Seata Saga 模式首秀以及三种模式详解](https://www.sofastack.tech/blog/sofa-meetup-3-seata-retrospect/) 其他框架集成seata可以查看[seata-samples](https://github.com/seata/seata-samples) ## 二丶本demo使用步骤 ![demo服务调用](doc/img/fescar-2.png) ### 1. 启动nacos注册中心 - 使用support包下的nacos-server-1.2.1.tar.gz, 解压 - 使用mysql, 创建nacos数据库 运行nacos-server解压包下conf/nacos-mysql.sql, 初始化nacos数据库 - 修改conf/application.properties中的数据源配置 ``` #*************** Config Module Related Configurations ***************# ### If user MySQL as datasource: spring.datasource.platform=mysql ### Count of DB: db.num=1 ### Connect URL of DB: db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root ``` - 运行 Nacos-server **Linux/Unix/Mac** 在nacos/bin下使用以下命令启动 ```bash sh startup.sh -m standalone ``` **Windows** ```bash cmd startup.cmd -m standalone ``` - 访问 Nacos 控制台:http://localhost:8848/nacos/index.html 若访问成功说明 Nacos-Server 服务运行成功(默认账号/密码: nacos/nacos) ### 2. 启动seata-server - 使用support包下的seata-server-1.2.0.tar.gz, 解压 - 使用mysql, 创建seata数据库 运行脚本[seata_1.2.0_.sql](seata-config-util/src/main/script/seata_1.2.0_.sql), 初始化数据库 其他类型的脚本可以在[该地址](https://github.com/seata/seata/tree/develop/script/server)找到 - 修改conf/registry.conf中配置中心为nacos ``` # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "localhost" namespace = "" cluster = "default" username = "nacos" password = "nacos" } ``` - 运行[SeataNacosConfigUtils](seata-config-util/src/main/java/com/ttx/seata/util/SeataNacosConfigUtils.java) 将seata-server的配置注册到nacos中 可以查看是否成功配置到nacos ![seata_nacos_config](doc/img/seata-nacos-config.png) - 运行 Seata-server **本demo运行命令,无需额外参数**: ``` sh seata-server.sh ``` **Linux/Unix/Mac** ```bash sh seata-server.sh -p $LISTEN_PORT -m $STORE_MODE -h $IP(此参数可选) ``` **Windows** ```bash cmd seata-server.bat -p $LISTEN_PORT -m $STORE_MODE -h $IP(此参数可选) ``` **$LISTEN_PORT**: Seata-Server 服务端口 **$STORE_MODE**: 事务操作记录存储模式:file、db **$IP(可选参数)**: 用于多 IP 环境下指定 Seata-Server 注册服务的IP 运行成功后可在 Nacos 控制台看到 服务名 =serverAddr 服务注册列表: ![seata-nacos-service](doc/img/seata-nacos-service.png) - 注意事项, seata saga模式下,需要额外创建数据库seata_saga (本demo使用了saga模式,所以需要创建初始化) 运行[脚本](seata-config-util/src/main/script/seata_saga_mysql.sql)初始化 其他类型的数据脚本可以在[这里](https://github.com/seata/seata/blob/develop/script/client/saga/)找到 ### 3. 配置各服务的数据库,并启动服务 - 创建mall_account数据库, 运行[脚本](mall-account/mall-account-application/src/main/script/mall_account.sql)初始化 修改mall-account-application服务的数据库配置, 并启动 - 创建mall_order数据库, 运行[脚本](mall-order/mall-order-application/src/main/script/mall_order.sql)初始化 修改mall-order-application服务的数据库配置, 并启动 - 创建mall_storage数据库, 运行[脚本](mall-storage/mall-storage-application/src/main/script/mall_storage.sql)初始化 修改mall-storage-application服务的数据库配置, 并启动 - 修改mall-business-application服务的数据库配置, 并启动 ### 4. 测试 访问http://127.0.0.1:8104/swagger-ui.html#/business-controller 输入以下数据进行测试 ```json { "userId":"1", "commodityCode":"C201901140001", "name":"风扇", "count":2, "amount":"100" } ```