Dubb本身具有很强的扩展性:协议有多种实现,注册中心和底层通信框架也有多种选择; 我们下面就梳理下dubbo的核心主流程。主要包括三个子流程:consumer启动,provider的启动,接口调用流程。
另外注意:注册中心我们选择zookeeper,协议基于dubbo,通信使用netty进行讲解。
整体流程还是比较复杂的,读源码的时候我习惯先面后点的方式,可以抓住主要链路把流程分析清楚,再扣细节进行点的突破。
核心流程图,后面就核心流程的一些说明:
1.在系统启动的时候,根据Spring shemas读取对应的配置,通过DubboBeanDefinitionParser解析相应的属性,放入到内存中,给后续服务暴露提供元数据。
DubboNamespaceHandler-init()
registerBeanDefinitionParser(“service”, new DubboBeanDefinitionParser(ServiceBean.class, true));
2.我们看Config模块中
2.1 ServiceConfig 【export>>doExport>>doExportUrls>>doExportUrlsFor1Protocol】
2.2 获取代理类,通过动态代理获取Invoker
proxyFactory.getInvoker
ProxyFactory$Adaptive.getInvoker【注意这个类是DUBBBO SPI动态生成的类】
JavassistProxyFactory.getInvoker(arg0, arg1, arg2)
2.3 进行invoker的发布[分为本地和远程的服务发布,]
protocol.export(wrapperInvoker)
Protocol$Adaptive.export 【注意这个类是DUBBBO SPI动态生成的类】
2.3.1 进入本地的发布动作【本地的比较简单,忽略,只是做了一个 exporterMap.put(key, exporter)】
2.3.2 进入远程的发布动作
2.3.2.1 RegistryProtocol.export(final Invoker originInvoker)
2.3.2.1.1 进行DubboProtocol的发布动作
RegistryProtocol.doLocalExport(originInvoker)
Protocol$Adaptive.export
DubboProtocol.export
2.3.2.1.1.1 将invoker放入exporterMap中,这一步是前面所有步骤的最终结果。非常关键的一步
exporterMap.put(key, exporter)key为 com.alibaba.dubbo.demo.DemoService:20880 value是invoker
2.3.2.1.1.2 打开连接,默认是使用NettyServer
DubboProtocol.openServer(url)
DubboProtocol.createServer(URL url)
Exchangers.bind(url, requestHandler);
HeaderExchanger.bind(URL url, ExchangeHandler handler)
Transporters.bind
NettyServer.doOpen
ServerBootstrap.bind
几个动态生产类【可以拷贝到对应目录,进行源码的debug】
ProxyFactory$Adaptive类:
package com.alibaba.dubbo.rpc;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class ProxyFactory$Adaptive implements com.alibaba.dubbo.rpc.ProxyFactory {
public com.alibaba.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1,
com.alibaba.dubbo.common.URL arg2) throws com.alibaba.dubbo.rpc.RpcException {
if (arg2 == null)
throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg2;
String extName = url.getParameter("proxy", "javassist");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url("
+ url.toString() + ") use keys([proxy])");
com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader
.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getInvoker(arg0, arg1, arg2);
}
public java.lang.Object getProxy(com.alibaba.dubbo.rpc.Invoker arg0, boolean arg1)
throws com.alibaba.dubbo.rpc.RpcException {
if (arg0 == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");
com.alibaba.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("proxy", "javassist");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url("
+ url.toString() + ") use keys([proxy])");
com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader
.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0, arg1);
}
public java.lang.Object getProxy(com.alibaba.dubbo.rpc.Invoker arg0) throws com.alibaba.dubbo.rpc.RpcException {
if (arg0 == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");
com.alibaba.dubbo.common.URL url = arg0.getUrl();
String extName = url.getParameter("proxy", "javassist");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.ProxyFactory) name from url("
+ url.toString() + ") use keys([proxy])");
com.alibaba.dubbo.rpc.ProxyFactory extension = (com.alibaba.dubbo.rpc.ProxyFactory) ExtensionLoader
.getExtensionLoader(com.alibaba.dubbo.rpc.ProxyFactory.class).getExtension(extName);
return extension.getProxy(arg0);
}
}
Protocol$Adaptive:
package com.alibaba.dubbo.rpc;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class Protocol$Adaptive implements com.alibaba.dubbo.rpc.Protocol {
public void destroy() {
throw new UnsupportedOperationException(
"method public abstract void com.alibaba.dubbo.rpc.Protocol.destroy() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public int getDefaultPort() {
throw new UnsupportedOperationException(
"method public abstract int com.alibaba.dubbo.rpc.Protocol.getDefaultPort() of interface com.alibaba.dubbo.rpc.Protocol is not adaptive method!");
}
public com.alibaba.dubbo.rpc.Invoker refer(java.lang.Class arg0, com.alibaba.dubbo.common.URL arg1)
throws com.alibaba.dubbo.rpc.RpcException {
if (arg1 == null)
throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg1;
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url("
+ url.toString() + ") use keys([protocol])");
com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol) ExtensionLoader
.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.refer(arg0, arg1);
}
public com.alibaba.dubbo.rpc.Exporter export(com.alibaba.dubbo.rpc.Invoker arg0)
throws com.alibaba.dubbo.rpc.RpcException {
if (arg0 == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null)
throw new IllegalArgumentException("com.alibaba.dubbo.rpc.Invoker argument getUrl() == null");
com.alibaba.dubbo.common.URL url = arg0.getUrl();
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.Protocol) name from url("
+ url.toString() + ") use keys([protocol])");
com.alibaba.dubbo.rpc.Protocol extension = (com.alibaba.dubbo.rpc.Protocol) ExtensionLoader
.getExtensionLoader(com.alibaba.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.export(arg0);
}
}