Dubbo之服务调用流程浅析

126 阅读7分钟

Dubbo调用流程解析

主要分为服务消费者和服务提供者。

1.消费者流程

结合之前说的在消费者端所有的ConsumerBean 都是proxy(代理类)

image.png

1> 消费者代理类的内部逻辑图

image-20230301162632758.png

2> InvokerInvocationHandler的invoke方法

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // 如果 这个方法属于Object
    if (method.getDeclaringClass() == Object.class) {
        // 则直接调用
        return method.invoke(invoker, args);
    }
    // 方法名
    String methodName = method.getName();
    // 入参类型
    Class<?>[] parameterTypes = method.getParameterTypes();
    // 如果是无参方法或者只有一个参数,则进行特殊判断
    if (parameterTypes.length == 0) {
        if ("toString".equals(methodName)) {
            return invoker.toString();
        } else if ("$destroy".equals(methodName)) {
            invoker.destroy();
            return null;
        } else if ("hashCode".equals(methodName)) {
            return invoker.hashCode();
        }
    } else if (parameterTypes.length == 1 && "equals".equals(methodName)) {
        return invoker.equals(args[0]);
    }
    // 根据 方法,接口名, 协议,请求参数 构造一个RpcInvocation
    RpcInvocation rpcInvocation = new RpcInvocation(method, invoker.getInterface().getName(), protocolServiceKey, args);
    // 设置接口全包名路径
    String serviceKey = invoker.getUrl().getServiceKey();
    rpcInvocation.setTargetServiceUniqueName(serviceKey);
​
    // invoker.getUrl() returns consumer url.
    RpcContext.setRpcContext(invoker.getUrl());
​
    // 代理类为空,则设置值.
    if (consumerModel != null) {
        // consumerModel
        rpcInvocation.put(Constants.CONSUMER_MODEL, consumerModel);
        // methodModel
        rpcInvocation.put(Constants.METHOD_MODEL, consumerModel.getMethodModel(method));
    }
​
    // 1.调用MigrationInvoker的invoke方法,返回 AsyncRpcResult
    // 2.调用AsyncRpcResult的recreate方法返回结果
    return invoker.invoke(rpcInvocation).recreate();
}

3> MigrationInvoker的invoke方法

public Result invoke(Invocation invocation) throws RpcException {
    // 判断serviceDiscoveryInvoker 是否可用
    if (!checkInvokerAvailable(serviceDiscoveryInvoker)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using interface addresses to handle invocation, interface " + type.getName() + ", total address size " + (invoker.getDirectory().getAllInvokers() == null ? "is null" : invoker.getDirectory().getAllInvokers().size()));
        }
        // 调用MockClusterInvoker的invoke方法
        return invoker.invoke(invocation);
    }
​
    if (!checkInvokerAvailable(invoker)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using instance addresses to handle invocation, interface " + type.getName() + ", total address size " + (serviceDiscoveryInvoker.getDirectory().getAllInvokers() == null ? " is null " : serviceDiscoveryInvoker.getDirectory().getAllInvokers().size()));
        }
        return serviceDiscoveryInvoker.invoke(invocation);
    }
​
    return currentAvailableInvoker.invoke(invocation);
}

4> MockClusterInvoker的invoke方法

public Result invoke(Invocation invocation) throws RpcException {
    Result result = null;
​
    // 判断url中mock的值
    String value = getUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim();
    if (value.length() == 0 || "false".equalsIgnoreCase(value)) {
        //no mock
        // 调用AbstractCluster$InterceptorInvokerNode的invoke方法
        result = this.invoker.invoke(invocation);
    } else if (value.startsWith("force")) {
        if (logger.isWarnEnabled()) {
            logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + getUrl());
        }
        //force:direct mock
        result = doMockInvoke(invocation, null);
    } else {
        //fail-mock
        try {
            result = this.invoker.invoke(invocation);
​
            //fix:#4585
            if(result.getException() != null && result.getException() instanceof RpcException){
                RpcException rpcException= (RpcException)result.getException();
                if(rpcException.isBiz()){
                    throw  rpcException;
                }else {
                    result = doMockInvoke(invocation, rpcException);
                }
            }
​
        } catch (RpcException e) {
            if (e.isBiz()) {
                throw e;
            }
​
            if (logger.isWarnEnabled()) {
                logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + getUrl(), e);
            }
            result = doMockInvoke(invocation, e);
        }
    }
    return result;
}

