从图中可以看出,传进来的proxy是DemoServiceImpl的一个实例对象,而type是DemoService接口的class对象,可以猜测下面的Wrapper.getWrapper方法就是为该实例对象生成代理对象。下面具体分析。
上述通过makeWrapper(c)方法会生成具体的Wrapper类,这里面代码比较长,它是通过javassist构建 Class。下面先通过阿里开源的arthas工具查看它生成的Wrapper类到底是什么? Arthas 用户文档:https://arthas.aliyun.com/doc/install-detail.html#id1。首先启动Provider服务提供者程序,将断点放置到ret = makerWrapper(c)这里,确定这里的c是DemoServiceImpl,然后运行程序至下一步,观察ret的名字,如果上面传递过来的c是:com.alibaba.dubbo.demo.DemoService对象 那么此时ret没有值,当传递过来的c是com.alibaba.dubbo.demo.provider.DemoServiceImpl对象时,才会有下图的ret属性。
如下图:
可以发现生成的Wrapper类是叫Wrapper1,然后放开断点,让程序走完,服务提供者处于运行状态。下图通过Arthas查看生成的Wrapper1到底是什么? 首先下载它的jar包,下载地址。然后打开cmd,执行以下命令。注意2是对应Provider程序的序号,需要和你自己程序对应。输入2后可以发现连上了Arthas。然后使用sc命令查找Wrapper1具体对应的是哪个class文件,最后使用jad命令反编译,查看Wrapper1类对应的Java文件具体是什么?
安装Arthas 按照文档:https://arthas.aliyun.com/doc/install-detail.html#id1
运行java -jar arthas-boot.jar 需要进入到arthas的减压目录中运行(否则会报错没有权限运行该jar),可以通过命令:
find / -name arthas-boot.jar查找安装路径,如:/Users/luzhen/.arthas/lib/3.4.0/arthas
$ java -jar arthas-boot.jar 53931 [INFO] arthas-boot version: 3.4.0 [INFO] arthas home: /Users/luzhen/.arthas/lib/3.4.0/arthas [INFO] Try to attach process 53931 [INFO] Attach process 53931 success. [INFO] arthas-client connect 127.0.0.1 3658 ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki https://arthas.aliyun.com/doc tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html version 3.4.0 pid 53931 time 2020-09-04 00:19:33 [arthas@53931]$ sc *Wrapper* com.alibaba.dubbo.common.bytecode.Wrapper com.alibaba.dubbo.common.bytecode.Wrapper$1 com.alibaba.dubbo.common.bytecode.Wrapper0 com.alibaba.dubbo.common.bytecode.Wrapper1 com.alibaba.dubbo.registry.integration.RegistryProtocol$ExporterChangeableWrapper com.alibaba.dubbo.registry.integration.RegistryProtocol$InvokerDelegete com.alibaba.dubbo.registry.support.ProviderInvokerWrapper com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterWrapper com.alibaba.dubbo.rpc.listener.ListenerExporterWrapper com.alibaba.dubbo.rpc.protocol.InvokerWrapper com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1 com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper com.alibaba.dubbo.rpc.proxy.wrapper.StubProxyFactoryWrapper com.sun.org.apache.xerces.internal.util.EntityResolverWrapper com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper com.taobao.arthas.core.shell.handlers.term.CloseHandlerWrapper org.springframework.beans.BeanWrapper org.springframework.beans.BeanWrapperImpl org.springframework.beans.BeanWrapperImpl$BeanPropertyHandler org.springframework.core.$Proxy2 org.springframework.core.$Proxy6 org.springframework.core.SerializableTypeWrapper org.springframework.core.SerializableTypeWrapper$1 org.springframework.core.SerializableTypeWrapper$2 org.springframework.core.SerializableTypeWrapper$MethodInvokeTypeProvider org.springframework.core.SerializableTypeWrapper$MethodParameterTypeProvider org.springframework.core.SerializableTypeWrapper$SerializableTypeProxy org.springframework.core.SerializableTypeWrapper$SimpleTypeProvider org.springframework.core.SerializableTypeWrapper$TypeProvider org.springframework.core.SerializableTypeWrapper$TypeProxyInvocationHandler sun.invoke.util.Wrapper sun.invoke.util.Wrapper$Format [Lsun.invoke.util.Wrapper; sun.nio.ch.KQueueArrayWrapper sun.nio.ch.KQueueArrayWrapper$Update Affect(row-cnt:36) cost in 86 ms.因为断点放开了,所以此时内存中会有很多Wrapper文件,根据推测
com.alibaba.dubbo.demo.DemoService这个对象的代理类应该是文件com.alibaba.dubbo.common.bytecode.Wrapper0jad 反编译:
[arthas@53931]$ jad com.alibaba.dubbo.common.bytecode.Wrapper0 ClassLoader: +-sun.misc.Launcher$AppClassLoader@18b4aac2 +-sun.misc.Launcher$ExtClassLoader@369f73a2 Location: /Users/luzhen/Downloads/dubbo-2.6.5/dubbo-common/target/classes/ /* * Decompiled with CFR. * * Could not load the following classes: * com.alibaba.dubbo.common.bytecode.ClassGenerator$DC * com.alibaba.dubbo.common.bytecode.NoSuchMethodException * com.alibaba.dubbo.common.bytecode.NoSuchPropertyException * com.alibaba.dubbo.common.bytecode.Wrapper * com.alibaba.dubbo.demo.DemoService */ package com.alibaba.dubbo.common.bytecode; import com.alibaba.dubbo.common.bytecode.ClassGenerator; import com.alibaba.dubbo.common.bytecode.NoSuchMethodException; import com.alibaba.dubbo.common.bytecode.NoSuchPropertyException; import com.alibaba.dubbo.common.bytecode.Wrapper; import com.alibaba.dubbo.demo.DemoService; import java.lang.reflect.InvocationTargetException; import java.util.Map; public class Wrapper0 extends Wrapper implements ClassGenerator.DC { public static String[] pns; public static Map pts; public static String[] mns; public static String[] dmns; public static Class[] mts0; public String[] getPropertyNames() { return pns; } public boolean hasProperty(String string) { return pts.containsKey(string); } public Class getPropertyType(String string) { return (Class)pts.get(string); } public String[] getMethodNames() { return mns; } public String[] getDeclaredMethodNames() { return dmns; } public void setPropertyValue(Object object, String string, Object object2) { try { DemoService demoService = (DemoService)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.DemoService.").toString()); } public Object getPropertyValue(Object object, String string) { try { DemoService demoService = (DemoService)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.DemoService.").toString()); } public Object invokeMethod(Object object, String string, Class[] arrclass, Object[] arrobject) throws InvocationTargetException { DemoService demoService; try { demoService = (DemoService)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } try { if ("sayHello".equals(string) && arrclass.length == 1) { return demoService.sayHello((String)arrobject[0]); } } catch (Throwable throwable) { throw new InvocationTargetException(throwable); } throw new NoSuchMethodException(new StringBuffer().append("Not found method \"").append(string).append("\" in class com.alibaba.dubbo.demo.DemoService.").toString()); } } Affect(row-cnt:1) cost in 455 ms. com.alibaba.dubbo.demo.provider.DemoServiceImpl对应的Wrapper代理类应该是com.alibaba.dubbo.common.bytecode.Wrapper1 [arthas@53931]$ jad com.alibaba.dubbo.common.bytecode.Wrapper1 ClassLoader: +-sun.misc.Launcher$AppClassLoader@18b4aac2 +-sun.misc.Launcher$ExtClassLoader@369f73a2 Location: /Users/luzhen/Downloads/dubbo-2.6.5/dubbo-common/target/classes/ /* * Decompiled with CFR. * * Could not load the following classes: * com.alibaba.dubbo.common.bytecode.ClassGenerator$DC * com.alibaba.dubbo.common.bytecode.NoSuchMethodException * com.alibaba.dubbo.common.bytecode.NoSuchPropertyException * com.alibaba.dubbo.common.bytecode.Wrapper * com.alibaba.dubbo.demo.provider.DemoServiceImpl */ package com.alibaba.dubbo.common.bytecode; import com.alibaba.dubbo.common.bytecode.ClassGenerator; import com.alibaba.dubbo.common.bytecode.NoSuchMethodException; import com.alibaba.dubbo.common.bytecode.NoSuchPropertyException; import com.alibaba.dubbo.common.bytecode.Wrapper; import com.alibaba.dubbo.demo.provider.DemoServiceImpl; import java.lang.reflect.InvocationTargetException; import java.util.Map; public class Wrapper1 extends Wrapper implements ClassGenerator.DC { public static String[] pns; public static Map pts; public static String[] mns; public static String[] dmns; public static Class[] mts0; public String[] getPropertyNames() { return pns; } public boolean hasProperty(String string) { return pts.containsKey(string); } public Class getPropertyType(String string) { return (Class)pts.get(string); } public String[] getMethodNames() { return mns; } public String[] getDeclaredMethodNames() { return dmns; } public void setPropertyValue(Object object, String string, Object object2) { try { DemoServiceImpl demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } public Object getPropertyValue(Object object, String string) { try { DemoServiceImpl demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } public Object invokeMethod(Object object, String string, Class[] arrclass, Object[] arrobject) throws InvocationTargetException { DemoServiceImpl demoServiceImpl; try { demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } try { if ("sayHello".equals(string) && arrclass.length == 1) { return demoServiceImpl.sayHello((String)arrobject[0]); } } catch (Throwable throwable) { throw new InvocationTargetException(throwable); } throw new NoSuchMethodException(new StringBuffer().append("Not found method \"").append(string).append("\" in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } } Affect(row-cnt:1) cost in 490 ms.
最后,我们可以得到Wrapper1的Java文件,如下
package com.alibaba.dubbo.common.bytecode; import com.alibaba.dubbo.common.bytecode.ClassGenerator; import com.alibaba.dubbo.common.bytecode.NoSuchMethodException; import com.alibaba.dubbo.common.bytecode.NoSuchPropertyException; import com.alibaba.dubbo.common.bytecode.Wrapper; import com.alibaba.dubbo.demo.provider.DemoServiceImpl; import java.lang.reflect.InvocationTargetException; import java.util.Map; public class Wrapper1 extends Wrapper implements ClassGenerator.DC { public static String[] pns; public static Map pts; public static String[] mns; public static String[] dmns; public static Class[] mts0; public String[] getPropertyNames() { return pns; } public boolean hasProperty(String string) { return pts.containsKey(string); } public Class getPropertyType(String string) { return (Class)pts.get(string); } public String[] getMethodNames() { return mns; } public String[] getDeclaredMethodNames() { return dmns; } public void setPropertyValue(Object object, String string, Object object2) { try { DemoServiceImpl demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } public Object getPropertyValue(Object object, String string) { try { DemoServiceImpl demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } throw new NoSuchPropertyException(new StringBuffer().append("Not found property \"").append(string).append("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } // 这个就是Wrapper对象的invokerMethod方法 public Object invokeMethod(Object object, String string, Class[] arrclass, Object[] arrobject) throws InvocationTargetException { DemoServiceImpl demoServiceImpl; try { demoServiceImpl = (DemoServiceImpl)object; } catch (Throwable throwable) { throw new IllegalArgumentException(throwable); } try { if ("sayHello".equals(string) && arrclass.length == 1) { // 调用DemoServiceImpl实例对象的sayHello方法,并将结果返回 return demoServiceImpl.sayHello((String)arrobject[0]); } } catch (Throwable throwable) { throw new InvocationTargetException(throwable); } throw new NoSuchMethodException(new StringBuffer().append("Not found method \"") .append(string) .append("\" in class com.alibaba.dubbo.demo.provider.DemoServiceImpl.").toString()); } }现在我们可以再来回顾一下JavassistProxyFactory中的getInvoker方法,它首先生成一个Wrapper实例对象,经过分析得知,它其实就是DemoServiceImpl的一个代理对象,内部还有一个invokeMethod方法。getInvoker方法中生成了一个匿名Invoker实例,当外部调用该Invoker的doInvoke方法时,最终会调用wrapper.invokeMethod方法,而wrapper.invokerMethod里面就是调用的目标对象,即DemoServiceImpl的sayHello方法。
public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) { final Wrapper wrapper = Wrapper.getWrapper(proxy.getClass().getName().indexOf('$') < 0 ? proxy.getClass() : type); return new AbstractProxyInvoker<T>(proxy, type, url) { @Override protected Object doInvoke(T proxy, String methodName,Class<?>[] parameterTypes,Object[] arguments) throws Throwable { return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments); } }; }makeWrapper方法就是创建上述的Wrapper的class文件,阅读代码时可以对照着看,更容易理解,具体就不分析,可以参考 服务导出。