提供端和消费端都需要生成代理对象,默认通过 JavassistProxyFactory
提供端
在项目启动暴露服务的时候完成代理对象的创建工作,调用顺序
ServiceBean#afterPropertiesSet =>
ServiceConfig#export =>
ServiceConfig#doExport =>
ServiceConfig#doExportUrlsFor1Protocol
- 创建代理对象
Wrapper
- 创建
AbstractProxyInvoker
对象,在AbstractProxyInvoker#doInvoke
方法中调用Wrapper#invokeMethod
- 以
Invoker
对象为入参创建Exporter
对象
package org.apache.dubbo.common.bytecode;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.dubbo.common.bytecode.ClassGenerator.DC;
import org.apache.dubbo.demo.provider.DemoServiceImpl;
public class Wrapper1 extends Wrapper implements 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 var1) {
return pts.containsKey(var1);
}
public Class getPropertyType(String var1) {
return (Class)pts.get(var1);
}
public String[] getMethodNames() {
return mns;
}
public String[] getDeclaredMethodNames() {
return dmns;
}
public void setPropertyValue(Object var1, String var2, Object var3) {
try {
DemoServiceImpl var4 = (DemoServiceImpl)var1;
} catch (Throwable var6) {
throw new IllegalArgumentException(var6);
}
throw new NoSuchPropertyException("Not found property \"" + var2 + "\" field or setter method in class org.apache.dubbo.demo.provider.DemoServiceImpl.");
}
public Object getPropertyValue(Object var1, String var2) {
try {
DemoServiceImpl var3 = (DemoServiceImpl)var1;
} catch (Throwable var5) {
throw new IllegalArgumentException(var5);
}
throw new NoSuchPropertyException("Not found property \"" + var2 + "\" field or setter method in class org.apache.dubbo.demo.provider.DemoServiceImpl.");
}
public Object invokeMethod(Object var1, String var2, Class[] var3, Object[] var4) throws InvocationTargetException {
DemoServiceImpl var5;
try {
var5 = (DemoServiceImpl)var1;
} catch (Throwable var8) {
throw new IllegalArgumentException(var8);
}
try {
if ("sayHello".equals(var2) && var3.length == 1) {
return var5.sayHello((String)var4[0]);
}
} catch (Throwable var9) {
throw new InvocationTargetException(var9);
}
throw new NoSuchMethodException("Not found method \"" + var2 + "\" in class org.apache.dubbo.demo.provider.DemoServiceImpl.");
}
public Wrapper1() {
}
}
消费端
在项目启动创建引用Bean
的时候完成代理对象的创建工作,引用Bean
即所谓的代理对象,调用顺序
ReferenceBean#afterPropertiesSet =>
ReferenceBean#getObject =>
ReferenceConfig#init =>
ReferenceConfig#createProxy
- 创建
Invoker
对象,此过程完成 消费端节点注册、节点监听、Invoker对象创建 - 以
Invoker
为入参创建代理对象
public class proxy0 implements ClassGenerator.DC, EchoService, DemoService{
public static Method[] methods;
private InvocationHandler handler;
public String sayHello(String paramString){
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = paramString;
Object localObject = this.handler.invoke(this, methods[0], arrayOfObject);
return ((String)localObject);
}
public Object $echo(Object paramObject)
{
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = paramObject;
Object localObject = this.handler.invoke(this, methods[1], arrayOfObject);
return ((Object)localObject);
}
public proxy0(){
}
public proxy0(InvocationHandler paramInvocationHandler){
this.handler = paramInvocationHandler;
}
}