今天將真正去看dubbo內部的實現過程,看dubbo的源碼前我先把dubbo的用戶指南和開發指指南大概的看了一遍,這樣再看dubbo源碼比較輕鬆。從用戶指南和開發指指南能夠找到相應的切入點,今天將介紹的是dubbo的初始化解析bean的過程:java
解析服務
基於dubbo.jar內的META-INF/spring.handlers配置,Spring在遇到dubbo名稱空間時,會回調DubboNamespaceHandler。
全部dubbo的標籤,都統一用DubboBeanDefinitionParser進行解析,基於一對一屬性映射,將XML標籤解析爲Bean對象。
在ServiceConfig.export()或ReferenceConfig.get()初始化時,將Bean對象轉換URL格式,全部Bean屬性轉成URL的參數。
而後將URL傳給Protocol擴展點,基於擴展點的Adaptive機制,根據URL的協議頭,進行不一樣協議的服務暴露或引用。node
dubbo服務的暴露調用的是:ServiceConfig.export()代碼以下:
com.alibaba.dubbo.config.ServiceConfig#exportspring
//暴露服務
public synchronized void export() {
if (provider != null) {
if (export == null) {
export = provider.getExport();
}
if (delay == null) {
delay = provider.getDelay();
}
}
if (export != null && ! export.booleanValue()) {
return;
}
if (delay != null && delay > 0) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(delay);
} catch (Throwable e) {
}
doExport();
}
});
thread.setDaemon(true);
thread.setName("DelayExportServiceThread");
thread.start();
} else {
doExport();
}
} api
在查看export調用鏈時,可看到2個地方調用了該方法:
一、com.alibaba.dubbo.config.spring.AnnotationBean#postProcessAfterInitialization:註解的方式暴露時
二、com.alibaba.dubbo.config.spring.ServiceBean#afterPropertiesSet:以spring配置文件暴露時app
AnnotationBean類的繼承關係
public class AnnotationBean extends AbstractConfig implements DisposableBean, BeanFactoryPostProcessor, BeanPostProcessor, ApplicationContextAware { async
AnnotationBean實現了spring bean和context相關的接口,在spring掃描完註解類,並解析完時調用 export()方法對服務進行暴露ide
ServiceBean
public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware {
在spring初始化解析bean完成,主要是在對spring標籤的解析,bean的定義,bean的屬性解析設值等完成後 進行 export()post
由於dubbo是本身的自定義標籤,因此對於bean的解析是 export 前最重要的部分,今天先不看服務的暴露,先看dubbo對於服務的解析,重要的兩個類:
com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser#parseui
如下爲DubboNamespaceHandler代碼,加上了個人註釋(本身的理解)
public class DubboNamespaceHandler extends NamespaceHandlerSupport {
static {
/**
* 檢索是否有重複的命名空間處理器
*/
Version.checkDuplicate(DubboNamespaceHandler.class);
}
public void init() {
/**
* 註冊bean,真正負解析的是DubboBeanDefinitionParser
* DubboBeanDefinitionParser將解析全部的屬性,並將屬性值放入BeanDefinition
*/
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
}
}
全部的解析工做都在 DubboBeanDefinitionParser 中spa
/**
* 解析dubbo自定義標籤,往BeanDefinition設置屬性值,這個時候bean尚未建立
* @param element
* @param parserContext
* @param beanClass
* @param required
* @return
*/
@SuppressWarnings("unchecked")
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
//設置懶加載爲false,表示當即加載,spring啓動時,馬上進行實例化
//若是設置爲true,那麼要第一次向容器經過getBean索取bean時實例化,在spring bean的配置裏能夠配置
//這裏會設置懶加載爲false其實還能夠獲得一個推斷就是:dubbo標籤建立的bean就是單例bean(singleton bean)
//由於lazy-init的設置只對singleton bean有效,對原型bean(prototype無效)
beanDefinition.setLazyInit(false);
String id = element.getAttribute("id");
//若是沒有設置bean的id
if ((id == null || id.length() == 0) && required) {
String generatedBeanName = element.getAttribute("name");
//name沒有配置
if (generatedBeanName == null || generatedBeanName.length() == 0) {
//若是是ProtocolConfig類型,bean name默認爲 dubbo,其餘的爲配置的interface值
if (ProtocolConfig.class.equals(beanClass)) {
generatedBeanName = "dubbo";
} else {
generatedBeanName = element.getAttribute("interface");
}
}
/*
* 若是還爲null 那麼取 beanClass 的名字,beanClass 其實就是要解析的類型
* 如:com.alibaba.dubbo.config.ApplicationConfig
*/
if (generatedBeanName == null || generatedBeanName.length() == 0) {
generatedBeanName = beanClass.getName();
}
//若是id沒有設置那麼 id = generatedBeanName,若是是ProtocolConfig類型的話天然就是 dubbo
id = generatedBeanName;
int counter = 2;
/*
* 因爲spring的bean id不能重複,但有些標籤可能會配置多個如:<dubbo:registry
* 因此 id 在後面加數字 二、三、4 區分
*/
while(parserContext.getRegistry().containsBeanDefinition(id)) {
id = generatedBeanName + (counter ++);
}
}
if (id != null && id.length() > 0) {
//檢查是否有 bean id 相同的
if (parserContext.getRegistry().containsBeanDefinition(id)) {
throw new IllegalStateException("Duplicate spring bean id " + id);
}
/*
* 註冊 bean 定義
* org.springframework.beans.factory.support.DefaultListableBeanFactory#registerBeanDefinition
* 會按照 id 即beanName作一些檢查,判斷是否重載已加載過的bean等等
* 跟到代碼裏其實 bean 的註冊也是放到 ConcurrentHashMap 裏
* beanName也就是這裏的 id 會放到 list 裏
*/
parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
//給bean添加屬性值
beanDefinition.getPropertyValues().addPropertyValue("id", id);
}
if (ProtocolConfig.class.equals(beanClass)) {
for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
if (property != null) {
Object value = property.getValue();
if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
}
}
}
} else if (ServiceBean.class.equals(beanClass)) { //解析<dubbo:service
String className = element.getAttribute("class");//獲取類全名
if(className != null && className.length() > 0) {
RootBeanDefinition classDefinition = new RootBeanDefinition();
//經過反射獲取類
classDefinition.setBeanClass(ReflectUtils.forName(className));
classDefinition.setLazyInit(false);
/*
解析子節點,有些配置多是:
<dubbo:service interface="com.alihealth.dubbo.api.drugInfo.service.DemoService"
executes="10" >
<property ref="demoService" name="ref"></property>
<property value="1.0.0" name="version"></property>
</dubbo:service>
*/
parseProperties(element.getChildNodes(), classDefinition);
/*
ref直接設置成了 接口名 + Impl 的bean ?
如:com.alihealth.dubbo.api.drugInfo.service.DemoService + Impl 的bean爲啥?
那<dubbo:service裏定義的 ref 屬性有啥用
*/
beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
}
} else if (ProviderConfig.class.equals(beanClass)) {
/*
<dubbo:provider 爲缺省配置 ,因此在解析的時候,若是<dubbo:service有些值沒配置,那麼會用<dubbo:provider值進行填充
*/
parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
} else if (ConsumerConfig.class.equals(beanClass)) {
/*
* 同上
*/
parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
}
Set<String> props = new HashSet<String>();
ManagedMap parameters = null;
for (Method setter : beanClass.getMethods()) {
String name = setter.getName();
//給model注入值時,如ServiceConfig,方法必須是set開頭,而且參數只能爲1
if (name.length() > 3 && name.startsWith("set")
&& Modifier.isPublic(setter.getModifiers())
&& setter.getParameterTypes().length == 1) {
//方法參數類型,由於參數只能是1,因此直接取[0]
Class<?> type = setter.getParameterTypes()[0];
//根據set方法名獲取屬性值,如:setListener 獲得的屬性爲:listener
String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
props.add(property);
Method getter = null;
try {
getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e) {
try {
getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
} catch (NoSuchMethodException e2) {
}
}
if (getter == null
|| ! Modifier.isPublic(getter.getModifiers())
|| ! type.equals(getter.getReturnType())) {
continue;
}
if ("parameters".equals(property)) {
/*
* 若是屬性爲 parameters,如ProtocolConfig裏的setParameters(Map<String, String> parameters)
* 那麼去子節點獲取 <dubbo:parameter
* <dubbo:protocol name="dubbo" host="127.0.0.1" port="9998" accepts="1000" >
<dubbo:parameter key="adsf" value="adf" />
<dubbo:parameter key="errer" value="aerdf" />
</dubbo:protocol>
*/
parameters = parseParameters(element.getChildNodes(), beanDefinition);
} else if ("methods".equals(property)) {
/*
解析 <dubbo:method 並設置 methods 值 --serviceConfig中
*/
parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
} else if ("arguments".equals(property)) {
/*
同上 ,解析<dubbo:argument --- MethodConfig中
*/
parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
} else {
String value = element.getAttribute(property);
if (value != null) {
value = value.trim();
if (value.length() > 0) {
//不發佈到任何註冊中心時 registry = "N/A"
if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
} else if ("registry".equals(property) && value.indexOf(',') != -1) {
//多註冊中心用 , 號分隔
parseMultiRef("registries", value, beanDefinition, parserContext);
} else if ("provider".equals(property) && value.indexOf(',') != -1) {
parseMultiRef("providers", value, beanDefinition, parserContext);
} else if ("protocol".equals(property) && value.indexOf(',') != -1) {
//同上 多協議暴露
parseMultiRef("protocols", value, beanDefinition, parserContext);
} else {
Object reference;
if (isPrimitive(type)) {//若是參數類型爲 java 的基本類型
if ("async".equals(property) && "false".equals(value)
|| "timeout".equals(property) && "0".equals(value)
|| "delay".equals(property) && "0".equals(value)
|| "version".equals(property) && "0.0.0".equals(value)
|| "stat".equals(property) && "-1".equals(value)
|| "reliable".equals(property) && "false".equals(value)) {
/*
兼容舊版本xsd中的default值,以上配置的值在xsd中有配置defalt值
<xsd:attribute name="version" type="xsd:string" use="optional" default="0.0.0">
*/
value = null;
}
reference = value;
} else if ("protocol".equals(property)
//若是屬性爲 protocol 那麼要判斷protocol對應的拓展點配置有沒有
&& ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)
//檢查當前使用的協議是否已經解析過 可能在這裏被解析過<dubbo:protocol name="dubbo"
&& (! parserContext.getRegistry().containsBeanDefinition(value)
|| ! ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
if ("dubbo:provider".equals(element.getTagName())) {
logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
}
// 兼容舊版本配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName(value);
reference = protocol;
} else if ("monitor".equals(property)
//同上
&& (! parserContext.getRegistry().containsBeanDefinition(value)
|| ! MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
// 兼容舊版本配置
reference = convertMonitor(value);
} else if ("onreturn".equals(property)) {
//回調方法 相似onSuccess
int index = value.lastIndexOf(".");
// bean的名字
String returnRef = value.substring(0, index);
String returnMethod = value.substring(index + 1);
reference = new RuntimeBeanReference(returnRef);
beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
} else if ("onthrow".equals(property)) {
//回調 異常執行的方法 ,相似 onError
int index = value.lastIndexOf(".");
String throwRef = value.substring(0, index);
String throwMethod = value.substring(index + 1);
reference = new RuntimeBeanReference(throwRef);
beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
} else {
if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
/*
必須是單例bean(singleton),原型bean(prototype)不行,sevice初始化一次,在spring容器裏也只有一個 實例
是否是和dubbo的冪等有關,若是爲原型bean,那麼服務就變成有狀態的了
*/
if (! refBean.isSingleton()) {
throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value+ "\" scope=\"singleton\" ...>");
}
}
reference = new RuntimeBeanReference(value);
}
/*
設置屬性,值爲另一個關聯的bean
RuntimeBeanReference 固定佔位符類,當在beanfactory中做爲另一個bean的引用時,做爲屬性值對象,將在運行時進行解析
*/
beanDefinition.getPropertyValues().addPropertyValue(property, reference);
}
}
}
}
}
}
NamedNodeMap attributes = element.getAttributes();
int len = attributes.getLength();
for (int i = 0; i < len; i++) {
Node node = attributes.item(i);
String name = node.getLocalName();
//通過上面的解析,若是還有一些屬性沒有解析到的
if (! props.contains(name)) {
if (parameters == null) {
parameters = new ManagedMap();
}
String value = node.getNodeValue();
parameters.put(name, new TypedStringValue(value, String.class));
}
}
if (parameters != null) {
beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
}
return beanDefinition;
}
private static final Pattern GROUP_AND_VERION = Pattern.compile("^[\\-.0-9_a-zA-Z]+(\\:[\\-.0-9_a-zA-Z]+)?$");
protected static MonitorConfig convertMonitor(String monitor) {
if (monitor == null || monitor.length() == 0) {
return null;
}
if (GROUP_AND_VERION.matcher(monitor).matches()) {
String group;
String version;
int i = monitor.indexOf(':');
if (i > 0) {
group = monitor.substring(0, i);
version = monitor.substring(i + 1);
} else {
group = monitor;
version = null;
}
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setGroup(group);
monitorConfig.setVersion(version);
return monitorConfig;
}
return null;
}
private static boolean isPrimitive(Class<?> cls) {
return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class
|| cls == Character.class || cls == Short.class || cls == Integer.class
|| cls == Long.class || cls == Float.class || cls == Double.class
|| cls == String.class || cls == Date.class || cls == Class.class;
}
@SuppressWarnings("unchecked")
private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition,
ParserContext parserContext) {
//解析 registries 、providers、protocols 時支持多引用
String[] values = value.split("\\s*[,]+\\s*");
ManagedList list = null;
for (int i = 0; i < values.length; i++) {
String v = values[i];
if (v != null && v.length() > 0) {
if (list == null) {
list = new ManagedList();
}
list.add(new RuntimeBeanReference(v));
}
}
beanDefinition.getPropertyValues().addPropertyValue(property, list);
}
private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass,
boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
NodeList nodeList = element.getChildNodes();
if (nodeList != null && nodeList.getLength() > 0) {
boolean first = true;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
if (tag.equals(node.getNodeName())
|| tag.equals(node.getLocalName())) {
if (first) {
first = false;
String isDefault = element.getAttribute("default");
/*
若是 <dubbo:provider 標籤沒有配置default開關,那麼直接設置 default = "false"
這樣作的目的是爲了讓 <dubbo:provider裏的配置都只是 <dubbo:service 或 <dubbo:reference的默認或缺省配置
*/
if (isDefault == null || isDefault.length() == 0) {
beanDefinition.getPropertyValues().addPropertyValue("default", "false");
}
}
BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
if (subDefinition != null && ref != null && ref.length() > 0) {
subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
}
}
}
}
}
}
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) {
if (nodeList != null && nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
//若是是 <property 元素
if ("property".equals(node.getNodeName())
|| "property".equals(node.getLocalName())) {
String name = ((Element) node).getAttribute("name");
if (name != null && name.length() > 0) {
String value = ((Element) node).getAttribute("value");
//獲取 ref
String ref = ((Element) node).getAttribute("ref");
if (value != null && value.length() > 0) {
beanDefinition.getPropertyValues().addPropertyValue(name, value);
} else if (ref != null && ref.length() > 0) {
beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref));
} else {
/*
只支持兩種property的設置方法:
<property ref="" name="">
<property value="" name="">
*/
throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />");
}
}
}
}
}
}
}
@SuppressWarnings("unchecked")
private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) {
if (nodeList != null && nodeList.getLength() > 0) {
ManagedMap parameters = null;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
//解析 <dubbo:parameter
if ("parameter".equals(node.getNodeName())
|| "parameter".equals(node.getLocalName())) {
if (parameters == null) {
parameters = new ManagedMap();
}
String key = ((Element) node).getAttribute("key");
String value = ((Element) node).getAttribute("value");
boolean hide = "true".equals(((Element) node).getAttribute("hide"));
if (hide) {
key = Constants.HIDE_KEY_PREFIX + key;
}
//添加參數,String 類型
parameters.put(key, new TypedStringValue(value, String.class));
}
}
}
return parameters;
}
return null;
}
@SuppressWarnings("unchecked")
private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
ParserContext parserContext) {
if (nodeList != null && nodeList.getLength() > 0) {
ManagedList methods = null;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
Element element = (Element) node;
//<dubbo:method
if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {
String methodName = element.getAttribute("name");
if (methodName == null || methodName.length() == 0) {
throw new IllegalStateException("<dubbo:method> name attribute == null");
}
if (methods == null) {
methods = new ManagedList();
}
//解析 <dubbo:method MethodConfig
BeanDefinition methodBeanDefinition = parse(((Element) node),
parserContext, MethodConfig.class, false);
String name = id + "." + methodName;
BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(
methodBeanDefinition, name);
methods.add(methodBeanDefinitionHolder);
}
}
}
if (methods != null) {
beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
}
}
}
解析的最終目的是返回 RootBeanDefinition 對象,RootBeanDefinition包含了解析出來的關於bean的全部信息,注意在bean的解析完後其實只是spring將其轉化成spring內部的一種抽象的數據對象結構,bean的建立(實例化)是第一次調用 getBean 時建立的。以上是dubbo對配置文件,服務定義的解析過程。後面再寫dubbo服務的暴露。
關注我獲取spring java dubbo 視頻