hmily源码解读

mac2024-03-28  31

一阶段TRYING

入口点orderService.orderPay

orderService.orderPay     -> CglibAopProxy$DynamicAdvisedInterceptor.intercept          -> methodProxy.invoke              -> OrderServiceImpl.orderPay业务方法

本地Hmily注解方法

paymentService.makePayment      -> CglibAopProxy$DynamicAdvisedInterceptor.intercept          -> (new CglibAopProxy.CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy)).proceed()              -> DubboHmilyTransactionAspect.interceptTccMethod                  -> DubboHmilyTransactionInterceptor.interceptor                      -> hmilyTransactionAspectService.invoke                         -> txTransactionHandler.handler                                 -> point.proceed()(PaymentServiceImpl.makePayment业务try方法)                                     -> 更新事务状态为TRYING

上述txTransactionHandler是StarterHmilyTransactionHandler,handler方法主要逻辑,构建根事务,绑定参与者,关联当前线程放入ThreadLocal,持久化事务日志,创建事务上下文,关联当前线程放入ThreadLocal。

下面是rpc远程调用,这里分析以dubbo为例

accountService.payment -     > proxy0.payment          -> InvokerInvocationHandler.invoke              -> dubbo filter调用链                  -> 框架自定义调用链DubboHmilyTransactionFilter.invoke                     -> invoker.invoke(Remote服务的AccountServiceImpl.payment)                          -> DubboHmilyTransactionAspect.interceptTccMethod                              -> DubboHmilyTransactionInterceptor.interceptor                                  -> hmilyTransactionAspectService.invoke                                     -> txTransactionHandler.handler()                                          -> point.proceed()(AccountServiceImpl.payment业务方法)                                             -> 更新事务状态为TRYING

上述txTransactionHandler是ParticipantHmilyTransactionHandler,此时事务上下文的的action为try阶段1,role为start发起者,获取到的handler为ParticipantHmilyTransactionHandler,handler方法主要逻辑,构建分支事务,绑定参与者,缓存分支事务并持久化,改变事务上下文role为LOCAL,关联当前线程放入ThreadLocal。 InventoryService.decrease类似accountService.payment。 二阶段CONFIRMING

根事务参与者

hmilyTransactionExecutor.confirm,循环根事务参与者,分支事务的参与者的提交在根事务发起远程服务commit过程(根据阶段CONFIRMING状态)

-> HmilyReflector.executor          -> MethodUtils.invokeMethod             -> CglibAopProxy.intercept                 -> (new CglibAopProxy.CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy)).proceed()                 -> PaymentServiceImpl.confirmOrderStatus业务方法

AccountService.payment参与者

MethodUtils.invokeMethod      -> method.invoke(proxy0.payment)             -> InvokerInvocationHandler.invoke  // rpc开始                 -> DubboHmilyTransactionFilter.invoke                     -> DubboHmilyTransactionAspect.interceptTccMethod(Remote)                         -> DubboHmilyTransactionInterceptor.interceptor                              -> HmilyTransactionAspectServiceImpl.invoke                                 -> ParticipantHmilyTransactionHandler.handler                                      -> hmilyTransactionExecutor.confirm                                         -> MethodUtils.invokeMethod(AccountServiceImpl.confirm业务方法)

InventoryService.decrease参与者类似AccountService.payment。 这一章主要讲述了try confirm执行流程,比较粗糙,嵌套事务分析见下章。

最新回复(0)