dubbo-exploration journey (6)---combined with spring

In this section, let's see how spring's schema is used in dubbo.

1) Take a look at the dubbo-registry-simple.xml file in dubbo. This file is configured by dubbo's beans in spring based on the schema file defined by dubbo itself.
With the knowledge introduced in the previous section, it is easy to understand by looking at this file now.
<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" //dubbo定义
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">//dubbo-defined XSD file

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="location" value="classpath:dubbo.properties" />
    </bean>
	
    <dubbo:application name="${dubbo.application.name}" owner="${dubbo.application.owner}" />
    
    <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" heartbeat="180000" />
    
    <dubbo:service id="registryServiceConfig" interface="com.alibaba.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000">
        <dubbo:method name="subscribe"><dubbo:argument index="1" callback="true" /></dubbo:method>
        <dubbo:method name="unsubscribe"><dubbo:argument index="1" callback="false" /></dubbo:method>
    </dubbo:service>
    
    <bean id="registryService" class="com.alibaba.dubbo.registry.simple.SimpleRegistryService" />

</beans>

With the above configuration files, there should be a corresponding set of schema reflections.

2) Next, let's take a look at NamespaceHandlerSupport:
In dubbo, DubboNamespaceHandler inherits NamespaceHandlerSupport:
finds its own BeanDefinitionParser according to the node name in the XSD file, and then BeanDefinitionParser completes the specific parsing work.
public class DubboNamespaceHandler extends NamespaceHandlerSupport {

	static {
		Version.checkDuplicate(DubboNamespaceHandler.class);
	}

	public void init() {
	    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));
    }

}

For example: registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
will find the BEAN information configured in the XSD according to the application name, and the DubboBeanDefinitionParser will complete the parsing work.
application node in XSD:
<xsd:element name="application" type="applicationType">
		<xsd:annotation>
			<xsd:documentation><![CDATA[ The application config ]]></xsd:documentation>
		</xsd:annotation>
	</xsd:element>


<xsd:complexType name="applicationType">
		<xsd:attribute name="id" type="xsd:ID">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="name" type="xsd:string" use="required">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application name. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="version" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application version. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="owner" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application owner name (email prefix). ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="organization" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The organization name. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="architecture" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The architecture. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="environment" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application environment, eg: dev/test/run ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="compiler" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The java code compiler. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="logger" type="xsd:string">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application logger. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="registry" type="xsd:string" use="optional">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application registry. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="monitor" type="xsd:string" use="optional">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ The application monitor. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
		<xsd:attribute name="default" type="xsd:string" use="optional">
			<xsd:annotation>
				<xsd:documentation><![CDATA[ Is default. ]]></xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
	</xsd:complexType>


Corresponding bean information:
ApplicationConfig class:
// Application Name
    private String               name;

    // module version
    private String               version;

    // application manager
    private String               owner;

    // organization name (BU or department)
    private String               organization;

    // layer
    private String               architecture;

    // Environment, such as: dev/test/run
    private String               environment;

    // Java code compiler
    private String               compiler;

    // log output method
    private String               logger;

    // registry
    private List<RegistryConfig> registries;

    // service monitoring
    private MonitorConfig        monitor;

    // is it default
    private Boolean              isDefault;


DubboBeanDefinitionParser will naturally implement the BeanDefinitionParser interface.
Writing the value configured in the XSD file to the bean (ApplicationConfig)

is the same parser for each node that dubbo parses, which is a bit debatable .

3) The following should be spring.handlers and spring.schemas.
Specify http://code.alibabatech.com/schema/dubbo/dubbo.xsd for the xsd file
defined in META-INF/spring.schemas
http\:// code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd is defined
in META-INF/spring.handlers
http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo .config.spring.schema.DubboNamespaceHandler

After the introduction of these two sections, you can understand more clearly, the combination of dubbo and spring!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326929712&siteId=291194637