前言
远程过程调用协议 RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。 RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。
生产者配置
*/
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;
}
}
public class ProviderBean extends ProviderConfig implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//发布生产者
doExport();
}
}
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;
}
}
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"/>
<!-- 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>
<!-- 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>
<!-- 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>
<?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="HelloService" alias="itStackRpc"/>
</beans>
<?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="HelloService"
ref="helloService" alias="itStackRpc" />
</beans>
public class ApiTest {
public static void main(String[] args) {
String[] configs = {"itstack-rpc-consumer.xml", "itstack-rpc-provider.xml"};
new ClassPathXmlApplicationContext(configs);
}
}
直接测试上述的ApiTest测试类即可
好了到这里就结束了netty之手写rpc框架-自定义配置xml的学习,大家一定要跟着动手操作起来。需要的源码的 可si我获取;