Search in sources :

Example 26 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class GrpcPluginTest method testDoExecuteParaIsBlankError.

@Test
public void testDoExecuteParaIsBlankError() {
    ServerWebExchange exchange = getServerWebExchange();
    exchange.getAttributes().put(Constants.META_DATA, new MetaData());
    RuleData data = mock(RuleData.class);
    StepVerifier.create(grpcPlugin.doExecute(exchange, chain, selector, data)).expectSubscription().verifyComplete();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) RuleData(org.apache.shenyu.common.dto.RuleData) MetaData(org.apache.shenyu.common.dto.MetaData) Test(org.junit.jupiter.api.Test)

Example 27 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class TarsPlugin method doExecute.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
    String body = exchange.getAttribute(Constants.PARAM_TRANSFORM);
    ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    MetaData metaData = exchange.getAttribute(Constants.META_DATA);
    if (!checkMetaData(metaData)) {
        assert metaData != null;
        LOG.error(" path is :{}, meta data have error.... {}", shenyuContext.getPath(), metaData);
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    if (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(body)) {
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.TARS_HAVE_BODY_PARAM, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    TarsInvokePrxList tarsInvokePrxList = ApplicationConfigCache.getInstance().get(metaData.getPath());
    int index = RANDOM.nextInt(tarsInvokePrxList.getTarsInvokePrxList().size());
    Object prx = tarsInvokePrxList.getTarsInvokePrxList().get(index).getInvokePrx();
    Method method = tarsInvokePrxList.getMethod();
    CompletableFuture future;
    try {
        future = (CompletableFuture) method.invoke(prx, PrxInfoUtil.getParamArray(tarsInvokePrxList.getParamTypes(), tarsInvokePrxList.getParamNames(), body));
    } catch (Exception e) {
        LOG.error("Invoke tars error", e);
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.TARS_INVOKE, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    return Mono.fromFuture(future.thenApply(ret -> {
        if (Objects.isNull(ret)) {
            ret = Constants.TARS_RPC_RESULT_EMPTY;
        }
        exchange.getAttributes().put(Constants.RPC_RESULT, ret);
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return ret;
    })).onErrorMap(m -> new ShenyuException("failed to invoke tars")).then(chain.execute(exchange));
}
Also used : Constants(org.apache.shenyu.common.constant.Constants) PluginEnum(org.apache.shenyu.common.enums.PluginEnum) ShenyuPluginChain(org.apache.shenyu.plugin.api.ShenyuPluginChain) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) StringUtils(org.apache.commons.lang3.StringUtils) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ShenyuResultEnum(org.apache.shenyu.plugin.api.result.ShenyuResultEnum) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) ApplicationConfigCache(org.apache.shenyu.plugin.tars.cache.ApplicationConfigCache) SelectorData(org.apache.shenyu.common.dto.SelectorData) Method(java.lang.reflect.Method) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) Logger(org.slf4j.Logger) WebFluxResultUtils(org.apache.shenyu.plugin.api.utils.WebFluxResultUtils) RuleData(org.apache.shenyu.common.dto.RuleData) Mono(reactor.core.publisher.Mono) ResultEnum(org.apache.shenyu.common.enums.ResultEnum) Objects(java.util.Objects) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) HttpStatus(org.springframework.http.HttpStatus) ShenyuResultWrap(org.apache.shenyu.plugin.api.result.ShenyuResultWrap) AbstractShenyuPlugin(org.apache.shenyu.plugin.base.AbstractShenyuPlugin) PrxInfoUtil(org.apache.shenyu.plugin.tars.util.PrxInfoUtil) MetaData(org.apache.shenyu.common.dto.MetaData) CompletableFuture(java.util.concurrent.CompletableFuture) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) MetaData(org.apache.shenyu.common.dto.MetaData) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Method(java.lang.reflect.Method) ShenyuException(org.apache.shenyu.common.exception.ShenyuException)

Example 28 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApplicationConfigCache method initPrxClass.

/**
 * initPrxClass.
 *
 * @param selectorData selectorData
 */
public void initPrxClass(final SelectorData selectorData) {
    try {
        final List<TarsUpstream> upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), TarsUpstream.class);
        if (CollectionUtils.isEmpty(upstreamList)) {
            invalidate(selectorData.getName());
            return;
        }
        refreshUpstreamCache.put(selectorData.getName(), upstreamList);
        List<MetaData> metaDataList = ctxPathCache.getOrDefault(selectorData.getName(), new ArrayList<>());
        for (MetaData metaData : metaDataList) {
            refreshTarsInvokePrxList(metaData, upstreamList);
        }
    } catch (ExecutionException | NoSuchMethodException e) {
        throw new ShenyuException(e.getCause());
    }
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) ExecutionException(java.util.concurrent.ExecutionException) TarsUpstream(org.apache.shenyu.common.dto.convert.selector.TarsUpstream)

Example 29 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class SofaPluginTest method setUp.

@BeforeEach
public void setUp() {
    exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
    metaData = new MetaData();
    metaData.setId("1332017966661636096");
    metaData.setAppName("sofa");
    metaData.setPath("/sofa/findAll");
    metaData.setServiceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService");
    metaData.setMethodName("findAll");
    metaData.setRpcType(RpcTypeEnum.SOFA.getName());
    SofaProxyService sofaProxyService = mock(SofaProxyService.class);
    when(sofaProxyService.genericInvoker(null, metaData, exchange)).thenReturn(Mono.empty());
    sofaPlugin = new SofaPlugin(sofaProxyService);
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) SofaProxyService(org.apache.shenyu.plugin.sofa.proxy.SofaProxyService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class TarsMetaDataSubscriber method onSubscribe.

@Override
public void onSubscribe(final MetaData metaData) {
    metaData.updateContextPath();
    if (Objects.equals(RpcTypeEnum.TARS.getName(), metaData.getRpcType())) {
        MetaData metaExist = META_DATA.get(metaData.getPath());
        List<TarsInvokePrx> prxList = ApplicationConfigCache.getInstance().get(metaData.getPath()).getTarsInvokePrxList();
        boolean exist = prxList.stream().anyMatch(tarsInvokePrx -> tarsInvokePrx.getHost().equals(metaData.getAppName()));
        if (!exist) {
            ApplicationConfigCache.getInstance().initPrx(metaData);
        }
        if (Objects.isNull(metaExist)) {
            META_DATA.put(metaData.getPath(), metaData);
        }
    }
}
Also used : TarsInvokePrx(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrx) MetaData(org.apache.shenyu.common.dto.MetaData)

Aggregations

MetaData (org.apache.shenyu.common.dto.MetaData)70 Test (org.junit.jupiter.api.Test)31 BeforeEach (org.junit.jupiter.api.BeforeEach)14 RuleData (org.apache.shenyu.common.dto.RuleData)11 SelectorData (org.apache.shenyu.common.dto.SelectorData)10 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)9 ArrayList (java.util.ArrayList)8 MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Objects (java.util.Objects)6 ShenyuException (org.apache.shenyu.common.exception.ShenyuException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Optional (java.util.Optional)5 Constants (org.apache.shenyu.common.constant.Constants)5 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ResultEnum (org.apache.shenyu.common.enums.ResultEnum)4 RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)4 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)4