Code for this article: point here
From 16 years came into contact Dubbo, just start to feel amazing, configuration is very simple, seamless and Spring configuration. Various Internet search, Zookeeper + Provider + Consumer, test project up and running, and very happy. But for the source always bring herself to learning, like so many, so many comments in English, scare me. Recently finally mind to come a little bit Debug code finally have a point of receipt, recorded.
Dubbo Profile Demo
Provider Profiles
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="test-dubbo" />
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"/>
<dubbo:protocol name="dubbo" port="${dubbo.registry.port}"/>
<dubbo:service interface="com.redsun.dubbo.provider.ProviderI" ref="providerImpl"/>
</beans>
Properties file configuration
dubbo.registry.address=zookeeper://127.0.0.1:2181
Consumer Profiles
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="test-dubbo-consumer" />
<!-- 使用zookeeper集群注册中心暴露发现服务地址 -->
<dubbo:registry address="${dubbo.registry.address}" timeout="2500" />
<!--<dubbo:protocol host="192.168.30.56" />-->
<dubbo:reference id="providerI" interface="com.redsun.dubbo.provider.ProviderI"/>
</beans>
Properties configuration with Provider
Dubbo configuration file is loaded
Spring loading process is very cumbersome, where powerful? I slowly come to be in the future, in fact, read the this article, for Spring load can be a little understanding, said today Dubbo main configuration file is loaded and parsed.
Spring configuration file to identify Dubbo
In the Spring, when loading a configuration file, the first thing to do is to parse the configuration file, the node is found in the analysis import
of time, it will then read and parse resource
the configuration file information. See the following detailed Debug Spring loaded centering tracking code is not difficult to find, here labeled critical code
After reading the configuration file Dubbo
After reading the file will begin to parse dubbo-provider.xml
the file, see the figure below
Continue with
在解析完元素之后,怎么就将解析权限交给Dubbo.jar
了呢,之前我们所跟踪的代码都是Spring代码中的,此处给出了答案。通过识别元素是Dubbo开头的来获取dubbo-provider.xml
配置文件中的头信息,就是我们常说的Schema,来获取Dubbo中的解析Handler --> DubboNamespaceHandler
。
Spring会获取META-INF/spring.handlers
中的信息,大家也许会对这个文件很陌生,我们来看看这里面的信息
http\://dubbo.apache.org/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
有没有豁然开朗的感觉,在此配置文件中配置了http\://code.alibabatech.com/schema/dubbo
此schema所对应的Handler了,有兴趣的同学可追踪下获取Handler的代码。
注:其实Spring也有相应的META-INF/spring.handlers
文件,里面也有类似的配置信息,有兴趣的同学可以自行跟踪下
Dubbo配置文件解析
好了,在获取到Dubbo的Handler之后又做了什么呢?
DubboNamespaceHandler – Dubbo处理器
这里相信大家就不陌生了吧,此处将Dubbo配置文件中的所有相关的节点都定义了对应的BeanDefinition解析器,此时也就不难想到,在解析到对应的节点时就去获取对应的parsers。
Service的解析
Here let's parse tracking details at Service node.
According to think before we begin resolved, and read the service node information for the corresponding operation.
What does it work?
In DubboNamespaceHandler
the init
process we know that the corresponding service beanClass
is ServiceBean
the aim here is to get all beanClass of reflection is set by a single public method parameter, and see whether there was a corresponding configuration in the configuration file, if there is provided to the beanDefinition
object . Then the object eventually exist where? it's here!
This line of code is too subtle, and careful not hard to find. Spring source familiar should know that there is a BeanMap Bean used to store all the information.
This, in the configuration file service configuration finally transformed into an in-memory objects, and also completed the process of resolution.
to sum up
Always bring herself to look at the source code before, difficult, and more, every retreat. Get down with it, but also to learn more details in the course of the familiar code. For example, when using a classpath configuration path in the configuration file, if ":" no intention to write a "/" and have little impact, Spring code subString of.
Read the source code, on the road, there will be follow-up ~