## 前言 1. 本章以SpringBlade的blade-ops模块下的模块为例进行对接讲解 2. 其中order为订单服务,storage为库存服务,本章将以这两个服务讲解如何对接到Seata实现分布式事务 3. 打开SpringBlade工程,找到如下项目 ![](https://img.kancloud.cn/05/e2/05e2a086eff1838fcaca193c4a3709d0_774x1636.png) <br> ## 对接准备 1. 需要接入seata的服务pom.xml引入分布式事务依赖 ![](https://img.kancloud.cn/0d/fb/0dfb2150c4fbb542585a7253a8ca2084_1312x1304.png) 2. application-xx.yml加上对应seata的配置 ![](https://img.kancloud.cn/6a/a1/6aa1a1fac9e625444f7b236d4247d86e_994x1176.png) 3. 程序启动器使用seata专用的注解 ![](https://img.kancloud.cn/da/2f/da2f3d45ffb73f8aa8707b3a723e6c7f_1706x882.png) 4. 在接口的第一层请求方法加上对应的两个注解 ![](https://img.kancloud.cn/79/26/79261e25702ce94a5f0c5a6f73834960_1708x1350.png) 5. 这里以docker为例,保持seata服务开启状态 ![](https://img.kancloud.cn/0a/71/0a710907cfae26058531a8d25a78b09b_2480x1458.png) 6. 使用sql创建`seata_order`和`seata_storage`的库 ![](https://img.kancloud.cn/5a/47/5a478ae4c1198a1f435dd1dd0bb43265_672x766.png) 7. 对应数据库结果如下 ![](https://img.kancloud.cn/7f/85/7f85e68fa445d24d7dd8106e4fd88bea_318x612.png) 8. 两张表的初始数据如下 ![](https://img.kancloud.cn/e2/d8/e2d8adc163b256718a493d2eb2ac7ee9_1320x290.png) ![](https://img.kancloud.cn/ac/06/ac06c1002470cdc1c7678871929f9153_1312x270.png) <br> ## 服务启动 1. 启动blade-seata-order服务 ![](https://img.kancloud.cn/41/d2/41d28e6dfb68411933eb32d0680824fa_1382x1244.png) 2. 启动完毕后查看控制台,可以看到服务注册成功 ![](https://img.kancloud.cn/5a/7a/5a7a992cb97fb435c088fa6bd7a4e5fa_2480x1458.png) 3. 同样启动balde-seata-storage服务 ![](https://img.kancloud.cn/00/43/004386abb050693546a7ea779850e9b5_1400x1292.png) 4. 启动完毕查看控制台,服务同样注册成功 ![](https://img.kancloud.cn/de/06/de06ca249c99bc67a341afe37e8b524c_2480x1458.png) <br> ## 分布式事务测试 1. 启动核心服务,填入order接口 [POST][http://localhost/blade-seata-order/order/create] 并配置token请求头 ![](https://img.kancloud.cn/99/5b/995befb8716eab4000f284e851063ba6_1692x1576.png) 2. 先测试普通正常调用的情况,返回操作成功 ![](https://img.kancloud.cn/1d/64/1d64900d3691722f27a514ff0cddd2a3_1680x1284.png) 3. 对应数据变化如下 ![](https://img.kancloud.cn/66/64/6664e1ebdc7692859b41920042e8a1dc_1314x238.png) ![](https://img.kancloud.cn/e3/8e/e38e79ad8fc24a72a719447fbe219b46_1314x270.png) 4. 下面我们调大数值,测试模拟异常的情况,返回创建订单失败 ![](https://img.kancloud.cn/7e/80/7e803473d35c12c12d7deef2b1026b72_1692x1280.png) 5. 对应数据没有变化,那是不是回滚成功了呢?下面我们来看控制台的数据 ![](https://img.kancloud.cn/66/64/6664e1ebdc7692859b41920042e8a1dc_1314x238.png) ![](https://img.kancloud.cn/e3/8e/e38e79ad8fc24a72a719447fbe219b46_1314x270.png) ## 分布式事务验证 1. 打开blade-seata-storage的控制台,可以看到如下日志 ![](https://img.kancloud.cn/e7/6b/e76bf7914911a90710f29758e171a0e3_3500x202.png) 2. 打开docker控制台,可以看到如下日志 ![](https://img.kancloud.cn/8d/13/8d1305e9a3d75f914b0cfb7a636f7332_2480x1458.png) 3. 再结合数据库数据没有变化,说明分布式事务成功回滚 4. 为了再次验证准确性,我们重复第一次的调用,模拟订单成功 ![](https://img.kancloud.cn/cc/c8/ccc8dffac97e88ab0110e890bc6a0b79_1676x1222.png) 5. 查看数据库的数据,发现主键从2跳跃至3,说明上一个请求确实是数据入库又被回滚了 ![](https://img.kancloud.cn/42/24/4224240d9e790018ba7e5a9df80a5eb5_878x280.png) ## 后记 * seata目前已经发布至1.x版本,可以用于生产环境,但每个版本配置变化较大,希望大家跟着bladex的主分支版本来 * 测试下来感觉seata的file模式足够使用,集群可以考虑file+db模式。 * nacos模式配置使用复杂,目前版本遗留有些许问题,所以暂时不推荐对接nacos,推荐直接使用file+db模式。