Source depth Dubbo - Dubbo configuration file parsing

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/red_sheeps/article/details/85239416

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 importof time, it will then read and parse resourcethe configuration file information. See the following detailed Debug Spring loaded centering tracking code is not difficult to find, here labeled critical code
Spring configuration file parsing

After reading the configuration file Dubbo

After reading the file will begin to parse dubbo-provider.xmlthe file, see the figure below
Custom parsing configuration files

Continue with

在解析完元素之后,怎么就将解析权限交给Dubbo.jar了呢,之前我们所跟踪的代码都是Spring代码中的,此处给出了答案。通过识别元素是Dubbo开头的来获取dubbo-provider.xml配置文件中的头信息,就是我们常说的Schema,来获取Dubbo中的解析Handler --> DubboNamespaceHandler
Parsing the specific elements
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 element parser
这里相信大家就不陌生了吧,此处将Dubbo配置文件中的所有相关的节点都定义了对应的BeanDefinition解析器,此时也就不难想到,在解析到对应的节点时就去获取对应的parsers。

Service的解析

Here let's parse tracking details at Service node.
Analytical service
According to think before we begin resolved, and read the service node information for the corresponding operation.
What does it work?
ServiceBean resolution
In DubboNamespaceHandlerthe initprocess we know that the corresponding service beanClassis ServiceBeanthe 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 beanDefinitionobject . Then the object eventually exist where? it's here!
Sign bean definition
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.
Here Insert Picture Description
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 ~

Guess you like

Origin blog.csdn.net/red_sheeps/article/details/85239416