You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by de...@apache.org on 2021/08/01 15:18:42 UTC
[incubator-shenyu] branch master updated: [ISSUE #1844] Optimze
code about dubbo async call (#1845)
This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 1eb8d09 [ISSUE #1844] Optimze code about dubbo async call (#1845)
1eb8d09 is described below
commit 1eb8d09792afbd3ba261269bdd9e0c1cffbcda5b
Author: Kevin Clair <70...@qq.com>
AuthorDate: Sun Aug 1 23:18:35 2021 +0800
[ISSUE #1844] Optimze code about dubbo async call (#1845)
* Update the printing method of exception log.
* Optimize code, invoke dubbo method with async module.
* Delete redundant judgement and update error log.
* update log.
* update log.
* update log.
* Fix check style.
---
.../dubbo/cache/ApplicationConfigCache.java | 6 ++---
.../plugin/apache/dubbo/ApacheDubboPlugin.java | 7 +++--
.../apache/dubbo/cache/ApplicationConfigCache.java | 5 ++--
.../dubbo/proxy/ApacheDubboProxyService.java | 30 ++++++++++------------
4 files changed, 22 insertions(+), 26 deletions(-)
diff --git a/shenyu-plugin/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/cache/ApplicationConfigCache.java b/shenyu-plugin/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/cache/ApplicationConfigCache.java
index 35628c3..78409d2 100644
--- a/shenyu-plugin/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/cache/ApplicationConfigCache.java
+++ b/shenyu-plugin/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/cache/ApplicationConfigCache.java
@@ -131,7 +131,7 @@ public final class ApplicationConfigCache {
return referenceConfig;
}
} catch (ExecutionException e) {
- log.error("init dubbo ref ex:{}", e.getMessage());
+ log.error("init dubbo ref exception", e);
}
return build(metaData);
@@ -171,11 +171,11 @@ public final class ApplicationConfigCache {
try {
Object obj = reference.get();
if (obj != null) {
- log.info("init alibaba dubbo reference success there meteData is :{}", metaData.toString());
+ log.info("init alibaba dubbo reference success there meteData is :{}", metaData);
cache.put(metaData.getPath(), reference);
}
} catch (Exception e) {
- log.error("init alibaba dubbo refernce ex:{}", e.getMessage());
+ log.error("init alibaba dubbo refernce exception", e);
}
return reference;
diff --git a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/ApacheDubboPlugin.java b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/ApacheDubboPlugin.java
index ffae7fa..b680b7d 100644
--- a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/ApacheDubboPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/ApacheDubboPlugin.java
@@ -27,11 +27,11 @@ import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.plugin.apache.dubbo.proxy.ApacheDubboProxyService;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
-import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
-import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@@ -62,8 +62,7 @@ public class ApacheDubboPlugin extends AbstractShenyuPlugin {
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);
+ log.error(" path is : {}, meta data have error : {}", shenyuContext.getPath(), metaData);
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
Object error = ShenyuResultWrap.error(ShenyuResultEnum.META_DATA_ERROR.getCode(), ShenyuResultEnum.META_DATA_ERROR.getMsg(), null);
return WebFluxResultUtils.result(exchange, error);
diff --git a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApplicationConfigCache.java b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApplicationConfigCache.java
index 5300c00..f759b99 100644
--- a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApplicationConfigCache.java
+++ b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApplicationConfigCache.java
@@ -131,7 +131,7 @@ public final class ApplicationConfigCache {
return referenceConfig;
}
} catch (ExecutionException e) {
- log.error("init dubbo ref ex:{}", e.getMessage());
+ log.error("init dubbo ref exception", e);
}
return build(metaData);
@@ -146,6 +146,7 @@ public final class ApplicationConfigCache {
public ReferenceConfig<GenericService> build(final MetaData metaData) {
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
reference.setGeneric("true");
+ reference.setAsync(true);
reference.setApplication(applicationConfig);
reference.setRegistry(registryConfig);
reference.setInterface(metaData.getServiceName());
@@ -175,7 +176,7 @@ public final class ApplicationConfigCache {
cache.put(metaData.getPath(), reference);
}
} catch (Exception e) {
- log.error("init apache dubbo reference ex:{}", e.getMessage());
+ log.error("init apache dubbo reference exception", e);
}
return reference;
}
diff --git a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
index c35bf51..747d6a7 100644
--- a/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
@@ -26,12 +26,12 @@ import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericException;
import org.apache.dubbo.rpc.service.GenericService;
-import org.apache.shenyu.common.exception.ShenyuException;
-import org.apache.shenyu.plugin.apache.dubbo.cache.ApplicationConfigCache;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.ResultEnum;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.common.utils.ParamCheckUtils;
+import org.apache.shenyu.plugin.apache.dubbo.cache.ApplicationConfigCache;
import org.apache.shenyu.plugin.api.param.BodyParamResolveService;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@@ -39,8 +39,6 @@ import reactor.core.publisher.Mono;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
-import static org.apache.dubbo.rpc.Constants.ASYNC_KEY;
-
/**
* dubbo proxy service is use GenericService.
*/
@@ -85,19 +83,7 @@ public class ApacheDubboProxyService {
} else {
pair = bodyParamResolveService.buildParameter(body, metaData.getParameterTypes());
}
- //Compatible with asynchronous calls of lower Dubbo versions
- RpcContext.getContext().setAttachment(ASYNC_KEY, Boolean.TRUE.toString());
- Object data = genericService.$invoke(metaData.getMethodName(), pair.getLeft(), pair.getRight());
- if (Objects.isNull(data)) {
- data = RpcContext.getContext().getFuture();
- }
- CompletableFuture<Object> future;
- if (data instanceof CompletableFuture) {
- future = (CompletableFuture<Object>) data;
- } else {
- future = CompletableFuture.completedFuture(data);
- }
- return Mono.fromFuture(future.thenApply(ret -> {
+ return Mono.fromFuture(invokeAsync(genericService, metaData.getMethodName(), pair.getLeft(), pair.getRight()).thenApply(ret -> {
if (Objects.isNull(ret)) {
ret = Constants.DUBBO_RPC_RESULT_EMPTY;
}
@@ -106,4 +92,14 @@ public class ApacheDubboProxyService {
return ret;
})).onErrorMap(exception -> exception instanceof GenericException ? new ShenyuException(((GenericException) exception).getExceptionMessage()) : new ShenyuException(exception));
}
+
+ private CompletableFuture<Object> invokeAsync(final GenericService genericService, final String method, final String[] parameterTypes, final Object[] args) throws GenericException {
+ //Compatible with asynchronous calls of lower Dubbo versions
+ genericService.$invoke(method, parameterTypes, args);
+ Object resultFromFuture = RpcContext.getContext().getFuture();
+ if (resultFromFuture instanceof CompletableFuture) {
+ return (CompletableFuture<Object>) resultFromFuture;
+ }
+ return CompletableFuture.completedFuture(resultFromFuture);
+ }
}