手写RPC框架第一章《自定义配置xml》
作者:付政委
栈山航海,逾沙轶漠之贡,府无虚月
微信公众号:bugstack虫洞栈 | 欢迎关注&获取源码
专注于原创专题案例沉淀技术,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x实战专题案例、用Java实现JVM、基于JavaAgent的全链路监控、手写RPC框架等,其他更多内容还在排兵布阵中。
案例介绍
本案例通过三个章节来实现一共简单的rpc框架,用于深入学习rpc框架是如何通信的,当前章节主要介绍如何自定义xml文件并进行解析。想解析自定义的xml首先定义自己的xsd文件,并且实现spring的NamespaceHandlerSupport、BeanDefinitionParser,两个方法进行处理。
远程过程调用协议
RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。
RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。Dubbo是 [1] 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 [2] Spring框架无缝集成。
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
环境准备
1、jdk 1.8.0
2、IntelliJ IDEA Community Edition 2018.3.1 x64
代码示例
1itstack-demo-rpc-01
2└── src
3 └── main
4 │ ├── java
5 │ │ └── org.itstack.demo.rpc.config
6 │ │ ├── spring
7 │ │ │ ├── bean
8 │ │ │ │ ├── ConsumerBean.java
9 │ │ │ │ ├── ProviderBean.java
10 │ │ │ │ └── ServerBean.java
11 │ │ │ ├── MyBeanDefinitionParser.java
12 │ │ │ └── MyNamespaceHandler.java
13 │ │ ├── ConsumerConfig.java
14 │ │ ├── ProviderConfig.java
15 │ │ └── ProviderConfig.java
16 │ └── resource
17 │ └── META-INF
18 │ ├── rpc.xsd
19 │ ├── spring.handlers
20 │ └── spring.schemas
21 └── test
22 ├── java
23 │ └── org.itstack.demo.test
24 │ ├── service
25 │ │ ├── impl
26 │ │ │ └── HelloServiceImpl.java
27 │ │ └── HelloService.java
28 │ └── ApiTest.java
29 └── resource
30 ├── itstack-rpc-consumer.xml
31 ├── itstack-rpc-provider.xml
32 └── log4j.xml
ProviderConfig.java
1public class ProviderConfig {
2
3 private String nozzle; //接口
4 private String ref; //映射
5 private String alias; //别名
6
7 //发布
8 protected void doExport() {
9 System.out.format("生产者信息=> [接口:%s] [映射:%s] [别名:%s] \r\n", nozzle, ref, alias);
10 }
11
12 public String getNozzle() {
13 return nozzle;
14 }
15
16 public void setNozzle(String nozzle) {
17 this.nozzle = nozzle;
18 }
19
20 public String getRef() {
21 return ref;
22 }
23
24 public void setRef(String ref) {
25 this.ref = ref;
26 }
27
28 public String getAlias() {
29 return alias;
30 }
31
32 public void setAlias(String alias) {
33 this.alias = alias;
34 }
35}
ProviderBean.java
1public class ProviderBean extends ProviderConfig implements ApplicationContextAware {
2
3
4 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
5 //发布生产者
6 doExport();
7 }
8
9}
MyBeanDefinitionParser.java
1public class MyBeanDefinitionParser implements BeanDefinitionParser {
2
3 private final Class<?> beanClass;
4
5 MyBeanDefinitionParser(Class<?> beanClass) {
6 this.beanClass = beanClass;
7 }
8
9
10 public BeanDefinition parse(Element element, ParserContext parserContext) {
11
12 RootBeanDefinition beanDefinition = new RootBeanDefinition();
13 beanDefinition.setBeanClass(beanClass);
14 beanDefinition.setLazyInit(false);
15 String beanName = element.getAttribute("id");
16 parserContext.getRegistry().registerBeanDefinition(beanName, beanDefinition);
17
18 for (Method method : beanClass.getMethods()) {
19 if (!isProperty(method, beanClass)) continue;
20 String name = method.getName();
21 String methodName = name.substring(3, 4).toLowerCase() + name.substring(4);
22 String value = element.getAttribute(methodName);
23 beanDefinition.getPropertyValues().addPropertyValue(methodName, value);
24 }
25
26 return beanDefinition;
27 }
28
29 private boolean isProperty(Method method, Class beanClass) {
30
31 String methodName = method.getName();
32 boolean flag = methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1;
33 Method getter = null;
34 if (!flag) return false;
35
36 Class<?> type = method.getParameterTypes()[0];
37 try {
38 getter = beanClass.getMethod("get" + methodName.substring(3));
39 } catch (NoSuchMethodException ignore) {
40
41 }
42
43 if (null == getter) {
44 try {
45 getter = beanClass.getMethod("is" + methodName.substring(3));
46 } catch (NoSuchMethodException ignore) {
47
48 }
49 }
50
51 flag = getter != null && Modifier.isPublic(getter.getModifiers()) && type.equals(getter.getReturnType());
52
53 return flag;
54
55 }
56
57}
MyNamespaceHandler.java
1public class MyNamespaceHandler extends NamespaceHandlerSupport {
2
3
4 public void init() {
5 registerBeanDefinitionParser("consumer", new MyBeanDefinitionParser(ConsumerBean.class));
6 registerBeanDefinitionParser("provider", new MyBeanDefinitionParser(ProviderBean.class));
7 registerBeanDefinitionParser("server", new MyBeanDefinitionParser(ServerBean.class));
8 }
9
10}
rpc.xsd
1
2<xsd:schema xmlns="http://rpc.itstack.org/schema/rpc"
3 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
4 xmlns:beans="http://www.springframework.org/schema/beans"
5 targetNamespace="http://rpc.itstack.org/schema/rpc"
6 elementFormDefault="qualified" attributeFormDefault="unqualified">
7 <xsd:import namespace="http://www.springframework.org/schema/beans"/>
8
9 <!-- org.itstack.demo.rpc.config.ServerConfig -->
10 <xsd:element name="server">
11 <xsd:complexType>
12 <xsd:complexContent>
13 <xsd:extension base="beans:identifiedType">
14 <xsd:attribute name="host" type="xsd:string">
15 <xsd:annotation>
16 <xsd:documentation><![CDATA[ 栈台地点 ]]></xsd:documentation>
17 </xsd:annotation>
18 </xsd:attribute>
19 <xsd:attribute name="port" type="xsd:string">
20 <xsd:annotation>
21 <xsd:documentation><![CDATA[ 栈台岸口 ]]></xsd:documentation>
22 </xsd:annotation>
23 </xsd:attribute>
24 </xsd:extension>
25 </xsd:complexContent>
26 </xsd:complexType>
27 </xsd:element>
28
29 <!-- org.itstack.demo.rpc.config.ConsumerConfig -->
30 <xsd:element name="consumer">
31 <xsd:complexType>
32 <xsd:complexContent>
33 <xsd:extension base="beans:identifiedType">
34 <xsd:attribute name="nozzle" type="xsd:string">
35 <xsd:annotation>
36 <xsd:documentation><![CDATA[ 接口名称 ]]></xsd:documentation>
37 </xsd:annotation>
38 </xsd:attribute>
39 <xsd:attribute name="alias" type="xsd:string">
40 <xsd:annotation>
41 <xsd:documentation><![CDATA[ 服务别名分组信息 ]]></xsd:documentation>
42 </xsd:annotation>
43 </xsd:attribute>
44 </xsd:extension>
45 </xsd:complexContent>
46 </xsd:complexType>
47 </xsd:element>
48
49 <!-- org.itstack.demo.rpc.config.ProviderConfig -->
50 <xsd:element name="provider">
51 <xsd:complexType>
52 <xsd:complexContent>
53 <xsd:extension base="beans:identifiedType">
54 <xsd:attribute name="nozzle" type="xsd:string">
55 <xsd:annotation>
56 <xsd:documentation><![CDATA[ 接口名称 ]]></xsd:documentation>
57 </xsd:annotation>
58 </xsd:attribute>
59 <xsd:attribute name="ref" type="xsd:string">
60 <xsd:annotation>
61 <xsd:documentation><![CDATA[ 接口实现类 ]]></xsd:documentation>
62 </xsd:annotation>
63 </xsd:attribute>
64 <xsd:attribute name="alias" type="xsd:string">
65 <xsd:annotation>
66 <xsd:documentation><![CDATA[ 服务别名分组信息 ]]></xsd:documentation>
67 </xsd:annotation>
68 </xsd:attribute>
69 </xsd:extension>
70 </xsd:complexContent>
71 </xsd:complexType>
72 </xsd:element>
73</xsd:schema>
spring.handlers
1http\://rpc.itstack.org/schema/rpc=org.itstack.demo.rpc.config.spring.MyNamespaceHandler
spring.schemas
1http\://rpc.itstack.org/schema/rpc/rpc.xsd=META-INF/rpc.xsd
测试部分
itstack-rpc-consumer.xml
1
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpc="http://rpc.itstack.org/schema/rpc"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://rpc.itstack.org/schema/rpc http://rpc.itstack.org/schema/rpc/rpc.xsd">
6
7 <!-- redis配置,保存链接 -->
8 <rpc:server id="consumer_itstack" host="127.0.0.1" port="6379"/>
9
10 <rpc:consumer id="consumer_helloService" nozzle="org.itstack.demo.test.service.HelloService" alias="itStackRpc"/>
11
12</beans>
itstack-rpc-provider.xml
1
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpc="http://rpc.itstack.org/schema/rpc"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://rpc.itstack.org/schema/rpc http://rpc.itstack.org/schema/rpc/rpc.xsd">
6
7 <rpc:provider id="provider_helloService" nozzle="org.itstack.demo.test.service.HelloService"
8 ref="helloService" alias="itStackRpc" />
9
10</beans>
ApiTest.java
1package org.itstack.demo.test;
2
3import org.springframework.context.support.ClassPathXmlApplicationContext;
4
5/**
6 * http://www.itstack.org
7 * create by fuzhengwei on 2019/5/4
8 * 本章节主要介绍如何读取自定义配置xml文件字段信息
9 */
10public class ApiTest {
11
12 public static void main(String[] args) {
13 String[] configs = {"itstack-rpc-consumer.xml", "itstack-rpc-provider.xml"};
14 new ClassPathXmlApplicationContext(configs);
15 }
16
17}
测试结果
12019-05-07 19:44:24,805 main INFO [org.springframework.context.support.ClassPathXmlApplicationContext:prepareRefresh:510] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@299a06ac: startup date [Tue May 07 19:44:24 CST 2019]; root of context hierarchy
22019-05-07 19:44:24,872 main INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader:loadBeanDefinitions:315] - Loading XML bean definitions from class path resource [itstack-rpc-consumer.xml]
32019-05-07 19:44:24,972 main INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader:loadBeanDefinitions:315] - Loading XML bean definitions from class path resource [itstack-rpc-provider.xml]
42019-05-07 19:44:25,008 main INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory:preInstantiateSingletons:577] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@192b07fd: defining beans [consumer_itstack,consumer_helloService,provider_helloService]; root of factory hierarchy
5服务端信息=> [注册中心地址:127.0.0.1] [注册中心端口:6379]
6生产者信息=> [接口:org.itstack.demo.test.service.HelloService] [映射:helloService] [别名:itStackRpc]
《Netty4.x专题案例》👇喜欢就点下在看吧