From 2b985abe1e4501d51eab65ec41b5d6dd9de7577f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Fri, 4 May 2018 17:14:48 +0800 Subject: [PATCH 1/2] fix: common client only support for motan2 --- .../api/motan/proxy/RefererCommonHandler.java | 3 -- .../com/weibo/api/motan/util/ReflectUtil.java | 9 ----- .../motan/demo/client/DemoRpcClient.java | 38 +++--------------- .../motan/demo/client/Motan2RpcClient.java | 39 ++++++++++++++++++- .../src/main/resources/motan2_demo_client.xml | 3 ++ .../src/main/resources/motan_demo_client.xml | 4 -- .../demo/server/MotanDemoServiceImpl.java | 4 ++ 7 files changed, 49 insertions(+), 51 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java b/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java index 6795884e5..6e369c355 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java +++ b/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java @@ -3,7 +3,6 @@ import com.weibo.api.motan.cluster.Cluster; import com.weibo.api.motan.rpc.DefaultRequest; import com.weibo.api.motan.rpc.Request; -import com.weibo.api.motan.util.ReflectUtil; import com.weibo.api.motan.util.RequestIdGenerator; import java.util.List; @@ -27,7 +26,6 @@ public Object call(String methodName, Object[] arguments, Class returnType, Map< request.setMethodName(methodName); request.setArguments(arguments); request.setAttachments(attachments); - request.setParamtersDesc(ReflectUtil.getParamsDesc(arguments)); return invokeRequest(request, returnType, async); } @@ -58,7 +56,6 @@ public Request buildRequest(String methodName, Object[] arguments) { request.setInterfaceName(interfaceName); request.setMethodName(methodName); request.setArguments(arguments); - request.setParamtersDesc(ReflectUtil.getParamsDesc(arguments)); return request; } diff --git a/motan-core/src/main/java/com/weibo/api/motan/util/ReflectUtil.java b/motan-core/src/main/java/com/weibo/api/motan/util/ReflectUtil.java index a2ea163b0..574002585 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/util/ReflectUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/util/ReflectUtil.java @@ -70,15 +70,6 @@ public static String getMethodParamDesc(Method method) { return builder.substring(0, builder.length() - 1); } - public static String getParamsDesc(Object[] params) { - StringBuilder builder = new StringBuilder(); - for (Object param : params) { - String className = getName(param.getClass()); - builder.append(className).append(PARAM_CLASS_SPLIT); - } - return builder.substring(0, builder.length() - 1); - } - /** * 获取方法的标示 : method_name + "(" + paramDesc + ")" * diff --git a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java index 6eeada043..a57232959 100644 --- a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java +++ b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java @@ -16,49 +16,21 @@ package com.weibo.motan.demo.client; -import com.weibo.api.motan.proxy.CommonHandler; -import com.weibo.api.motan.rpc.Future; -import com.weibo.api.motan.rpc.FutureListener; -import com.weibo.api.motan.rpc.Request; -import com.weibo.api.motan.rpc.ResponseFuture; import com.weibo.motan.demo.service.MotanDemoService; -import com.weibo.motan.demo.service.model.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DemoRpcClient { - public static void main(String[] args) throws Throwable { + public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan_demo_client.xml"}); MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer"); - System.out.println(service.hello("motan")); - - CommonHandler client = (CommonHandler) ctx.getBean("motanDemoReferer2"); - User user = new User(1, "AAA"); - System.out.println(user); - - user = (User) client.call("rename", new Object[]{user, "BBB"}, User.class); - System.out.println(user); - - ResponseFuture future = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "CCC"}, User.class); - user = (User) future.getValue(); - System.out.println(user); - - ResponseFuture future2 = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "DDD"}, User.class); - future2.addListener(new FutureListener() { - @Override - public void operationComplete(Future future) { - System.out.println(future.getValue()); - } - }); - - Request request = client.buildRequest("rename", new Object[]{user, "EEE"}); - request.setAttachment("a", "a"); - user = (User) client.call(request, User.class); - System.out.println(user); - + for (int i = 0; i < Integer.MAX_VALUE; i++) { + System.out.println(service.hello("motan" + i)); + Thread.sleep(1000); + } System.out.println("motan demo is finish."); System.exit(0); } diff --git a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java index 489ee8882..6c378d240 100644 --- a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java +++ b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java @@ -18,15 +18,21 @@ package com.weibo.motan.demo.client; +import com.weibo.api.motan.proxy.CommonHandler; +import com.weibo.api.motan.rpc.Future; +import com.weibo.api.motan.rpc.FutureListener; +import com.weibo.api.motan.rpc.Request; +import com.weibo.api.motan.rpc.ResponseFuture; import com.weibo.motan.demo.service.MotanDemoService; import com.weibo.motan.demo.service.PbParamService; +import com.weibo.motan.demo.service.model.User; import io.grpc.examples.routeguide.Point; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Motan2RpcClient { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Throwable { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan2_demo_client.xml"}); @@ -37,10 +43,39 @@ public static void main(String[] args) throws InterruptedException { service = (MotanDemoService) ctx.getBean("motanDemoReferer-simple"); print(service); - //pb serialization + // pb serialization PbParamService pbService = (PbParamService) ctx.getBean("motanDemoReferer-pb"); System.out.println(pbService.getFeature(Point.newBuilder().setLatitude(123).setLongitude(456).build())); + // common client + CommonHandler client = (CommonHandler) ctx.getBean("motanDemoReferer-common-client"); + System.out.println(client.call("hello", new Object[]{"a"}, String.class)); + + User user = new User(1, "AAA"); + System.out.println(user); + + user = (User) client.call("rename", new Object[]{user, "BBB"}, User.class); + System.out.println(user); + + ResponseFuture future = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "CCC"}, User.class); + user = (User) future.getValue(); + System.out.println(user); + + ResponseFuture future2 = (ResponseFuture) client.asyncCall("rename", new Object[]{user, "DDD"}, User.class); + future2.addListener(new FutureListener() { + @Override + public void operationComplete(Future future) { + System.out.println(future.getValue()); + } + }); + + Request request = client.buildRequest("rename", new Object[]{user, "EEE"}); + request.setAttachment("a", "a"); + user = (User) client.call(request, User.class); + System.out.println(user); + + client.call("rename", new Object[]{null, "FFF"}, void.class); + System.out.println("motan demo is finish."); System.exit(0); } diff --git a/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml b/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml index dfa1c827a..eceefdf9d 100755 --- a/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml +++ b/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml @@ -59,4 +59,7 @@ + \ No newline at end of file diff --git a/motan-demo/motan-demo-client/src/main/resources/motan_demo_client.xml b/motan-demo/motan-demo-client/src/main/resources/motan_demo_client.xml index 201ab426f..3dfc3c1c6 100755 --- a/motan-demo/motan-demo-client/src/main/resources/motan_demo_client.xml +++ b/motan-demo/motan-demo-client/src/main/resources/motan_demo_client.xml @@ -42,9 +42,5 @@ - - \ No newline at end of file diff --git a/motan-demo/motan-demo-server/src/main/java/com/weibo/motan/demo/server/MotanDemoServiceImpl.java b/motan-demo/motan-demo-server/src/main/java/com/weibo/motan/demo/server/MotanDemoServiceImpl.java index ecd5241b7..4a98cf976 100644 --- a/motan-demo/motan-demo-server/src/main/java/com/weibo/motan/demo/server/MotanDemoServiceImpl.java +++ b/motan-demo/motan-demo-server/src/main/java/com/weibo/motan/demo/server/MotanDemoServiceImpl.java @@ -31,6 +31,10 @@ public String hello(String name) { @Override public User rename(User user, String name) { + if (user == null) { + System.out.println("user: null"); + return null; + } System.out.println(user.getId() + " rename " + user.getName() + " to " + name); user.setName(name); return user; From 246fa77c24c4b511d60552d9934ff7291b6bb870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Wed, 16 May 2018 10:56:20 +0800 Subject: [PATCH 2/2] set default value to proxy --- .../api/motan/config/AbstractConfig.java | 18 +++--- .../weibo/api/motan/config/RefererConfig.java | 14 +++-- .../motan/demo/client/Motan2RpcClient.java | 57 +++++++++++++++---- .../src/main/resources/motan2_demo_client.xml | 2 +- .../src/test/resources/spi_test_context.xml | 19 +------ 5 files changed, 67 insertions(+), 43 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/config/AbstractConfig.java b/motan-core/src/main/java/com/weibo/api/motan/config/AbstractConfig.java index 9e7ddd1b0..51fb977b0 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/config/AbstractConfig.java +++ b/motan-core/src/main/java/com/weibo/api/motan/config/AbstractConfig.java @@ -16,19 +16,18 @@ package com.weibo.api.motan.config; -import java.io.Serializable; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.StringUtils; - import com.weibo.api.motan.common.MotanConstants; import com.weibo.api.motan.config.annotation.ConfigDesc; import com.weibo.api.motan.exception.MotanErrorMsgConstant; import com.weibo.api.motan.exception.MotanFrameworkException; import com.weibo.api.motan.util.LoggerUtil; +import org.apache.commons.lang3.StringUtils; + +import java.io.Serializable; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.List; +import java.util.Map; /** * @@ -94,8 +93,7 @@ protected void appendConfigParams(Map parameters, String prefix) String name = method.getName(); if (isConfigMethod(method)) { int idx = name.startsWith("get") ? 3 : 2; - String prop = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1); - String key = prop; + String key = name.substring(idx, idx + 1).toLowerCase() + name.substring(idx + 1); ConfigDesc configDesc = method.getAnnotation(ConfigDesc.class); if (configDesc != null && !StringUtils.isBlank(configDesc.key())) { key = configDesc.key(); diff --git a/motan-core/src/main/java/com/weibo/api/motan/config/RefererConfig.java b/motan-core/src/main/java/com/weibo/api/motan/config/RefererConfig.java index 6cb770503..688b855f3 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/config/RefererConfig.java +++ b/motan-core/src/main/java/com/weibo/api/motan/config/RefererConfig.java @@ -116,8 +116,8 @@ public synchronized void initRef() { checkInterfaceAndMethods(interfaceClass, methods); - clusterSupports = new ArrayList>(protocols.size()); - List> clusters = new ArrayList>(protocols.size()); + clusterSupports = new ArrayList<>(protocols.size()); + List> clusters = new ArrayList<>(protocols.size()); String proxy = null; ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE); @@ -125,7 +125,7 @@ public synchronized void initRef() { List registryUrls = loadRegistryUrls(); String localIp = getLocalHostAddress(registryUrls); for (ProtocolConfig protocol : protocols) { - Map params = new HashMap(); + Map params = new HashMap<>(); params.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER); params.put(URLParamType.version.getName(), URLParamType.version.getValue()); params.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis())); @@ -140,8 +140,10 @@ public synchronized void initRef() { clusterSupports.add(clusterSupport); clusters.add(clusterSupport.getCluster()); - proxy = (proxy == null) ? refUrl.getParameter(URLParamType.proxy.getName(), URLParamType.proxy.getValue()) : proxy; - + if (proxy == null) { + String defaultValue = StringUtils.isBlank(serviceInterface) ? URLParamType.proxy.getValue() : MotanConstants.PROXY_COMMON; + proxy = refUrl.getParameter(URLParamType.proxy.getName(), defaultValue); + } } ref = configHandler.refer(interfaceClass, clusters, proxy); @@ -150,7 +152,7 @@ public synchronized void initRef() { } private ClusterSupport createClusterSupport(URL refUrl, ConfigHandler configHandler, List registryUrls) { - List regUrls = new ArrayList(); + List regUrls = new ArrayList<>(); // 如果用户指定directUrls 或者 injvm协议访问,则使用local registry if (StringUtils.isNotBlank(directUrl) || MotanConstants.PROTOCOL_INJVM.equals(refUrl.getProtocol())) { diff --git a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java index 6c378d240..63982bbc1 100644 --- a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java +++ b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/Motan2RpcClient.java @@ -18,6 +18,9 @@ package com.weibo.motan.demo.client; +import com.weibo.api.motan.config.ProtocolConfig; +import com.weibo.api.motan.config.RefererConfig; +import com.weibo.api.motan.config.RegistryConfig; import com.weibo.api.motan.proxy.CommonHandler; import com.weibo.api.motan.rpc.Future; import com.weibo.api.motan.rpc.FutureListener; @@ -33,12 +36,12 @@ public class Motan2RpcClient { public static void main(String[] args) throws Throwable { - ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan2_demo_client.xml"}); // hessian MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer"); print(service); + // simple serialization service = (MotanDemoService) ctx.getBean("motanDemoReferer-simple"); print(service); @@ -48,7 +51,22 @@ public static void main(String[] args) throws Throwable { System.out.println(pbService.getFeature(Point.newBuilder().setLatitude(123).setLongitude(456).build())); // common client - CommonHandler client = (CommonHandler) ctx.getBean("motanDemoReferer-common-client"); + CommonHandler xmlClient = (CommonHandler) ctx.getBean("motanDemoReferer-common-client"); + motan2XmlCommonClientDemo(xmlClient); + motan2ApiCommonClientDemo(); + + System.out.println("motan demo is finish."); + System.exit(0); + } + + public static void print(MotanDemoService service) throws InterruptedException { + for (int i = 0; i < 3; i++) { + System.out.println(service.hello("motan" + i)); + Thread.sleep(1000); + } + } + + public static void motan2XmlCommonClientDemo(CommonHandler client) throws Throwable { System.out.println(client.call("hello", new Object[]{"a"}, String.class)); User user = new User(1, "AAA"); @@ -75,16 +93,35 @@ public void operationComplete(Future future) { System.out.println(user); client.call("rename", new Object[]{null, "FFF"}, void.class); - - System.out.println("motan demo is finish."); - System.exit(0); } - public static void print(MotanDemoService service) throws InterruptedException { - for (int i = 0; i < 3; i++) { - System.out.println(service.hello("motan" + i)); - Thread.sleep(1000); - } + public static void motan2ApiCommonClientDemo() throws Throwable { + RefererConfig referer = new RefererConfig<>(); + + // 设置服务端接口 + referer.setInterface(CommonHandler.class); + referer.setServiceInterface("com.weibo.motan.demo.service.MotanDemoService"); + + // 配置服务的group以及版本号 + referer.setGroup("motan-demo-rpc"); + referer.setVersion("1.0"); + referer.setRequestTimeout(1000); + + // 配置注册中心直连调用 + RegistryConfig registry = new RegistryConfig(); + registry.setRegProtocol("direct"); + registry.setAddress("127.0.0.1:8001"); + referer.setRegistry(registry); + + // 配置RPC协议 + ProtocolConfig protocol = new ProtocolConfig(); + protocol.setId("motan2"); + protocol.setName("motan2"); + referer.setProtocol(protocol); + + // 使用服务 + CommonHandler client = referer.getRef(); + System.out.println(client.call("hello", new Object[]{"a"}, String.class)); } } diff --git a/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml b/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml index eceefdf9d..503481ae9 100755 --- a/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml +++ b/motan-demo/motan-demo-client/src/main/resources/motan2_demo_client.xml @@ -59,7 +59,7 @@ - \ No newline at end of file diff --git a/motan-springsupport/src/test/resources/spi_test_context.xml b/motan-springsupport/src/test/resources/spi_test_context.xml index a862d4a4a..0bb6469ac 100644 --- a/motan-springsupport/src/test/resources/spi_test_context.xml +++ b/motan-springsupport/src/test/resources/spi_test_context.xml @@ -17,24 +17,11 @@ - + \ No newline at end of file