ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Soul 源码分析14 sofa plugin 分析

2021-01-30 21:58:47  阅读:225  来源: 互联网

标签:return 14 exchange plugin soul 源码 final metaData


SOUL sofa plugin 分析

soul-plugin/soul-plugin-global/src/main/java/org/dromara/soul/plugin/global/GlobalPlugin.java  中使用了builder构建soulContext

@Override
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
    final ServerHttpRequest request = exchange.getRequest();
    final HttpHeaders headers = request.getHeaders();
    final String upgrade = headers.getFirst("Upgrade");
    SoulContext soulContext;
    if (StringUtils.isBlank(upgrade) || !"websocket".equals(upgrade)) {
        soulContext = builder.build(exchange);
    } else {
        final MultiValueMap<String, String> queryParams = request.getQueryParams();
        soulContext = transformMap(queryParams);
    }
    exchange.getAttributes().put(Constants.CONTEXT, soulContext);
    return chain.execute(exchange);
}

soul-plugin/soul-plugin-global/src/main/java/org/dromara/soul/plugin/global/DefaultSoulContextBuilder.java

@Override
public SoulContext build(final ServerWebExchange exchange) {
    final ServerHttpRequest request = exchange.getRequest();
    String path = request.getURI().getPath();
  //根据url拿到缓存的Metadata,供后面的插件使用
    MetaData metaData = MetaDataCache.getInstance().obtain(path);
    if (Objects.nonNull(metaData) && metaData.getEnabled()) {
        exchange.getAttributes().put(Constants.META_DATA, metaData);
    }
    return transform(request, metaData);

soul-plugin/soul-plugin-sofa/src/main/java/org/dromara/soul/plugin/sofa/param/BodyParamPlugin.java

@Override
    public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
        final ServerHttpRequest request = exchange.getRequest();
        final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
        if (Objects.nonNull(soulContext) && RpcTypeEnum.SOFA.getName().equals(soulContext.getRpcType())) {
            MediaType mediaType = request.getHeaders().getContentType();
            ServerRequest serverRequest = ServerRequest.create(exchange, messageReaders);
          //根据不同的请求类型做不同处理
            if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
                return body(exchange, serverRequest, chain);
            }
            if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) {
                return formData(exchange, serverRequest, chain);
            }
            return query(exchange, serverRequest, chain);
        }
        return chain.execute(exchange);
    }

//可以看到oder小于SOFA plugin, 这样就能保证在sofa plugin之前执行
    @Override
    public int getOrder() {
        return PluginEnum.SOFA.getCode() - 1;
    }
private Mono<Void> formData(final ServerWebExchange exchange, final ServerRequest serverRequest, final SoulPluginChain chain) {
//sofa plugin前置处理, 需要的参数放进exchange上下文中
        return serverRequest.formData()
                .switchIfEmpty(Mono.defer(() -> Mono.just(new LinkedMultiValueMap<>())))
                .flatMap(map -> {
                    exchange.getAttributes().put(Constants.SOFA_PARAMS, HttpParamConverter.toMap(() -> map));
                    return chain.execute(exchange);
                });
    }
//sofa plugin前置处理, 需要的参数放进exchange上下文中
    private Mono<Void> query(final ServerWebExchange exchange, final ServerRequest serverRequest, final SoulPluginChain chain) {
        exchange.getAttributes().put(Constants.SOFA_PARAMS,
                HttpParamConverter.ofString(() -> serverRequest.uri().getQuery()));
        return chain.execute(exchange);
    }

soul-plugin/soul-plugin-sofa/src/main/java/org/dromara/soul/plugin/sofa/SofaPlugin.java

@Override
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);
    }
  //进行sofa泛化调用
    final Mono<Object> result = sofaProxyService.genericInvoker(body, metaData, exchange);
    return result.then(chain.execute(exchange));
}

soul-plugin/soul-plugin-sofa/src/main/java/org/dromara/soul/plugin/sofa/proxy/SofaProxyService.java

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;
        }
//讲结果放入exchange如, 供后面的SofaResponsePlugin.java 处理
        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-plugin/soul-plugin-sofa/src/main/java/org/dromara/soul/plugin/sofa/response/SofaResponsePlugin.java

@Override
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
    return chain.execute(exchange).then(Mono.defer(() -> {
      //根据不同结果返回请求调用结果
        final Object result = exchange.getAttribute(Constants.SOFA_RPC_RESULT);
        if (Objects.isNull(result)) {
            Object error = SoulResultWrap.error(SoulResultEnum.SERVICE_RESULT_ERROR.getCode(), SoulResultEnum.SERVICE_RESULT_ERROR.getMsg(), null);
            return WebFluxResultUtils.result(exchange, error);
        }
        Object success = SoulResultWrap.success(SoulResultEnum.SUCCESS.getCode(), SoulResultEnum.SUCCESS.getMsg(), JsonUtils.removeClass(result));
        return WebFluxResultUtils.result(exchange, success);
    }));
}

标签:return,14,exchange,plugin,soul,源码,final,metaData
来源: https://blog.csdn.net/WincherHoo/article/details/113447261

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有