5> 调用AbstractCluster$InterceptorInvokerNode的invoke方法

public Result invoke(Invocation invocation) throws RpcException {
    Result asyncResult;
    try {
        // 设置invoker 为next,且移除serverCOntext
        interceptor.before(next, invocation);
        // FailoverClusterInvoker的invoke方法(先走AbstractClusterInvoker的invoke方法)
        asyncResult = interceptor.intercept(next, invocation);
    } catch (Exception e) {
        // onError callback
        if (interceptor instanceof ClusterInterceptor.Listener) {
            ClusterInterceptor.Listener listener = (ClusterInterceptor.Listener) interceptor;
            listener.onError(e, clusterInvoker, invocation);
        }
        throw e;
    } finally {
        interceptor.after(next, invocation);
    }
    return asyncResult.whenCompleteWithContext((r, t) -> {
        // onResponse callback
        if (interceptor instanceof ClusterInterceptor.Listener) {
            ClusterInterceptor.Listener listener = (ClusterInterceptor.Listener) interceptor;
            if (t == null) {
                listener.onMessage(r, clusterInvoker, invocation);
            } else {
                listener.onError(t, clusterInvoker, invocation);
            }
        }
    });
}

6> 调用AbstractClusterInvoker的invoke方法

public Result invoke(final Invocation invocation) throws RpcException {
    // 判断是否销毁
    checkWhetherDestroyed();
​
    // binding attachments into invocation.
    Map<String, Object> contextAttachments = RpcContext.getContext().getObjectAttachments();
    if (contextAttachments != null && contextAttachments.size() != 0) {
        ((RpcInvocation) invocation).addObjectAttachments(contextAttachments);
    }
​
    // 从注册中心获取相关的服务信息(可以参考下图)
    List<Invoker<T>> invokers = list(invocation);
    // 获取负载均衡计算规则 默认是RandomLoadBalance(根据权重随机) (具体看5.1)
    LoadBalance loadbalance = initLoadBalance(invokers, invocation);
    RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
    // 调用FailoverClusterInvoker的invoke方法
    return doInvoke(invocation, invokers, loadbalance);
}

从注册中心获取相关的服务信息

image.png

6.1> initLoadBalance方法

protected LoadBalance initLoadBalance(List<Invoker<T>> invokers, Invocation invocation) {
    if (CollectionUtils.isNotEmpty(invokers)) {
        // Dubbo的SPI选择一个负载均衡算法,默认为(random)
        return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                                                                                  .getMethodParameter(RpcUtils.getMethodName(invocation), LOADBALANCE_KEY, DEFAULT_LOADBALANCE));
    } else {
        // Dubbo的SPI选择一个负载均衡算法,默认为(random)
        return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(DEFAULT_LOADBALANCE);
    }
}

7> 调用FailoverClusterInvoker的invoke方法

