You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/27 11:36:58 UTC
[dubbo] branch 3.0 updated: An optimization of generic invocation
in gson format (#8924)
This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 711c323 An optimization of generic invocation in gson format (#8924)
711c323 is described below
commit 711c323efc77c4b37d322a3961aed41eef7acf7e
Author: 张志勇 <go...@163.com>
AuthorDate: Mon Sep 27 19:36:47 2021 +0800
An optimization of generic invocation in gson format (#8924)
---
.../dubbo/demo/consumer/GenericApplication.java | 47 +++++++++-------------
.../org/apache/dubbo/rpc/filter/GenericFilter.java | 6 ++-
2 files changed, 24 insertions(+), 29 deletions(-)
diff --git a/dubbo-demo/dubbo-demo-generic-call/src/main/java/org/apache/dubbo/demo/consumer/GenericApplication.java b/dubbo-demo/dubbo-demo-generic-call/src/main/java/org/apache/dubbo/demo/consumer/GenericApplication.java
index 365c1ea..1c16ad8 100644
--- a/dubbo-demo/dubbo-demo-generic-call/src/main/java/org/apache/dubbo/demo/consumer/GenericApplication.java
+++ b/dubbo-demo/dubbo-demo-generic-call/src/main/java/org/apache/dubbo/demo/consumer/GenericApplication.java
@@ -16,6 +16,7 @@
*/
package org.apache.dubbo.demo.consumer;
+import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.ReferenceConfig;
@@ -23,46 +24,45 @@ import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.rpc.service.GenericService;
-import java.util.HashMap;
-import java.util.Map;
+import com.google.gson.Gson;
public class GenericApplication {
+
public static void main(String[] args) {
- if (isClassic(args)) {
-// runWithRefer();
- } else {
- runWithBootstrap();
- }
+ runWithBootstrap(args);
}
- private static boolean isClassic(String[] args) {
- return args.length > 0 && "classic".equalsIgnoreCase(args[0]);
- }
- private static void runWithBootstrap() {
+ private static void runWithBootstrap(String[] args) {
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
reference.setInterface("org.apache.dubbo.demo.DemoService");
- reference.setGeneric("true");
+
+ String param = "dubbo generic invoke";
+
+ if (args.length > 0 && CommonConstants.GENERIC_SERIALIZATION_GSON.equals(args[0])) {
+ reference.setGeneric(CommonConstants.GENERIC_SERIALIZATION_GSON);
+ param = new Gson().toJson(param + " gson");
+ } else {
+ reference.setGeneric("true");
+ }
ApplicationConfig applicationConfig = new ApplicationConfig("demo-consumer");
- Map<String, String> parameters = new HashMap<>();
- applicationConfig.setParameters(parameters);
MetadataReportConfig metadataReportConfig = new MetadataReportConfig();
metadataReportConfig.setAddress("zookeeper://127.0.0.1:2181");
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(applicationConfig)
- .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
- .reference(reference)
- .start();
+ .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
+ .reference(reference)
+ .start();
// generic invoke
- GenericService genericService = (GenericService) bootstrap.getCache().get(reference);
+ GenericService genericService = bootstrap.getCache().get(reference);
while (true) {
try {
Object genericInvokeResult = genericService.$invoke("sayHello", new String[]{String.class.getName()},
- new Object[]{"dubbo generic invoke"});
+ new Object[]{param});
System.out.println(genericInvokeResult);
Thread.sleep(1000);
} catch (Exception e) {
@@ -71,13 +71,4 @@ public class GenericApplication {
}
}
-// private static void runWithRefer() {
-// ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
-// reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
-// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
-// reference.setInterface(DemoService.class);
-// DemoService service = reference.get();
-// String message = service.sayHello("dubbo");
-// System.out.println(message);
-// }
}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
index 57d5736..9230620 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
@@ -66,6 +66,8 @@ import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
private final Logger logger = LoggerFactory.getLogger(GenericFilter.class);
+ private static final Gson gson = new Gson();
+
private ApplicationModel applicationModel;
@Override
@@ -196,8 +198,10 @@ public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
}
private Object[] getGsonGenericArgs(final Object[] args, Type[] types) {
- Gson gson = new Gson();
return IntStream.range(0, args.length).mapToObj(i -> {
+ if (!(args[i] instanceof String)) {
+ throw new RpcException("When using GSON to deserialize generic dubbo request arguments, the arguments must be of type String");
+ }
String str = args[i].toString();
Type type = TypeToken.get(types[i]).getType();
try {