微服务痛点-基于Dubbo + Seata的分布式事务(AT)模式 (8)

修改BusinessServiceImpl类,并重启。

private boolean flag; @Override @GlobalTransactional(timeoutMills = 300000, name = "seata-demo-business") public Boolean handleBusiness(BusinessDTO businessDTO) { flag = false; log.info("开始全局事务,XID = " + RootContext.getXID()); CommodityDTO commodityDTO = new CommodityDTO(); commodityDTO.setCommodityCode(businessDTO.getCommodityCode()); commodityDTO.setCount(businessDTO.getCount()); boolean storageResult = storageApi.decreaseStorage(commodityDTO); OrderDTO orderDTO = new OrderDTO(); orderDTO.setUserId(businessDTO.getUserId()); orderDTO.setCommodityCode(businessDTO.getCommodityCode()); orderDTO.setOrderCount(businessDTO.getCount()); orderDTO.setOrderAmount(businessDTO.getAmount()); boolean orderResult = orderApi.createOrder(orderDTO); //打开注释测试事务发生异常后,全局回滚功能 if (!flag) { throw new RuntimeException("测试抛异常后,分布式事务回滚!"); } if (!storageResult || !orderResult) { throw new RuntimeException("失败"); } return true; }

执行business模块test/java目录下的business.http文件,对接口发起请求。

POST :8084/business/buy HTTP/1.1 500 Content-Type: application/json Transfer-Encoding: chunked Date: Mon, 21 Dec 2020 08:46:24 GMT Connection: close { "timestamp": "2020-12-21T08:46:24.678+00:00", "status": 500, "error": "Internal Server Error", "message": "", "path": "/business/buy" }

各应用下二阶段回滚情况,下面日志以Storage应用为例。

2020-12-21 16:46:23.665 INFO 5123 --- [:20881-thread-6] c.m.storage.provider.StorageApiImpl : storage-全局事务,XID = 172.20.0.2:8091:84327252002611200 2020-12-21 16:46:23.670 DEBUG 5123 --- [:20881-thread-6] c.m.s.dao.StorageDao.decreaseStorage : ==> Preparing: update storage set count = count - ? where commodity_code = ? 2020-12-21 16:46:23.671 DEBUG 5123 --- [:20881-thread-6] c.m.s.dao.StorageDao.decreaseStorage : ==> Parameters: 2(Integer), cola(String) 2020-12-21 16:46:23.689 DEBUG 5123 --- [:20881-thread-6] c.m.s.dao.StorageDao.decreaseStorage : <== Updates: 1 2020-12-21 16:46:24.461 INFO 5123 --- [h_RMROLE_1_7_16] i.s.c.r.p.c.RmBranchRollbackProcessor : rm handle branch rollback process:xid=172.20.0.2:8091:84327252002611200,branchId=84327252610785280,branchType=AT,resourceId=jdbc:mysql://localhost:3306/storage,applicationData=null 2020-12-21 16:46:24.462 INFO 5123 --- [h_RMROLE_1_7_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 172.20.0.2:8091:84327252002611200 84327252610785280 jdbc:mysql://localhost:3306/storage 2020-12-21 16:46:24.580 INFO 5123 --- [h_RMROLE_1_7_16] i.s.r.d.undo.AbstractUndoLogManager : xid 172.20.0.2:8091:84327252002611200 branch 84327252610785280, undo_log deleted with GlobalFinished 2020-12-21 16:46:24.588 INFO 5123 --- [h_RMROLE_1_7_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked 2020-12-21 16:46:24.596 INFO 5123 --- [h_RMROLE_1_8_16] i.s.c.r.p.c.RmBranchRollbackProcessor : rm handle branch rollback process:xid=172.20.0.2:8091:84327252002611200,branchId=84327252556259328,branchType=AT,resourceId=jdbc:mysql://localhost:3306/storage,applicationData=null 2020-12-21 16:46:24.596 INFO 5123 --- [h_RMROLE_1_8_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 172.20.0.2:8091:84327252002611200 84327252556259328 jdbc:mysql://localhost:3306/storage 2020-12-21 16:46:24.610 INFO 5123 --- [h_RMROLE_1_8_16] i.s.r.d.undo.AbstractUndoLogManager : xid 172.20.0.2:8091:84327252002611200 branch 84327252556259328, undo_log added with GlobalFinished 2020-12-21 16:46:24.615 INFO 5123 --- [h_RMROLE_1_8_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked 2020-12-21 16:46:24.621 INFO 5123 --- [h_RMROLE_1_9_16] i.s.c.r.p.c.RmBranchRollbackProcessor : rm handle branch rollback process:xid=172.20.0.2:8091:84327252002611200,branchId=84327252489150464,branchType=AT,resourceId=jdbc:mysql://localhost:3306/storage,applicationData=null 2020-12-21 16:46:24.621 INFO 5123 --- [h_RMROLE_1_9_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 172.20.0.2:8091:84327252002611200 84327252489150464 jdbc:mysql://localhost:3306/storage 2020-12-21 16:46:24.634 INFO 5123 --- [h_RMROLE_1_9_16] i.s.r.d.undo.AbstractUndoLogManager : xid 172.20.0.2:8091:84327252002611200 branch 84327252489150464, undo_log added with GlobalFinished 2020-12-21 16:46:24.641 INFO 5123 --- [h_RMROLE_1_9_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked

大家可以观察到各数据库下的数据并没有发生变化。

以上代码,我已经上传到GitHub中了,大家详见: https://github.com/sanshengshui/seata-dubbo-action,AT模式在master分支上。

下一章将给大家介绍基于Dubbo + Seata的分布式事务 --- TCC模式的实战案例,敬请期待!

参考文章

Seata官网/AT模式:https://seata.io/zh-cn/docs/dev/mode/at-mode.html

分布式事务 Seata 及其三种模式详解/作者:屹远(陈龙:https://seata.io/zh-cn/blog/seata-at-tcc-saga.html

微服务痛点-基于Dubbo + Seata的分布式事务(AT)模式

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwgsj.html