public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
    List<Invoker<T>> copyInvokers = invokers;
    checkInvokers(copyInvokers, invocation);
    String methodName = RpcUtils.getMethodName(invocation);
    // 计算最大调用次数(默认为3次)
    int len = calculateInvokeTimes(methodName);
    // retry loop.
    RpcException le = null; // last exception.
    List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyInvokers.size()); // invoked invokers.
    Set<String> providers = new HashSet<String>(len);
    // 循环调用3次
    for (int i = 0; i < len; i++) {
        //Reselect before retry to avoid a change of candidate `invokers`.
        //NOTE: if `invokers` changed, then `invoked` also lose accuracy.
        // 如果重试了,则需要重新从注册中心中获得提供者列表,并校验
        if (i > 0) {
            checkWhetherDestroyed();
            copyInvokers = list(invocation);
            // check again
            checkInvokers(copyInvokers, invocation);
        }
        // 根据提供者列表和负载均衡算法,获取一个invoker(具体流程看7.1)
        Invoker<T> invoker = select(loadbalance, invocation, copyInvokers, invoked);
        invoked.add(invoker);
        RpcContext.getContext().setInvokers((List) invoked);
        try {
            // 调用RegistryDirectory$InvokerDelegate的invoke方法 -> InvokerWrapper的invoke方法
            Result result = invoker.invoke(invocation);
            if (le != null && logger.isWarnEnabled()) {
                logger.warn("Although retry the method " + methodName
                            + " in the service " + getInterface().getName()
                            + " was successful by the provider " + invoker.getUrl().getAddress()
                            + ", but there have been failed providers " + providers
                            + " (" + providers.size() + "/" + copyInvokers.size()
                            + ") from the registry " + directory.getUrl().getAddress()
                            + " on the consumer " + NetUtils.getLocalHost()
                            + " using the dubbo version " + Version.getVersion() + ". Last error is: "
                            + le.getMessage(), le);
            }
            return result;
        } catch (RpcException e) {
            if (e.isBiz()) { // biz exception.
                throw e;
            }
            le = e;
        } catch (Throwable e) {
            le = new RpcException(e.getMessage(), e);
        } finally {
            providers.add(invoker.getUrl().getAddress());
        }
    }
    throw new RpcException(le.getCode(), "Failed to invoke the method "
                           + methodName + " in the service " + getInterface().getName()
                           + ". Tried " + len + " times of the providers " + providers
                           + " (" + providers.size() + "/" + copyInvokers.size()
                           + ") from the registry " + directory.getUrl().getAddress()
                           + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version "
                           + Version.getVersion() + ". Last error is: "
                           + le.getMessage(), le.getCause() != null ? le.getCause() : le);
}

7.1> AbstractClusterInvoker的select方法

protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation,
                            List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
    // 判空
    if (CollectionUtils.isEmpty(invokers)) {
        return null;
    }
    String methodName = invocation == null ? StringUtils.EMPTY_STRING : invocation.getMethodName();
​
    // 从url中获取'sticky'的属性,缺省值为false
    boolean sticky = invokers.get(0).getUrl()
        .getMethodParameter(methodName, CLUSTER_STICKY_KEY, DEFAULT_CLUSTER_STICKY);
​
    //ignore overloaded method
    if (stickyInvoker != null && !invokers.contains(stickyInvoker)) {
        stickyInvoker = null;
    }
    
    //ignore concurrency problem
    if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))) {
        if (availablecheck && stickyInvoker.isAvailable()) {
            return stickyInvoker;
        }
    }
    
    // 真正的调用负载均衡的逻辑,选择一个invoker
    Invoker<T> invoker = doSelect(loadbalance, invocation, invokers, selected);
​
    if (sticky) {
        stickyInvoker = invoker;
    }
    return invoker;
}
​
// 整整的负载均衡的流程
private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation,
                            List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
​
    // 判空
    if (CollectionUtils.isEmpty(invokers)) {
        return null;
    }
    // 只有一个,则直接返回
    if (invokers.size() == 1) {
        return invokers.get(0);
    }
    // 调用loadbalance的select方法 选择一个invoker
    Invoker<T> invoker = loadbalance.select(invokers, getUrl(), invocation);
