Seata分布式事务框架示例
2023-09-27 14:25:20 时间
一、背景
阿里给出了Seata的官方示例,地址: https://github.com/seata/seata-samples, 提供了很多示例:
- springcloud-seata-sharding-jdbc-mybatis-plus-samplesEasy to understand, high availability,Integration example of Spring Cloud,Seata,ShardingJdbc,MyBatisPlus.
- spring-cloud-alibaba-samples - Spring Cloud Alibaba + Nacos + Dubbo + OpenFeign + Sentinel + Seata
- dubbo - Integration example of Seata and Apache Dubbo
- springboot - Integration example of Seata and Spring Boot
- nacos - Integration example of Seata、 Apache Dubbo and Alibaba Nacos
- springboot-dubbo-seata - Integration example of Seata、 Apache Dubbo and Spring Boot
- nutzboot-dubbo-seata - Integration example of Seata、 Apache Dubbo and NutzBoot
- springcloud-jpa-seata - Integration example of Seata and Spring Cloud and JPA
- spring-boot-multiple-datasource - Integration example of Seata and Spring Boot with multiple datasource and MyBatis
- springboot-mybatis - Integration example of Seata and Spring Boot and Mybatis
- api - Non-Spring environment uses api to build Seata distributed transactions
- spring-boot-multiple-datasource-mybatis-plus - Integration example of Seata and Spring Boot with multiple datasource and MyBatisPlus
- springcloud-nacos-seata - Integration example of Seata and Spring Cloud and Alibaba Nacos
- saga - Saga mode distributed transaction demo projects
- dubbo-multiple-datasource-mybatis-plus Integration example of Seata and Spring Boot Apache Dubbo with dynamic multiple datasource and MyBatisPlus and Alibaba Nacos
本文以springboot-mybatis为例,该示例中有5个module:
- sbm-account-service
- sbm-business-service
- sbm-common-service
- sbm-order-service
- sbm-storage-service
二、环境部署
2.1 mysql DB建表
示例中提供了mysql的建表语句,位置:seata-samples/springboot-mybatis/sql/all_in_one.sql
建了3个DB, 详情如下:
db | table1 | table2 |
db_account |
account_tbl |
undo_lob |
db_order | order_tbl | undo_lob |
db_stock | stock_tbl | undo_lob |
2.2 Seata-Server 下载
官网下载:http://seata.io/zh-cn/blog/download.html
2.3 Seata-Server 配置
/conf/application.yml, seata3个节点(config、register、store)有多重配置方式,为了简单演示,这里采用默认file方式,其他方式可以参考application.example.yml
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: file
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: file
store:
# support: file 、 db 、 redis
mode: file
2.4 Seata-Server 启动
sh /bin/seata-server.sh
启动日志:seata/logs/start.out
main] i.s.core.rpc.netty.NettyServerBootstrap : Server started, service listen port: 8091
默认启动接口:8091
三、服务启动
分别启动4个服务,启动端口分别为:
server | port |
sbm-account-service | 8083 |
sbm-order-service | 8082 |
sbm-stock-service | 8081 |
sbm-business-service | 8084 |
四、测试
测试服务位置:sbm-business-service/src/main/java/io/seata/samples/business/controller/BusinessController.java
/** * 购买下单,模拟全局事务提交 * * @return */ @RequestMapping("/purchase/commit") public Boolean purchaseCommit(HttpServletRequest request) { businessService.purchase("1001", "2001", 1); return true; } /** * 购买下单,模拟全局事务回滚 * * @return */ @RequestMapping("/purchase/rollback") public Boolean purchaseRollback() { try { businessService.purchase("1002", "2001", 1); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
4.1 正常提交
http://localhost:8084/api/business/purchase/commit
4.2 异常提交
http://localhost:8084/api/business/purchase/rollback
相关文章
- Seata AT事务底层原理分析
- 微服务架构之分布式事务
- RDIFramework.NET 框架兼容各种数据库类型事务使用范例参考
- mysql之事务
- 详解 MyBatis 事务管理,彻底颠覆你对事务的理解!
- Spring 双层事务,我抛出的异常去哪了?
- 浅析SQL Server实现分布式事务的两阶段提交协议2PC
- Spring框架10:spring编程式事务控制
- Spring框架5:事务和动态代理
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
- 数据库事务系列-MySQL跨行事务模型
- [转] 事务并发的可能问题与其解决方案
- 彻底理解事务的4个隔离级别
- Spring事务专题之四、详解Spring事务中7种传播行为
- spring成神之路第五十一篇:导致 Spring 事务失效常见的几种情况
- 对LR analysis的平均事务响应时间和summary中时间值不同的解释
- Yii2开发技巧 使用类似闭包的方式封装事务
- MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认(转)
- OceanBase由于合并操作导致事务被杀死的情况。
- Spring相关面试题:谈一谈你对事务的理解?
- SQL批处理与事务控制
- WCF技术剖析之三十一:WCF事务编程[上篇]
- 谈谈分布式事务(Distributed Transaction)[共5篇]
- 大数据必学Java基础(九十七):事务及回滚点
- “大”事务引起的锁等待分析案例
- 处理分布式事务(SpringCloud Alibaba Seata)
- Spring事务用法