主要分析 SofaPlugin 的代码
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
String body = exchange.getAttribute(Constants.SOFA_PARAMS); // 获取请求体
SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
assert soulContext != null;
MetaData metaData = exchange.getAttribute(Constants.META_DATA); // 获取元数据
if (!checkMetaData(metaData)) { // 对元数据进行检查
assert metaData != null;
log.error(" path is :{}, meta data have error.... {}", soulContext.getPath(), metaData.toString());
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
Object error = SoulResultWrap.error(SoulResultEnum.META_DATA_ERROR.getCode(), SoulResultEnum.META_DATA_ERROR.getMsg(), null);
return WebFluxResultUtils.result(exchange, error);
}
if (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(body)) { // 元数据的类型分息
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
Object error = SoulResultWrap.error(SoulResultEnum.SOFA_HAVE_BODY_PARAM.getCode(), SoulResultEnum.SOFA_HAVE_BODY_PARAM.getMsg(), null);
return WebFluxResultUtils.result(exchange, error);
}
final Mono<Object> result = sofaProxyService.genericInvoker(body, metaData, exchange); // 定义调用过程,生成远程调用
return result.then(chain.execute(exchange)); // 执行调用,走下一个插件
}
生产RPC调用客户端的代码如下
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws SoulException {
ConsumerConfig<GenericService> reference = ApplicationConfigCache.getInstance().get(metaData.getPath()); // 根据元数据的路径信息获取服务
if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterfaceId())) {
ApplicationConfigCache.getInstance().invalidate(metaData.getServiceName()); // 找不到服务重新生成服务
reference = ApplicationConfigCache.getInstance().initRef(metaData);
}
GenericService genericService = reference.refer();
Pair<String[], Object[]> pair;
if (null == body || "".equals(body) || "{}".equals(body) || "null".equals(body)) {
pair = new ImmutablePair<>(new String[]{}, new Object[]{}); // 生产调用的 空请求体
} else {
pair = sofaParamResolveService.buildParameter(body, metaData.getParameterTypes());
}
CompletableFuture<Object> future = new CompletableFuture<>();
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback<Object>() { // 对调用的结果进行封装
@Override
public void onAppResponse(final Object o, final String s, final RequestBase requestBase) {
future.complete(o); // 应用响应成功的回调
}
@Override
public void onAppException(final Throwable throwable, final String s, final RequestBase requestBase) {
future.completeExceptionally(throwable); // 应用响应异常的回调
}
@Override
public void onSofaException(final SofaRpcException e, final String s, final RequestBase requestBase) {
future.completeExceptionally(e); // 应用调用失败的回调
}
});
genericService.$genericInvoke(metaData.getMethodName(), pair.getLeft(), pair.getRight()); // 执行调用
return Mono.fromFuture(future.thenApply(ret -> {
if (Objects.isNull(ret)) {
ret = Constants.SOFA_RPC_RESULT_EMPTY;
}
GenericObject genericObject = (GenericObject) ret;
exchange.getAttributes().put(Constants.SOFA_RPC_RESULT, genericObject.getFields());
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
return ret;
})).onErrorMap(SoulException::new);
}
综上所述,Sofa的调用步骤如下:
1. 对输入参数进行判断合法性
2. 根据路径找到相关的本地服务
3. 如果没有找到对应的本地服务,则进行相应的初始化本地服务
4. 设置调用的回调,封装为CompletableFuture,留待后续在上下文中获取
5. 执行调用走下一个插件