​
    //If the `invoker` is in the  `selected` or invoker is unavailable && availablecheck is true, reselect.
    // 如果invoker 在selected中 或 invoker 为 (不可用 && availablecheck 为true), 则重新选择。
    if ((selected != null && selected.contains(invoker))
        || (!invoker.isAvailable() && getUrl() != null && availablecheck)) {
        try {
            Invoker<T> rInvoker = reselect(loadbalance, invocation, invokers, selected, availablecheck);
            if (rInvoker != null) {
                invoker = rInvoker;
            } else {
                //Check the index of current selected invoker, if it's not the last one, choose the one at index+1.
                int index = invokers.indexOf(invoker);
                try {
                    //Avoid collision
                    invoker = invokers.get((index + 1) % invokers.size());
                } catch (Exception e) {
                    logger.warn(e.getMessage() + " may because invokers list dynamic change, ignore.", e);
                }
            }
        } catch (Throwable t) {
            logger.error("cluster reselect fail reason is :" + t.getMessage() + " if can not solve, you can set cluster.availablecheck=false in url", t);
        }
    }
    
    return invoker;
}

8> InvokerWrapper的invoke方法

public Result invoke(Invocation invocation) throws RpcException {
    // 调用FilterNode的invoke方法
    return invoker.invoke(invocation);
}

FilterNode的invoke流程

// 以下为FilterNode类
public Result invoke(Invocation invocation) throws RpcException {
    Result asyncResult;
    try {
        // 执行Filter链(ConsumerContextFilter,FutureFilter,MonitorFilter)
        asyncResult = filter.invoke(next, invocation);
    } catch (Exception e) {
        if (filter instanceof ListenableFilter) {
            ListenableFilter listenableFilter = ((ListenableFilter) filter);
            try {
                Filter.Listener listener = listenableFilter.listener(invocation);
                if (listener != null) {
                    listener.onError(e, invoker, invocation);
                }
            } finally {
                listenableFilter.removeListener(invocation);
            }
        } else if (filter instanceof Filter.Listener) {
            Filter.Listener listener = (Filter.Listener) filter;
            listener.onError(e, invoker, invocation);
        }
        throw e;
    } finally {
​
    }
    return asyncResult.whenCompleteWithContext((r, t) -> {
        if (filter instanceof ListenableFilter) {
            ListenableFilter listenableFilter = ((ListenableFilter) filter);
            Filter.Listener listener = listenableFilter.listener(invocation);
            try {
                if (listener != null) {
                    if (t == null) {
                        listener.onResponse(r, invoker, invocation);
                    } else {
                        listener.onError(t, invoker, invocation);
                    }
                }
            } finally {
                listenableFilter.removeListener(invocation);
            }
        } else if (filter instanceof Filter.Listener) {
            Filter.Listener listener = (Filter.Listener) filter;
            if (t == null) {
                listener.onResponse(r, invoker, invocation);
            } else {
                listener.onError(t, invoker, invocation);
            }
        }
    });
}

9> Filter 执行流程

image-20230302195150212.png 整体的Filter链的流程,调用顺序为: 1.ConsumerContextFilter 2.FutureFilter 3.MonitorFilter

最后执行 1.AsyncToSyncInvoker的invoke方法 2.DubboInvoker的invoke方法

Filter的流程执行:

1.ConsumerContextFilter的invoke方法

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    // Rpc的容器,赋值
    RpcContext context = RpcContext.getContext();
    context.setInvoker(invoker)
        .setInvocation(invocation)
        .setLocalAddress(NetUtils.getLocalHost(), 0)
        .setRemoteAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort())
        .setRemoteApplicationName(invoker.getUrl().getParameter(REMOTE_APPLICATION_KEY))
        .setAttachment(REMOTE_APPLICATION_KEY, invoker.getUrl().getParameter(APPLICATION_KEY));
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation) invocation).setInvoker(invoker);
    }
