1. Overview
dubbo is a simple and easy-to-use RPC framework. Through simple providers, consumers can complete insensible network calls. So how do you expose the provider's services in dubbo, and how do consumers obtain the provider-related information?
2. The integration of dubbo and spring
Understanding dubboService Registration and Service DiscoveryBefore, we first need to master a knowledge point:Custom Schema in Spring
3. Spring custom schema
Dubbo's current design is completely non-invasive, which means that users only rely on configuration contracts. In Dubbo, you can use XML to configure the relevant information, or use it to introduce or export services.
After the configuration is completed, the project is started, Spring will read the configuration file and generate the injection-related beans.So how does Dubbo implement custom XML read by Spring?
Starting with Spring 2.0,Spring begins to provide an XML Schema format extension mechanism for defining and configuring beans
Beginner Cases
Learn and useSpring XML Schema The expansion mechanism is not difficult, and the following steps are required:
1. Create a JavaBean object with configuration properties
2. Create an XML Schema file to describe a custom legal building block, that is, the xsd file
3. Customize the processor class and implement the NamespaceHandler interface
4. Customize the parser to implement the BeanDefinitionParser interface (the most critical part)
5. Write and file all components
Define JavaBean object, in spring this object will be automatically created according to the configuration
public class User {
private String id;
private String name;
private Integer age;
//Omit the getter setter method
}
definitionl Configuration file and import corresponding constraints
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="http:///2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:util="/schema/util"
xmlns:task="/schema/task"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:itheima="/schema/user"
xsi:schemaLocation="/schema/beans /schema/beans/
/schema/context /schema/context/
/schema/util /schema/util/
/schema/task /schema/task/
/schema/aop /schema/aop/
/schema/tx /schema/tx/
/schema/user /schema/">
<itheima:user name="zhangsan" age="12"></itheima:user>
</beans>
definitionFile, according toxsi:schemaLocation="/schema/user" andNamespaceHandler classcorrespondence; must be placed under classpathMETA-INFIn the folder
http\:///schema/user=
CustomizeUserNamespaceHandler class
package ;
import ;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
BeanDefinitionParserIt is the parser corresponding to the tag. Spring will use this class to parse when reading the corresponding tag;
public class UserBeanDefinitionParser extends
AbstractSingleBeanDefinitionParser {
protected Class getBeanClass(Element element) {
return ;
}
protected void doParse(Element element, BeanDefinitionBuilder bean) {
String name = ("name");
String age = ("age");
String id = ("id");
if ((id)) {
("id", id);
}
if ((name)) {
("name", name);
}
if ((age)) {
("age", (age));
}
}
}
definitionfile, based on xsi:schemaLocation="/schema/" Correspondence with file; must be placed in the META-INF folder under classpath.
http\:///schema/=META-INF/
existMETA-INFDefinition belowdocument,Rules used to describe tags using xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="/schema/user"
xmlns:xsd="http:///2001/XMLSchema"
xmlns:beans="/schema/beans"
targetNamespace="/schema/user"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="/schema/beans" />
<xsd:element name="user">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="age" type="xsd:int" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>