Sofa之Invoker/FilterInvoker

Sofa版本5.6.1

核心方法如下List-1只有invoke方法

    List-1

public interface Invoker {

    /**
     * 执行调用
     *
     * @param request 请求
     * @return SofaResponse 响应
     * @throws SofaRpcException rpc异常
     */
    SofaResponse invoke(SofaRequest request) throws SofaRpcException;
}

FilterInvoker.invoke()如下List-2

    List-2

@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
    if (nextFilter == null && invoker == null) {
        throw new SofaRpcException(RpcErrorType.SERVER_FILTER,
            LogCodes.getLog(LogCodes.ERROR_NEXT_FILTER_AND_INVOKER_NULL));
    }
    return nextFilter == null ?
        invoker.invoke(request) :
        nextFilter.invoke(invoker, request);
}

ProviderInvoker和ConsumerInvoker实现了FilterInvoke类,分别用于服务提供者端和消费者端

如下是ProviderInvoker.invoke(),通过JDK的反射调用方法,finally里面还会记录服务调用了多长时间

    List-3

public SofaResponse invoke(SofaRequest request) throws SofaRpcException {

    /*// 将接口的<sofa:param />的配置复制到RpcInternalContext
    RpcInternalContext context = RpcInternalContext.getContext();
    Map params = providerConfig.getParameters();
    if (params != null) {
        context.setAttachments(params);
    }
    // 将方法的<sofa:param />的配置复制到invocation
    String methodName = request.getMethodName();
    params = (Map) providerConfig.getMethodConfigValue(methodName, SofaConstants.CONFIG_KEY_PARAMS);
    if (params != null) {
        context.setAttachments(params);
    }*/

    SofaResponse sofaResponse = new SofaResponse();
    long startTime = RpcRuntimeContext.now();
    long bizStartTime = System.nanoTime();
    RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_INVOKE_START_TIME_NANO, System.nanoTime());
    try {
        // 反射 真正调用业务代码
        Method method = request.getMethod();
        if (method == null) {
            throw new SofaRpcException(RpcErrorType.SERVER_FILTER,
                LogCodes.getLog(LogCodes.ERROR_NEED_DECODE_METHOD));
        }
        Object result = method.invoke(providerConfig.getRef(), request.getMethodArgs());

        sofaResponse.setAppResponse(result);
    } catch (IllegalArgumentException e) { // 非法参数,可能是实现类和接口类不对应)
        sofaResponse.setErrorMsg(e.getMessage());
    } catch (IllegalAccessException e) { // 如果此 Method 对象强制执行 Java 语言访问控制,并且底层方法是不可访问的
        sofaResponse.setErrorMsg(e.getMessage());
        //        } catch (NoSuchMethodException e) { // 如果找不到匹配的方法
        //            sofaResponse.setErrorMsg(e.getMessage());
        //        } catch (ClassNotFoundException e) { // 如果指定的类加载器无法定位该类
        //            sofaResponse.setErrorMsg(e.getMessage());
    } catch (InvocationTargetException e) { // 业务代码抛出异常
        cutCause(e.getCause());
        sofaResponse.setAppResponse(e.getCause());
    } finally {
        // R8: Record business processing execution time
        if (RpcInternalContext.isAttachmentEnable()) {
            long endTime = RpcRuntimeContext.now();
            RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE,
                endTime - startTime);
        }
        RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE_NANO,
            System.nanoTime() - bizStartTime);

        RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_INVOKE_END_TIME_NANO,
            System.nanoTime());
    }

    return sofaResponse;
}

ConsumerInvoker.invoke()

如下List-4,从context中获取appName,对sofaRequest进行设置,重要的是consumerBootstrap.getCluster().sendMsg,这个后面会分析

List-4

public SofaResponse invoke(SofaRequest sofaRequest) throws SofaRpcException {
    RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CONSUMER_INVOKE_START_TIME_NANO, System.nanoTime());
    // 设置下服务器应用
    ProviderInfo providerInfo = RpcInternalContext.getContext().getProviderInfo();
    String appName = providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME);
    if (StringUtils.isNotEmpty(appName)) {
        sofaRequest.setTargetAppName(appName);
    }

    // 目前只是通过client发送给服务端
    SofaResponse sofaResponse = consumerBootstrap.getCluster().sendMsg(providerInfo, sofaRequest);
    RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CONSUMER_INVOKE_END_TIME_NANO, System.nanoTime());
    return sofaResponse;
}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章