​
    // pass default timeout set by end user (ReferenceConfig)
    Object countDown = context.get(TIME_COUNTDOWN_KEY);
    if (countDown != null) {
        TimeoutCountDown timeoutCountDown = (TimeoutCountDown) countDown;
        if (timeoutCountDown.isExpired()) {
            return AsyncRpcResult.newDefaultAsyncResult(new RpcException(RpcException.TIMEOUT_TERMINATE,
                                                                         "No time left for making the following call: " + invocation.getServiceName() + "."
                                                                         + invocation.getMethodName() + ", terminate directly."), invocation);
        }
    }
    // 调用FutureFilter的invoke方法
    return invoker.invoke(invocation);
}

2.FutureFilter的invoke方法

public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
    fireInvokeCallback(invoker, invocation);
    // need to configure if there's return value before the invocation in order to help invoker to judge if it's
    // necessary to return future.
    // 调用MonitorFilter的invoke
    return invoker.invoke(invocation);
}
​
private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
    // 异步调用方法信息,为空则返回
    final AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
    if (asyncMethodInfo == null) {
        return;
    }
    final Method onInvokeMethod = asyncMethodInfo.getOninvokeMethod();
    final Object onInvokeInst = asyncMethodInfo.getOninvokeInstance();
​
    if (onInvokeMethod == null && onInvokeInst == null) {
        return;
    }
    if (onInvokeMethod == null || onInvokeInst == null) {
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a oninvoke callback config , but no such " + (onInvokeMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
    }
​
    ReflectUtils.makeAccessible(onInvokeMethod);
    Object[] params = invocation.getArguments();
    try {
        onInvokeMethod.invoke(onInvokeInst, params);
    } catch (InvocationTargetException e) {
        fireThrowCallback(invoker, invocation, e.getTargetException());
    } catch (Throwable e) {
        fireThrowCallback(invoker, invocation, e);
    }
}

3.MonitorFilter的invoke方法

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    // 如果URL中有minotor属性, 则invocation给赋值相关的监控信息,并计数
    if (invoker.getUrl().hasParameter(MONITOR_KEY)) {
        invocation.put(MONITOR_FILTER_START_TIME, System.currentTimeMillis());
        invocation.put(MONITOR_REMOTE_HOST_STORE, RpcContext.getContext().getRemoteHost());
        getConcurrent(invoker, invocation).incrementAndGet(); // count up
    }
    // 调用AsyncToSyncInvoker的invoke方法
    return invoker.invoke(invocation); // proceed invocation chain
}

4.AsyncToSyncInvoker的invoke方法

