dubbo调用mock
同理,为了实现dubbo的mock,也是要在dubbo调用的过程中添加拦截器
dubbo原始的执行拦截器
Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker))
这里代码写死了,没办法了,只能将整个JavassistProxyFactory 替换掉
public class JavassistProxyFactory extends AbstractProxyFactory {
@Override
@SuppressWarnings("unchecked")
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
}
@Override
public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
// TODO Wrapper cannot handle this scenario correctly: the classname contains '$'
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);
}
};
}
}
覆盖默认代理类
在项目 \src\test\resources\META-INF\dubbo\internal下增加文件org.apache.dubbo.rpc.ProxyFactory,增加自定义代理类
localMockProxy=com.xxx.RemoteResultAdaptDubboProxyFactory
@Adaptive
public class RemoteResultAdaptDubboProxyFactory extends JavassistProxyFactory {
@Override
@SuppressWarnings("unchecked")