3.4 dubbo-消费方通过代理bean调用远程方法

本文详细解析了Dubbo消费者如何通过代理bean调用远程方法的过程。调用方式为异步,首先通过动态代理的demoService执行InvokerInvocationHandler.invoke(),再经由AsyncToSyncInvoker和DubboInvoker的invoke()方法,最后通过ReferenceCountExchangeClient请求目标方法。根据不同配置,currentClient可能是HeaderExchangeClient(非懒加载)或LazyConnectExchangeClient(懒加载)。
摘要由CSDN通过智能技术生成

1.4 通过代理bean调用远程方法。

  • 底层调用方式为异步调用,

demoService 是一个动态代理,在执行方法时,先执行InvokerInvocationHandler.invoke()方法,代码如下:

	//InvokerInvocationHandler.invoke()
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   
        String methodName = method.getName();
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (method.getDeclaringClass() == Object.class) {
   
        	//方法来自Object方法
            return method.invoke(invoker, args);
        }
        if ("toString".equals(methodName) && parameterTypes.length == 0) {
   
            return invoker.toString();
        }
        if ("hashCode".equals(methodName) && parameterTypes.length == 0) {
   
            return invoker.hashCode();
        }
        if ("equals".equals(methodName) && parameterTypes.length == 1) {
   
            return invoker.equals(args[0]);
        }

        return invoker.invoke(new RpcInvocation(method, args)).recreate();
    }

其中的invoker是一个包含目标类DubboInvoker的代理类AsyncToSyncInvoker的实例,AsyncToSyncInvoker的invoke()如下。
通过调用目标类获取远程方法调用结果,

	//AsyncToSyncInvoker.invoke()
    public Result invoke(Invocation invocation) throws RpcException {
   
        Result asyncResult = invoker.invoke(invocation);

        try {
   
            if (InvokeMode.SYNC == ((RpcInvocation) invocation).getInvokeMode()) {
   
                asyncResult.get(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
            }
        } catch (InterruptedException e) {
   
            throw new RpcException("Interrupted unexpectedly while waiting for remoting result to return!  method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
        } catch (ExecutionException e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值