public Result invoke(Invocation invocation) throws RpcException {
    // 调用AbstractInvoker的invoke方法,然后再调用DubboInvoker的doInvoke方法
    Result asyncResult = invoker.invoke(invocation);
​
    try {
        if (InvokeMode.SYNC == ((RpcInvocation) invocation).getInvokeMode()) {
            /**
             * NOTICE!
             * must call {@link java.util.concurrent.CompletableFuture#get(long, TimeUnit)} because
             * {@link java.util.concurrent.CompletableFuture#get()} was proved to have serious performance drop.
             */
            asyncResult.get(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
        }
    } catch (InterruptedException e) {
        throw new RpcException("Interrupted unexpectedly while waiting for remote result to return!  method: " +
                               invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (ExecutionException e) {
        Throwable t = e.getCause();
        if (t instanceof TimeoutException) {
            throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " +
                                   invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
        } else if (t instanceof RemotingException) {
            throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " +
                                   invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
        } else {
            throw new RpcException(RpcException.UNKNOWN_EXCEPTION, "Fail to invoke remote method: " +
                                   invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
        }
    } catch (Throwable e) {
        throw new RpcException(e.getMessage(), e);
    }
    return asyncResult;
}

5.调用AbstractInvoker的invoke方法

public Result invoke(Invocation inv) throws RpcException {
    // if invoker is destroyed due to address refresh from registry, let's allow the current invoke to proceed
    if (destroyed.get()) {
        logger.warn("Invoker for service " + this + " on consumer " + NetUtils.getLocalHost() + " is destroyed, "
                    + ", dubbo version is " + Version.getVersion() + ", this invoker should not be used any longer");
    }
    // invocation 参数赋值
    RpcInvocation invocation = (RpcInvocation) inv;
    invocation.setInvoker(this);
    if (CollectionUtils.isNotEmptyMap(attachment)) {
        invocation.addObjectAttachmentsIfAbsent(attachment);
    }
​
    Map<String, Object> contextAttachments = RpcContext.getContext().getObjectAttachments();
    if (CollectionUtils.isNotEmptyMap(contextAttachments)) {
        /**
         * invocation.addAttachmentsIfAbsent(context){@link RpcInvocation#addAttachmentsIfAbsent(Map)}should not be used here,
         * because the {@link RpcContext#setAttachment(String, String)} is passed in the Filter when the call is triggered
         * by the built-in retry mechanism of the Dubbo. The attachment to update RpcContext will no longer work, which is
         * a mistake in most cases (for example, through Filter to RpcContext output traceId and spanId and other information).
         */
        invocation.addObjectAttachments(contextAttachments);
    }
​
    invocation.setInvokeMode(RpcUtils.getInvokeMode(url, invocation));
    RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
​
    // 设置序列化ID的Key
    Byte serializationId = CodecSupport.getIDByName(getUrl().getParameter(SERIALIZATION_KEY, DEFAULT_REMOTING_SERIALIZATION));
    if (serializationId != null) {
        invocation.put(SERIALIZATION_ID_KEY, serializationId);
    }
​
    AsyncRpcResult asyncResult;
    try {
        // 调用 DubboInvoker的 doInvoke方法
        asyncResult = (AsyncRpcResult) doInvoke(invocation);
    } catch (InvocationTargetException e) { // biz exception
        Throwable te = e.getTargetException();
        if (te == null) {
            asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation);
        } else {
            if (te instanceof RpcException) {
                ((RpcException) te).setCode(RpcException.BIZ_EXCEPTION);
            }
            asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, te, invocation);
        }
    } catch (RpcException e) {
        if (e.isBiz()) {
            asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation);
        } else {
            throw e;
        }
    } catch (Throwable e) {
        asyncResult = AsyncRpcResult.newDefaultAsyncResult(null, e, invocation);
    }
    RpcContext.getContext().setFuture(new FutureAdapter(asyncResult.getResponseFuture()));
    return asyncResult;
}   

6.调用DubboInvoker的doInvoke方法

protected Result doInvoke(final Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName = RpcUtils.getMethodName(invocation);
    inv.setAttachment(PATH_KEY, getUrl().getPath());
    inv.setAttachment(VERSION_KEY, version);
​
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        // 是不是oneway方式(不关心返回值)
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = calculateTimeout(invocation, methodName);
        invocation.put(TIMEOUT_KEY, timeout);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            // 调用client的send方法
            currentClient.send(inv, isSent);
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            ExecutorService executor = getCallbackExecutor(getUrl(), inv);
            // 调用client的request方法
            CompletableFuture<AppResponse> appResponseFuture =
                currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
            // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
            FutureContext.getContext().setCompatibleFuture(appResponseFuture);
            AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, inv);
            result.setExecutor(executor);
            return result;
        }
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}

2.生产者调用流程

1.DubboProtocol的reply方法

@Override
public Object reply(ExchangeChannel channel, Object message) throws RemotingException {
  if (message instanceof Invocation) {
    Invocation inv = (Invocation) message;
    // 获取Invoker (具体看1,1)
    Invoker<?> invoker = getInvoker(channel, inv);
    // need to consider backward-compatibility if it's a callback
    if (Boolean.TRUE.toString().equals(inv.getAttachments().get(IS_CALLBACK_SERVICE_INVOKE))) {
      String methodsStr = invoker.getUrl().getParameters().get("methods");
      boolean hasMethod = false;
      if (methodsStr == null || methodsStr.indexOf(",") == -1) {
        hasMethod = inv.getMethodName().equals(methodsStr);
      } else {
        String[] methods = methodsStr.split(",");
        for (String method : methods) {
          if (inv.getMethodName().equals(method)) {
            hasMethod = true;
            break;
          }
        }
      }
      if (!hasMethod) {
        logger.warn(new IllegalStateException("The methodName " + inv.getMethodName()
                                              + " not found in callback service interface ,invoke will be ignored."
                                              + " please update the api interface. url is:"
                                              + invoker.getUrl()) + " ,invocation is :" + inv);
        return null;
      }
    }
    RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress());
    return invoker.invoke(inv);
  }
  throw new RemotingException(channel, "Unsupported request: "
                              + (message == null ? null : (message.getClass().getName() + ": " + message))
                              + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
}

