我们使用Dubbo时,一般都会使用xml配置基本信息,如项目名称(application)、注册中心(register)、协议(protocal)、服务(service),如下所示:
1
2
3
4
5
6
7
|
< dubbo:application name = "demo-provider" owner = "programmer" organization = "dubbox" />
< dubbo:protocol name = "dubbo" serialization = "kryo" optimizer = "com.alibaba.dubbo.demo.SerializationOptimizerImpl" />
< dubbo:service interface = "com.alibaba.dubbo.demo.bid.BidService" ref = "bidService" protocol = "dubbo" />
|
可是大家这些xml节点都是从哪里来的,为什么dubbo启动时可以识别,为什么输入有提示,下面首先为大家介绍一下xml节点从哪里来,为什么可以自动提示?下一篇再介绍怎样处理xml内容。
一、从哪里来?
首先看xml的beans根目录的URL:http://code.alibabatech.com/schema/dubbo
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "UTF-8" ?>
xsi:schemaLocation="
|
为什么引入这个dubbo的url,在xml文件中就可以自动提示相应的节点信息,我们首先看dubbo源码中的和两个文件:
1、repository\com\alibaba\dubbo\2.5.3\dubbo-2.5.3\META-INF\spring.handlers
图中的代码说明了url对应的处理类
2、repository\com\alibaba\dubbo\2.5.3\dubbo-2.5.3\META-INF\spring.schemas
图中的代码说明了url对应的xsd文件
二、Dubbo.xsd的组成
第一步中解释了xml元素的来源,下一步通过分析dubbo.xsd文件解释xml元素、属性及自动提示。
dubbo.xsd主要包含以下4个类型的元素:
1、xsd:import:引入其它标签库,dubbo.xsd中还需要其它的xsd,处理方式和dubbo.xsd相同
1
2
3
|
|
2、xsd:annotation:xsd文件描述
1
2
3
|
< xsd:annotation >
< xsd:documentation > <![CDATA[ Namespace support for the dubbo services provided by dubbo framework. ]]> </ xsd:documentation >
</ xsd:annotation >
|
3、xsd:complexType name="abstractMethodType:abstract开头的complextType:作为其它complexType类型的基类,提取共同的属性配置
这个抽象complexType只列出了前面两个参数:timeout、retries
1
2
3
4
5
6
7
8
9
10
11
|
< xsd:complexType name = "abstractMethodType" >
< xsd:attribute name = "timeout" type = "xsd:string" use = "optional" default = "0" >
< xsd:annotation >
< xsd:documentation > <![CDATA[ The method invoke timeout. ]]> </ xsd:documentation >
</ xsd:annotation >
</ xsd:attribute >
< xsd:attribute name = "retries" type = "xsd:string" use = "optional" >
< xsd:annotation >
< xsd:documentation > <![CDATA[ The method retry times. ]]> </ xsd:documentation >
</ xsd:annotation >
</ xsd:attribute >
|
4、xsd:element name="service" type="serviceType:某个节点的配置,这里以service节点为例,type属性指向了具体的类型
type属性值说明具体的元素时serviceType
1
2
3
4
5
|
< xsd:element name = "service" type = "serviceType" >
< xsd:annotation >
< xsd:documentation > <![CDATA[ Export service config ]]> </ xsd:documentation >
</ xsd:annotation >
</ xsd:element >
|
5、xsd:complexType name="serviceType":某个节点的具体属性,第4步中type属性指向的节点,其中base属性指向集成的complexType
具体的complexType(部分),包括子元素method、parameter,基类元素abstractServiceType
1
2
3
4
5
6
7
8
|
< xsd:complexType name = "serviceType" >
< xsd:complexContent >
< xsd:extension base = "abstractServiceType" >
< xsd:choice minOccurs = "0" maxOccurs = "unbounded" >
< xsd:element ref = "method" minOccurs = "0" maxOccurs = "unbounded" />
< xsd:element ref = "parameter" minOccurs = "0" maxOccurs = "unbounded" />
< xsd:element ref = "beans:property" minOccurs = "0" maxOccurs = "unbounded" />
</ xsd:choice >
|
三、dubbo.xml配置文件和dubbo.xsd的关系
1、示例一:protocol
1
2
|
< dubbo:protocol name = "dubbo" port = "-1" dispatcher = "all" threadpool = "fixed" threads = "100" />
|
对应的dubbo.xsd节点
2、示例二
1
2
3
|
< dubbo:service interface = "org.gossip.dub.facade.ISeckillService" ref = "seckillService" retries = "0" >
< dubbo:method name = "a1" ></ dubbo:method >
</ dubbo:service >
|