hmily源码解读

一阶段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执行流程,比较粗糙,嵌套事务分析见下章。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值