Search in sources :

Example 1 with TarsInvokePrxList

use of org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList 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 2 with TarsInvokePrxList

use of org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList in project incubator-shenyu by apache.

the class ApplicationConfigCache method refreshTarsInvokePrxList.

/**
 * refresh metaData path upstream url.
 *
 * @param metaData     metaData
 * @param upstreamList upstream list
 */
private void refreshTarsInvokePrxList(final MetaData metaData, final List<TarsUpstream> upstreamList) throws NoSuchMethodException, ExecutionException {
    Class<?> prxClass = prxClassCache.get(metaData.getPath());
    if (Objects.isNull(prxClass)) {
        return;
    }
    TarsInvokePrxList tarsInvokePrxList = cache.get(metaData.getPath());
    tarsInvokePrxList.getTarsInvokePrxList().clear();
    if (Objects.isNull(tarsInvokePrxList.getMethod())) {
        TarsParamInfo tarsParamInfo = prxParamCache.get(getClassMethodKey(prxClass.getName(), metaData.getMethodName()));
        Object prx = communicator.stringToProxy(prxClass, PrxInfoUtil.getObjectName(upstreamList.get(0).getUpstreamUrl(), metaData.getServiceName()));
        Method method = prx.getClass().getDeclaredMethod(PrxInfoUtil.getMethodName(metaData.getMethodName()), tarsParamInfo.getParamTypes());
        tarsInvokePrxList.setMethod(method);
        tarsInvokePrxList.setParamTypes(tarsParamInfo.getParamTypes());
        tarsInvokePrxList.setParamNames(tarsParamInfo.getParamNames());
    }
    tarsInvokePrxList.getTarsInvokePrxList().addAll(upstreamList.stream().map(upstream -> {
        Object strProxy = communicator.stringToProxy(prxClass, PrxInfoUtil.getObjectName(upstream.getUpstreamUrl(), metaData.getServiceName()));
        return new TarsInvokePrx(strProxy, upstream.getUpstreamUrl());
    }).collect(Collectors.toList()));
}
Also used : TarsInvokePrx(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrx) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) Method(java.lang.reflect.Method)

Example 3 with TarsInvokePrxList

use of org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList in project incubator-shenyu by apache.

the class ApplicationConfigCacheTest method testGet.

@Test
public void testGet() throws ClassNotFoundException {
    final String rpcExt = "{\"methodInfo\":[{\"methodName\":\"method1\",\"params\":" + "[{\"left\":\"int\",\"right\":\"param1\"},{\"left\":\"java.lang.Integer\"," + "\"right\":\"param2\"}],\"returnType\":\"java.lang.String\"}]}";
    final MetaData metaData = new MetaData("id", "127.0.0.1:8080", "contextPath", "path5", RpcTypeEnum.TARS.getName(), "serviceName5", "method1", "parameterTypes", rpcExt, false);
    assertThrows(NullPointerException.class, () -> {
        applicationConfigCacheUnderTest.initPrx(metaData);
        final TarsInvokePrxList result = applicationConfigCacheUnderTest.get("path5");
        assertNotNull(result);
        assertEquals("promise_method1", result.getMethod().getName());
        assertEquals(2, result.getParamTypes().length);
        assertEquals(2, result.getParamNames().length);
        Class<?> prxClazz = Class.forName(PrxInfoUtil.getPrxName(metaData));
        assertTrue(Arrays.stream(prxClazz.getAnnotations()).anyMatch(annotation -> annotation instanceof Servant));
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Arrays(java.util.Arrays) ShenyuThreadFactory(org.apache.shenyu.common.concurrent.ShenyuThreadFactory) Servant(com.qq.tars.protocol.annotation.Servant) Executors(java.util.concurrent.Executors) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum) Test(org.junit.jupiter.api.Test) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Lists(org.assertj.core.util.Lists) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) PrxInfoUtil(org.apache.shenyu.plugin.tars.util.PrxInfoUtil) MetaData(org.apache.shenyu.common.dto.MetaData) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) MetaData(org.apache.shenyu.common.dto.MetaData) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) Servant(com.qq.tars.protocol.annotation.Servant) Test(org.junit.jupiter.api.Test)

Example 4 with TarsInvokePrxList

use of org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList in project incubator-shenyu by apache.

the class TarsPluginTest method testTarsPluginNormal.

@Test
public void testTarsPluginNormal() throws InvocationTargetException, IllegalAccessException {
    ShenyuContext context = mock(ShenyuContext.class);
    exchange.getAttributes().put(Constants.CONTEXT, context);
    exchange.getAttributes().put(Constants.META_DATA, metaData);
    exchange.getAttributes().put(Constants.PARAM_TRANSFORM, "{\"param1\":\"1\",\"param2\":\"1\"}");
    when(chain.execute(exchange)).thenReturn(Mono.empty());
    RuleData data = mock(RuleData.class);
    SelectorData selectorData = mock(SelectorData.class);
    TarsInvokePrxList tarsInvokePrxList = ApplicationConfigCache.getInstance().get(metaData.getPath());
    Method method = mock(Method.class);
    ExecutorService executorService = Executors.newFixedThreadPool(1, ShenyuThreadFactory.create("long-polling", true));
    CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> "", executorService);
    when(method.invoke(any(), any())).thenReturn(stringCompletableFuture);
    tarsInvokePrxList.setMethod(method);
    assertThrows(IllegalArgumentException.class, () -> {
        StepVerifier.create(tarsPluginUnderTest.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
    });
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) ExecutorService(java.util.concurrent.ExecutorService) Method(java.lang.reflect.Method) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 5 with TarsInvokePrxList

use of org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList in project incubator-shenyu by apache.

the class ApplicationConfigCacheTest method testInitPrx.

@Test
public void testInitPrx() {
    final MetaData metaData = new MetaData("id", "127.0.0.1:8080", "contextPath", "path6", RpcTypeEnum.TARS.getName(), "serviceName6", "method1", "parameterTypes", "{\"methodInfo\":[{\"methodName\":\"method1\",\"params\":[{\"left\":\"int\",\"right\":\"param1\"}," + "{\"left\":\"java.lang.Integer\",\"right\":\"param2\"}],\"returnType\":\"java.lang.String\"}]}", false);
    assertThrows(NullPointerException.class, () -> {
        applicationConfigCacheUnderTest.initPrx(metaData);
        final TarsInvokePrxList result = applicationConfigCacheUnderTest.get("path6");
        assertEquals("promise_method1", result.getMethod().getName());
    });
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) Test(org.junit.jupiter.api.Test)

Aggregations

TarsInvokePrxList (org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList)5 Method (java.lang.reflect.Method)3 MetaData (org.apache.shenyu.common.dto.MetaData)3 Test (org.junit.jupiter.api.Test)3 ExecutorService (java.util.concurrent.ExecutorService)2 RuleData (org.apache.shenyu.common.dto.RuleData)2 SelectorData (org.apache.shenyu.common.dto.SelectorData)2 RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)2 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)2 PrxInfoUtil (org.apache.shenyu.plugin.tars.util.PrxInfoUtil)2 Servant (com.qq.tars.protocol.annotation.Servant)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Random (java.util.Random)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ShenyuThreadFactory (org.apache.shenyu.common.concurrent.ShenyuThreadFactory)1