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

修改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-28 18:04:12.125 WARN 28287 --- [erverWorker-3-1] o.a.d.remoting.transport.AbstractServer : [DUBBO] All clients has disconnected from /172.17.54.171:20881. You can graceful shutdown now., dubbo version: 2.7.8, current host: 172.17.54.171 2020-12-28 18:04:12.126 INFO 28287 --- [erverWorker-3-1] o.a.d.r.t.netty4.NettyServerHandler : [DUBBO] The connection of /172.17.54.171:53500 -> /172.17.54.171:20881 is disconnected., dubbo version: 2.7.8, current host: 172.17.54.171 2020-12-28 18:05:00.344 INFO 28287 --- [erverWorker-3-2] o.a.d.r.t.netty4.NettyServerHandler : [DUBBO] The connection of /172.17.54.171:54267 -> /172.17.54.171:20881 is established., dubbo version: 2.7.8, current host: 172.17.54.171 2020-12-28 18:05:13.544 INFO 28287 --- [:20881-thread-5] c.m.storage.provider.StorageApiImpl : commodityDTO: CommodityDTO(id=null, commodityCode=cola, name=null, count=2), actionContext: [xid:172.20.0.2:8091:86883805524140032,branch_Id:86883805897433088,action_name:decreaseStorageTcc,action_context:{action-start-time=1609149913535, sys::prepare=decreaseStoragePrepare, sys::rollback=decreaseStorageCancel, sys::commit=decreaseStorageCommit, commdityDTO=CommodityDTO(id=null, commodityCode=cola, name=null, count=2), host-name=172.17.54.171, actionName=decreaseStorageTcc}] 2020-12-28 18:05:13.557 DEBUG 28287 --- [:20881-thread-5] c.m.s.d.S.tccDecreaseStoragePrepare : ==> Preparing: update tcc_storage set count = count - ?, frozen_count = frozen_count + ? where commodity_code = ? 2020-12-28 18:05:13.558 DEBUG 28287 --- [:20881-thread-5] c.m.s.d.S.tccDecreaseStoragePrepare : ==> Parameters: 2(Integer), 2(Integer), cola(String) 2020-12-28 18:05:13.562 DEBUG 28287 --- [:20881-thread-5] c.m.s.d.S.tccDecreaseStoragePrepare : <== Updates: 1 2020-12-28 18:05:13.782 INFO 28287 --- [h_RMROLE_1_2_16] i.s.c.r.p.c.RmBranchRollbackProcessor : rm handle branch rollback process:xid=172.20.0.2:8091:86883805524140032,branchId=86883805897433088,branchType=TCC,resourceId=decreaseStorageTcc,applicationData={"actionContext":{"action-start-time":1609149913535,"sys::prepare":"decreaseStoragePrepare","sys::rollback":"decreaseStorageCancel","sys::commit":"decreaseStorageCommit","commdityDTO":{"commodityCode":"cola","count":2},"host-name":"172.17.54.171","actionName":"decreaseStorageTcc"}} 2020-12-28 18:05:13.784 INFO 28287 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacking: 172.20.0.2:8091:86883805524140032 86883805897433088 decreaseStorageTcc 2020-12-28 18:05:13.789 DEBUG 28287 --- [h_RMROLE_1_2_16] c.m.s.d.S.tccDecreaseStorageCancel : ==> Preparing: update tcc_storage set count = count + ?, frozen_count = frozen_count - ? where commodity_code = ? 2020-12-28 18:05:13.789 DEBUG 28287 --- [h_RMROLE_1_2_16] c.m.s.d.S.tccDecreaseStorageCancel : ==> Parameters: 2(Integer), 2(Integer), cola(String) 2020-12-28 18:05:13.793 DEBUG 28287 --- [h_RMROLE_1_2_16] c.m.s.d.S.tccDecreaseStorageCancel : <== Updates: 1 2020-12-28 18:05:13.815 INFO 28287 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractResourceManager : TCC resource rollback result : true, xid: 172.20.0.2:8091:86883805524140032, branchId: 86883805897433088, resourceId: decreaseStorageTcc 2020-12-28 18:05:13.815 INFO 28287 --- [h_RMROLE_1_2_16] io.seata.rm.AbstractRMHandler : Branch Rollbacked result: PhaseTwo_Rollbacked

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

到此,基于Dubbo + Seata的分布式事务已经讲解完毕。

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

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