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));
}
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()));
}
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));
});
}
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();
});
}
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());
});
}
Aggregations