1.1.DubboProtocol的getInvoker方法

Invoker<?> getInvoker(Channel channel, Invocation inv) throws RemotingException {
  boolean isCallBackServiceInvoke = false;
  boolean isStubServiceInvoke = false;
  int port = channel.getLocalAddress().getPort();
  String path = inv.getAttachments().get(Constants.PATH_KEY);
  // if it's callback service on client side
  isStubServiceInvoke = Boolean.TRUE.toString().equals(inv.getAttachments().get(Constants.STUB_EVENT_KEY));
  if (isStubServiceInvoke) {
    port = channel.getRemoteAddress().getPort();
  }
  //callback
  isCallBackServiceInvoke = isClientSide(channel) && !isStubServiceInvoke;
  if (isCallBackServiceInvoke) {
    path = inv.getAttachments().get(Constants.PATH_KEY) + "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY);
    inv.getAttachments().put(IS_CALLBACK_SERVICE_INVOKE, Boolean.TRUE.toString());
  }
  // 被调用的服务全包名+端口号
  String serviceKey = serviceKey(port, path, inv.getAttachments().get(Constants.VERSION_KEY), inv.getAttachments().get(Constants.GROUP_KEY));
    
  // 根据servideKey 从exporterMap中获取暴露的服务exporter
  // Dubbo 在服务暴露时,会将服务暴露到expoterMap中,具体流程可以查看Dubbo第二讲
  DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get(serviceKey);
​
  //判空校验
  if (exporter == null)
    throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv);
​
  return exporter.getInvoker();
}

简单复习一下当前的Invoker的内部图

Bean内部流程.png 后续的流程就是Invoker的invoke调用流程

2.invoker的Filter调用流程如下:

1、EchoFilter 2、ClassLoaderFilter 3、GenericFilter 4、ContextFilter 5、TraceFilter 6、TimoutFilter 7、MonitorFilter 8、ExceptionFilter

3.执行完相关的Filter之后

3.1 DelegateProviderMetaDataInvoker的invoke方法

@Override
public Result invoke(Invocation invocation) throws RpcException {
  // invoker 为JavassistProxyFactory
  return invoker.invoke(invocation);
}

3.2 JavassistProxyFactory的Invoker

public class JavassistProxyFactory extends AbstractProxyFactory {
​
    @Override
    @SuppressWarnings("unchecked")
    public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
        return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
    }
​
    @Override
    public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
        // TODO Wrapper cannot handle this scenario correctly: the classname contains '$'
        // 通过Wrapper.getWrapper的方法 获取“helloServiceImpl的全包名”对应 wrapper
        final Wrapper wrapper = Wrapper.getWrapper(proxy.getClass().getName().indexOf('$') < 0 ? proxy.getClass() : type);
        // 构造了一个AbstractProxyInvoker 然后 生产者被调用时 会执行doInvoke方法
        return new AbstractProxyInvoker<T>(proxy, type, url) {
            @Override
            protected Object doInvoke(T proxy, String methodName,
                                      Class<?>[] parameterTypes,
                                      Object[] arguments) throws Throwable {
                // proxy 真正的被代理的Bean
                // methodName 真正的被调用的方法
                // parameterTypes 类型类型
                // argu
                return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
            }
        };
    }
​
}