结束了集群容错
和服务发布原理
这两个小专题以后,有朋友问我服务引用
何时开始,本篇为服务引用
的启蒙篇.以前是一直和你们一块儿看源码,鉴于Talk is cheap.Show me your code
,因此本篇将和你们一块儿写写代码.php
dubbo的原理是怎么样的?请简单谈谈java
有没有考虑过本身实现一个相似dubbo的RPC框架,若是有,请问你会若是着手实现?(面试高频题,区分度高)
面试
你说你用过mybatis,那你知道Mapper接口的原理吗?(若是回答得不错,而且提到动态代理这个关键词会继续往下问,那这个动态代理又是如何经过依赖注入到Mapper接口的呢?)spring
谈到dubbo的原理,咱们就必须首先要知道,dubbo的基本概念,通俗的说,就是dubbo是干吗的设计模式
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案服务器
在此以前,就必需要讲讲如下几个简单又容易混淆的概念网络
同一个业务,部署在多个服务器上mybatis
一个业务分拆多个子业务,部署在不一样的服务器上架构
RPC(Remote Procedure Call Protocol)---远程过程调用app
咱们捕捉到几个重要的关键词,分布式
,透明化
,RPC
.
既然各服务是部署在不一样的服务器上,那服务间的调用就是要经过网络通讯,简单的用图描述以下:
以前在[dubbo源码解析-本地暴露]的时候就有不少朋友留言问到,这个本地暴露有什么用.首先,dubbo做为一个被普遍运用的框架,点滴的性能提高,那么受益者都是很大一个数量.这也就是为何JDK的源码,都喜欢用位运算
.好比图中的UserService
和RoleService
服务是在同一模块内的,他们直接的通讯经过JVM性能确定要比经过网络通讯要好得多.这就是为何dubbo在设计上,既有远程暴露
,又有本地暴露
的缘由.
既然涉及到了网络通讯,那么服务消费者调用服务以前,都要写各类网络请求,编解码之类的相关代码,明显是很不友好的.dubbo所说的透明
,就是指,让调用者对网络请求,编解码之类的细节透明,让咱们像调用本地服务同样调用远程服务,甚至感受不到本身在调用远程服务.
说了这么多,那到底怎么作?要实现这个需求,咱们很容易想到一个关键词,那就是动态代理
public interface MenuService {
void sayHello();
}
复制代码
public class MenuServiceImpl implements MenuService{
@Override
public void sayHello() {
}
}
复制代码
public class ProxyFactory implements InvocationHandler {
private Class interfaceClass;
public ProxyFactory(Class interfaceClass) {
this.interfaceClass = interfaceClass;
}
//返回代理对象,此处用泛型为了调用时不用强转,用Object须要强转
public <T> T getProxyObject(){
return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(),//类加载器
new Class[]{interfaceClass},//为哪些接口作代理(拦截哪些方法)
this);//(把这些方法拦截到哪处理)
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method);
System.out.println("进行编码");
System.out.println("发送网络请求");
System.out.println("将网络请求结果进行解码并返回");
return null;
}
}
复制代码
public void test() throws Exception {
ProxyFactory proxyFactor = new ProxyFactory(MenuService.class);
MenuService menuService = proxyFactor.getProxyObject();
menuService.sayHello();
//输出结果以下:
//public abstract void com.toby.rpc.MenuService.sayHello()
//进行编码
//发送网络请求
//将网络请求结果进行解码并返回
}
复制代码
看到这里可能有朋友要吐槽了,我都看了你几个月的源码解析了,上面说的那些我早就懂了,那我还关注肥朝公众号
干吗.我要的是整出一个相似dubbo的框架,性能上差点不要紧,至少外观使用上要差很少,好比咱们平时使用dubbo都是先在配置文件配置这么个东西
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
复制代码
而后用在用@Autowired
依赖注入来使用的,说白了,逼格要有!
咱们假如要写一个简单的RPC,就取名叫tobyRPC
(肥朝英文名为toby),其实我我的是比较喜欢截图,可是部分朋友和我反复强调贴代码,那这里我就贴代码吧
1.设计配置属性和JavaBean
public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean {
@Override
public Object getObject() throws Exception {
return get();
}
@Override
public Class<?> getObjectType() {
return getInterfaceClass();
}
@Override
public boolean isSingleton() {
return true;
}
}
复制代码
public class ReferenceConfig<T> {
private Class<?> interfaceClass;
// 接口代理类引用
private transient volatile T ref;
public synchronized T get() {
if (ref == null) {
init();
}
return ref;
}
private void init() {
ref = new ProxyFactory(interfaceClass).getProxyObject();
}
public Class<?> getInterfaceClass() {
return interfaceClass;
}
public void setInterfaceClass(Class<?> interfaceClass) {
this.interfaceClass = interfaceClass;
}
}
复制代码
2.编写XSD文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://toby.com/schema/tobyRPC" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace="http://toby.com/schema/tobyRPC">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:complexType name="referenceType">
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="interface" type="xsd:token" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[ The service interface class name. ]]></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type="java.lang.Class"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="reference" type="referenceType">
<xsd:annotation>
<xsd:documentation><![CDATA[ Reference service config ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:schema>
复制代码
3.编写NamespaceHandler
和BeanDefinitionParser
完成解析工做
public class TobyRPCBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
protected Class getBeanClass(Element element) {
return ReferenceBean.class;
}
protected void doParse(Element element, BeanDefinitionBuilder bean) {
String interfaceClass = element.getAttribute("interface");
if (StringUtils.hasText(interfaceClass)) {
bean.addPropertyValue("interfaceClass", interfaceClass);
}
}
}
复制代码
public class TobyRPCNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("reference", new TobyRPCBeanDefinitionParser());
}
}
复制代码
4.编写spring.handlers
和spring.schemas
串联起全部部件
spring.handlers
http\://toby.com/schema/tobyRPC=com.toby.config.TobyRPCNamespaceHandler
复制代码
spring.schemas
http\://toby.com/schema/tobyRPC.xsd=META-INF/tobyRPC.xsd
复制代码
5.建立配置文件
<?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:tobyRPC="http://toby.com/schema/tobyRPC" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://toby.com/schema/tobyRPC http://toby.com/schema/tobyRPC.xsd">
<tobyRPC:reference id="menuService" interface="com.toby.rpc.MenuService" />
</beans>
复制代码
demo结构截图以下:
万事俱备,那咱们跑个单元测试看看
运行结果如咱们所料.可是具体要怎么编码,怎么发送请求,又如何解码好像也没说啊.没说?没说就对了.在完结服务引用
这个小专题后,还会重点和你们看一下dubbo中的编解码
,spi
,javassist
等重点内容源码,等粗略把整个框架的思想都掌握后,再手把手临摹一个五脏俱全(包含设计模式,dubbo架构设计)的简易dubbo框架.总之一句话,关注肥朝公众号便可.
为何面试都喜欢问原理,难道都是为了装逼?固然不是,明白了原理,不少东西都是一通百通的.咱们来看mybatis的这道面试题.首先Mapper接口的原理,能够参考我以前的图解源码 | MyBatis的Mapper原理 ,其实说白了,就是给Mapper接口注入一个代理对象,而后动态代理对象调用方法会被拦截到invoke
中,而后在这个invoke
方法中,作了一些不可描述的事情(老司机能够尽情YY).而这一切的前提,都是要无声无息的把动态代理对象注入进去.其实注入进去的原理和dubbo也是同样的,咱们简单看两个图