This is an automated email from the ASF dual-hosted git repository. jerrick pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new e18810b [Dubbo-2446]Fix ClassNotFoundException when load Service Interface by parent ClassLoader (#2447) e18810b is described below commit e18810baba0e2314f11a2c8c080cd58d9af446bf Author: 邓志 <dz_...@163.com> AuthorDate: Tue Sep 11 15:32:25 2018 +0800 [Dubbo-2446]Fix ClassNotFoundException when load Service Interface by parent ClassLoader (#2447) * 用jarslink做的module隔离,dubbo调用时会抛ClassNotFoundException.经查是ClassLoader不一致。为了严谨,做此修改,以确保Service Interface和Invocation Interface的ClassLoader是同一个。 * invokerInterface非空判断 --- .../src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java | 8 ++++++++ .../src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java index ea499a3..78f7b92 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectUtils.java @@ -591,6 +591,14 @@ public final class ReflectUtils { } } + public static Class<?> forName(ClassLoader cl, String name) { + try { + return name2class(cl, name); + } catch (ClassNotFoundException e) { + throw new IllegalStateException("Not found class " + name + ", cause: " + e.getMessage(), e); + } + } + /** * name to class. * "boolean" => boolean.class diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index 077970a..e2b0a66 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -49,7 +49,9 @@ public class RpcUtils { && !invocation.getMethodName().startsWith("$")) { String service = invocation.getInvoker().getUrl().getServiceInterface(); if (service != null && service.length() > 0) { - Class<?> cls = ReflectUtils.forName(service); + Class<?> invokerInterface = invocation.getInvoker().getInterface(); + Class<?> cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service) + : ReflectUtils.forName(service); Method method = cls.getMethod(invocation.getMethodName(), invocation.getParameterTypes()); if (method.getReturnType() == void.class) { return null; @@ -71,7 +73,9 @@ public class RpcUtils { && !invocation.getMethodName().startsWith("$")) { String service = invocation.getInvoker().getUrl().getServiceInterface(); if (service != null && service.length() > 0) { - Class<?> cls = ReflectUtils.forName(service); + Class<?> invokerInterface = invocation.getInvoker().getInterface(); + Class<?> cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service) + : ReflectUtils.forName(service); Method method = cls.getMethod(invocation.getMethodName(), invocation.getParameterTypes()); if (method.getReturnType() == void.class) { return null;