• 本文介绍soul中对sofa的集成,方便使用sofa项目在接入soul时可以无缝衔接,sofa插件其在调用的时候,与dubbo类似,话不多说上代码

    protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
      // 前面这部分跟dubbo如出一辙,毕竟sofa跟dubbo还是有很多相似的地方的。
            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));
        }
    
  • 实际的区别还是在泛化调用的部分

    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());
            }
      // 从这部分开始与dubbo有些不一样,dubbo的异步泛化调用直接返回一个future了,sofa里面则是需要注册回调响应,通过回调响应,将结果封装装入future转出,全程异步了
            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);
        }
    
  • soul中的插件,选择器,路由器,本身就经过了很好的抽象设计,在插件部分,更是将一些核心插件单独抽离出来作为内部通用插件,对外提供的插件,都是提供给用户很灵活的配置,即使在开发过程中有自定义插件的需求,也可以继承AbstractSoulPlugin来实现,作为bean装入到spring中就可以用了,扩展性极强。

Q.E.D.