手写RPC框架第一章自定义配置xml

43次阅读

共计 8769 个字符,预计需要花费 22 分钟才能阅读完成。

案例介绍
本案例通过三个章节来实现一共简单的 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

代码示例

itstack-demo-rpc-01
└── src
    └── main
    │    ├── java
    │    │   └── org.itstack.demo.rpc.config
    │    │        ├── spring
    │    │        │   ├── bean
    │    │        │   │   ├── ConsumerBean.java
    │    │        │   │   ├── ProviderBean.java
    │    │        │   │   └── ServerBean.java 
    │    │        │   ├── MyBeanDefinitionParser.java    
    │    │        │   └── MyNamespaceHandler.java
    │    │        ├── ConsumerConfig.java    
    │    │        ├── ProviderConfig.java    
    │    │        └── ProviderConfig.java    
    │      └── resource
    │        └── META-INF
    │            ├── rpc.xsd
    │            ├── spring.handlers
    │            └── spring.schemas    
    └── test
         ├── java
         │   └── org.itstack.demo.test
         │       ├── service
         │       │   ├── impl
         │        │   │   └── HelloServiceImpl.java  
         │        │   └── HelloService.java
         │       └── ApiTest.java                 
         └── resource  
             ├── itstack-rpc-consumer.xml         
             ├── itstack-rpc-provider.xml
             └── log4j.xml

ProviderConfig.java

public class ProviderConfig {

    private String nozzle; // 接口
    private String ref;    // 映射
    private String alias;  // 别名

    // 发布
    protected void doExport() {System.out.format("生产者信息 => [接口:%s] [映射:%s] [别名:%s] \r\n", nozzle, ref, alias);
    }

    public String getNozzle() {return nozzle;}

    public void setNozzle(String nozzle) {this.nozzle = nozzle;}

    public String getRef() {return ref;}

    public void setRef(String ref) {this.ref = ref;}

    public String getAlias() {return alias;}

    public void setAlias(String alias) {this.alias = alias;}
}

ProviderBean.java

public class ProviderBean extends ProviderConfig implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 发布生产者
        doExport();}

}

MyBeanDefinitionParser.java

public class MyBeanDefinitionParser implements BeanDefinitionParser {

    private final Class<?> beanClass;

    MyBeanDefinitionParser(Class<?> beanClass) {this.beanClass = beanClass;}

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);
        String beanName = element.getAttribute("id");
        parserContext.getRegistry().registerBeanDefinition(beanName, beanDefinition);

        for (Method method : beanClass.getMethods()) {if (!isProperty(method, beanClass)) continue;
            String name = method.getName();
            String methodName = name.substring(3, 4).toLowerCase() + name.substring(4);
            String value = element.getAttribute(methodName);
            beanDefinition.getPropertyValues().addPropertyValue(methodName, value);
        }

        return beanDefinition;
    }

    private boolean isProperty(Method method, Class beanClass) {String methodName = method.getName();
        boolean flag = methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1;
        Method getter = null;
        if (!flag) return false;

        Class<?> type = method.getParameterTypes()[0];
        try {getter = beanClass.getMethod("get" + methodName.substring(3));
        } catch (NoSuchMethodException ignore) { }

        if (null == getter) {
            try {getter = beanClass.getMethod("is" + methodName.substring(3));
            } catch (NoSuchMethodException ignore) {}}

        flag = getter != null && Modifier.isPublic(getter.getModifiers()) && type.equals(getter.getReturnType());

        return flag;

    }

}

MyNamespaceHandler.java

public class MyNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {registerBeanDefinitionParser("consumer", new MyBeanDefinitionParser(ConsumerBean.class));
        registerBeanDefinitionParser("provider", new MyBeanDefinitionParser(ProviderBean.class));
        registerBeanDefinitionParser("server", new MyBeanDefinitionParser(ServerBean.class));
    }

}

rpc.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://rpc.itstack.org/schema/rpc"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            targetNamespace="http://rpc.itstack.org/schema/rpc"
            elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans"/>

    <!-- org.itstack.demo.rpc.config.ServerConfig -->
    <xsd:element name="server">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="host" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 栈台地点]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                    <xsd:attribute name="port" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 栈台岸口]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <!-- org.itstack.demo.rpc.config.ConsumerConfig -->
    <xsd:element name="consumer">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="nozzle" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 接口名称]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                    <xsd:attribute name="alias" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 服务别名分组信息]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <!-- org.itstack.demo.rpc.config.ProviderConfig -->
    <xsd:element name="provider">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="nozzle" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 接口名称]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                    <xsd:attribute name="ref" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 接口实现类]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                    <xsd:attribute name="alias" type="xsd:string">
                        <xsd:annotation>
                            <xsd:documentation><![CDATA[ 服务别名分组信息]]></xsd:documentation>
                        </xsd:annotation>
                    </xsd:attribute>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

spring.handlers

http\://rpc.itstack.org/schema/rpc=org.itstack.demo.rpc.config.spring.MyNamespaceHandler

spring.schemas

http\://rpc.itstack.org/schema/rpc/rpc.xsd=META-INF/rpc.xsd

测试部分

itstack-rpc-consumer.xml

<?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:rpc="http://rpc.itstack.org/schema/rpc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://rpc.itstack.org/schema/rpc http://rpc.itstack.org/schema/rpc/rpc.xsd">

    <!-- redis 配置,保存链接 -->
    <rpc:server id="consumer_itstack" host="127.0.0.1" port="6379"/>

    <rpc:consumer id="consumer_helloService" nozzle="org.itstack.demo.test.service.HelloService" alias="itStackRpc"/>

</beans>

itstack-rpc-provider.xml

<?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:rpc="http://rpc.itstack.org/schema/rpc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://rpc.itstack.org/schema/rpc http://rpc.itstack.org/schema/rpc/rpc.xsd">

    <rpc:provider id="provider_helloService" nozzle="org.itstack.demo.test.service.HelloService"
                 ref="helloService" alias="itStackRpc" />

</beans>

ApiTest.java

package org.itstack.demo.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * http://www.itstack.org
 * create by fuzhengwei on 2019/5/4
 * 本章节主要介绍如何读取自定义配置 xml 文件字段信息
 */
public class ApiTest {public static void main(String[] args) {String[] configs = {"itstack-rpc-consumer.xml", "itstack-rpc-provider.xml"};
        new ClassPathXmlApplicationContext(configs);
    }

}

测试结果

2019-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
2019-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]
2019-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]
2019-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
服务端信息 => [注册中心地址:127.0.0.1] [注册中心端口:6379] 
生产者信息 => [接口:org.itstack.demo.test.service.HelloService] [映射:helloService] [别名:itStackRpc] 

关注{bugstack 虫洞栈}公众号获取源码,回复 <rpc 源码 >

正文完
 0