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;
}