在Dubbo的服务发布过程中,Invoker的构造是一个重要的步骤,Invoker代表的是一个可以调用的服务的接口。它的定义如下:
public interface Invoker<T> extends Node {
/**
* get service interface.
*
* @return service interface.
*/
Class<T> getInterface();
/**
* invoke.
*
* @param invocation
* @return result
* @throws RpcException
*/
Result invoke(Invocation invocation) throws RpcException;
}
invoke方法就是代表一次RPC调用,Invocation类代表的是一次调用需要的参数。为了实现通用性,以及实现很多RPC相关的附加功能,在服务端对一个接口实现,不是机械地实现一个Invoker接口,而是实现了一个Invoker的调用链。本文主要就是分析这个调用链是如何构成的。(本文假设读者已经对Dubbo的基本配置和使用有所了解,对涉及到的基础知识不再讲解)
首先是ServiceConfig这个类的doExportUrlsFor1Protocol方法中的一段代码
for (URL registryURL : registryURLs) {
//if protocol is only injvm ,not register
if (LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
continue;
}
url = url.addParameterIfAbsent(DYNAMIC_KEY, registryURL.getParameter(DYNAMIC_KEY));
URL monitorUrl = loadMonitor(registryURL);
if (monitorUrl != null) {
url = url.addParameterAndEncoded(MONITOR_KEY, monitorUrl.toFullString());
}
if (logger.isInfoEnabled()) {
logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL);
}
// For providers, this is used to enable custom proxy to generate invoker
String proxy = url.getParameter(PROXY_KEY);
if (StringUtils.isNotEmpty(proxy)) {
registryURL = registryURL.addParameter(PROXY_KEY, proxy);
}
Invoker<?> invoker = PROXY_FACTORY.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(EXPORT_KEY, url.toFullString()));
DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this);
Exporter<?> exporter = protocol.export(wrapperInvoker);
exporters.add(exporter);
}
这个for循环是遍历配置的所有的注册中心,进行服务的发布。下面看循环体的内容,前面的内容主要是对配置进行一些处理,之后到了下面这一行
Invoker<?> invoker = PROXY_FACTORY.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(EXPORT_KEY, url.toFullString()));
这个PROXY_FACTORY定义
/**
* A {@link ProxyFactory} implementation that will generate a exported service proxy,the JavassistProxyFactory is its
* default implementation
*/
private static final ProxyFactory PROXY_FACTORY = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
这个对象是一个ProxyFactory接口,它获取的是这个接口的AdaptiveExtension对象。关于Dubbo的SPI接口的Adaptive对象,它是一种接口的自适应机制。例如ProxyFactory接口的定义
@SPI("javassist")
public interface ProxyFactory {
/**
* create proxy.
*
* @param invoker
* @return proxy
*/
@Adaptive({PROXY_KEY})
<T> T getProxy(Invoker<T> invoker) throws RpcException;
/**
* create proxy.
*
* @param invoker
* @return proxy
*/
@Adaptive({PROXY_KEY})
<T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException;
/**
* create invoker.
*
* @param <T>
* @param proxy
* @param type
* @param url
* @return invoker
*/
@Adaptive({PROXY_KEY})
<T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException;
}
生成的Adaptive对象的每一个方法,会根据Adaptive注解,从参数中获取对应实现在SPI配置中的key,从而获取到对应的实现类,再调用实现类对此方法的实现。比如说这个类的实现
package org.apache.dubbo.rpc;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class ProxyFactory$Adaptive implements org.apache.dubbo.rpc.ProxyFactory {
public org.apache.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1, org.apache.dubbo.common.URL arg2) throws org.apache.dubbo.rpc.RpcException {
if (arg2 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg2;
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getInvoker(arg0, arg1, arg2);
}
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0, boolean arg1) throws org.apache.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0, arg1);
}
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("proxy", "javassist");
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0);
}
}
通过源码看出,getInvoker这个方法,如果没有修改默认配置的话,会获取到配置为javassist为key的SPI实现类。它就是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);
}
};
}
}
为什么这个类叫做JavassistProxyFactory呢?因为getInvoker这个方法的第一步就是获取一个包裹类,这个包裹类是用Javassist这个第三方库实现的。简单地说,就是在程序运行的过程中动态地生成一个类的源代码,这个类是实现了T这个接口类型的,再在运行时将这个类的源码编译成字节码并加载到JVM中以供使用。
那为什么不直接使用这个接口的实现类,而是还要包一层呢?主要是为了对一些异常情况做通用化的处理,这样能减少应用层的工作量。例如
/**
* @param o 实现类的对象
* @param n 方法名
* @param p 参数类型列表
* @param v 参数对象列表
*/
public Object invokeMethod(Object o, String n, Class[] p, Object[] v) throws java.lang.reflect.InvocationTargetException {
org.apache.dubbo.demo.DemoService w;
try{
w = ((org.apache.dubbo.demo.DemoService)$1);
} catch(Throwable e) {
throw new IllegalArgumentException(e);
}
try{
if( "sayHello".equals( $2 ) && $3.length == 1 ) {
return ($w)w.sayHello((java.lang.String)$4[0]);
}
} catch(Throwable e) {
throw new java.lang.reflect.InvocationTargetException(e);
}
throw new org.apache.dubbo.common.bytecode.NoSuchMethodException("Not found method \""+$2+"\" in class org.apache.dubbo.demo.DemoService.");
}
这是对DemoService这个接口的包装类的源码。(请参考Dubbo源码中dubbo-demo中的实例)它首先通过强制类型转换检验了传入实现类是否是对应接口的实现类,之后检验了方法名和参数类型列表长度是否正确,如果正确再调用方法。
获取到包装类之后,利用它生成了一个AbstractProxyInvoker的抽象子类。并实现了它的doInvoker方法,里面正是利用这个包装类进行了调用。这个AbstractProxyInvoker是一个通用的Invoker实现类,实现了一些通用的逻辑,它实现RPC调用的方法invoke
@Override
public Result invoke(Invocation invocation) throws RpcException {
try {
// 调用RPC实现获得结果
Object value = doInvoke(proxy, invocation.getMethodName(), invocation.getParameterTypes(), invocation.getArguments());
// 将结果包装为一个CompletableFuture
CompletableFuture<Object> future = wrapWithFuture(value, invocation);
// 构造一个未完成的RPC调用结果
AsyncRpcResult asyncRpcResult = new AsyncRpcResult(invocation);
// 采用异步编程思维,当future完成时执行传入的函数
future.whenComplete((obj, t) -> {
AppResponse result = new AppResponse();
if (t != null) {
if (t instanceof CompletionException) {
result.setException(t.getCause());
} else {
result.setException(t);
}
} else {
result.setValue(obj);
}
// 结果完成
asyncRpcResult.complete(result);
});
// 这里返回的可能是未完成的结果
return asyncRpcResult;
} catch (InvocationTargetException e) {
if (RpcContext.getContext().isAsyncStarted() && !RpcContext.getContext().stopAsync()) {
logger.error("Provider async started, but got an exception from the original method, cannot write the exception back to consumer because an async result may have returned the new thread.", e);
}
return AsyncRpcResult.newDefaultAsyncResult(null, e.getTargetException(), invocation);
} catch (Throwable e) {
throw new RpcException("Failed to invoke remote proxy method " + invocation.getMethodName() + " to " + getUrl() + ", cause: " + e.getMessage(), e);
}
}
wrapWithFuture方法
private CompletableFuture<Object> wrapWithFuture (Object value, Invocation invocation) {
//
if (RpcContext.getContext().isAsyncStarted()) {
return ((AsyncContextImpl)(RpcContext.getContext().getAsyncContext())).getInternalFuture();
} else if (value instanceof CompletableFuture) {
return (CompletableFuture<Object>) value;
}
return CompletableFuture.completedFuture(value);
}
以上是一个RPC调用在服务端包装之后的Invoker。只要拿到这个对象,已经可以完成一次RPC的最底层调用(也就是调用到实际的接口实现类)。
下面再看这行代码
DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this);
这里传入的invoker参数就是上面获取到的invoker,这里由把它和这个ServiceConfig本身传给DelegateProviderMetaDataInvoker这个类的构造方法。那这个DelegateProviderMetaDataInvoker有什么用呢?
/**
*
* A Invoker wrapper that wrap the invoker and all the metadata (ServiceConfig)
*/
public class DelegateProviderMetaDataInvoker<T> implements Invoker {
protected final Invoker<T> invoker;
private ServiceConfig metadata;
这个类其实就是把ServiceConfig作为元数据也封装了进去,这样如果调用的时候需要这些数据就可以很方便的拿到了。
总结
本文主要是分析了服务端服务发布过程中,在最常用的场景(采用注册中心)的两行重要的代码,也就是生成Invoker。因为发布的过程非常复杂,如果通过一篇文章讲所有内容的话,信息量太大。所以一篇文章只是重点讲一个点。后续会讲解通过protocol发布服务端,包括建立Invoker调用链,建立服务端服务器等内容。
1249


Invoker创建&spm=1001.2101.3001.11974&articleId=96050021&d=1&t=3&u=8caaff0cfb2645bca79ab24a7d77499c)

被折叠的 条评论
为什么被折叠?



