前言

本周空闲时间利用了百分之六七十的样子。主要将Dubbo官网文档和本地代码debug结合起来学习,基本看完了服务导出、服务引入以及服务调用的过程,暂未涉及路由、字典等功能。下面对这一周的收获进行一下总结梳理。

一、基于事件驱动的服务导出

提起服务导出,不要被它的名字误导了,通俗点说就是服务的暴露和注册。服务的暴露是指将服务端的端口开放,等待消费端来连接。服务的注册即将服务信息注册到注册中心。针对服务暴露和注册的具体流程,可参见博主之前的一篇文章  https://www.cnblogs.com/zzq6032010/p/11275478.html ,讲述的比较详细,暂不赘述。

注重提一下的是Dubbo启动服务暴露和注册的时机,是采用的事件驱动来触发的,跟SpringBoot有点神似。这种通过事件驱动来触发特定逻辑的方式,在实际开发工作中也可以灵活使用。

二、服务引入及SPI

对于Dubbo的SPI自适应扩展,可参见博主之前的一篇文章 https://www.cnblogs.com/zzq6032010/p/11219611.html,但此篇文章当时写的比较浅显,还未悟得全部。

下面以Protocol类为例,看一下在ServiceConfig类中的成员变量  Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension() 是什么样子。

 package org.apache.dubbo.rpc;
import org.apache.dubbo.common.extension.ExtensionLoader;
public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol { public void destroy() {
throw new UnsupportedOperationException("The method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
} public int getDefaultPort() {
throw new UnsupportedOperationException("The method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
} public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
org.apache.dubbo.common.URL url = arg0.getUrl();
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.export(arg0);
} public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
if (arg1 == null) throw new IllegalArgumentException("url == null");
org.apache.dubbo.common.URL url = arg1;
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
return extension.refer(arg0, arg1);
} public java.util.List getServers() {
throw new UnsupportedOperationException("The method public default java.util.List org.apache.dubbo.rpc.Protocol.getServers() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
}
}

这就是getAdaptiveExtension()之后得到的代理类,可见在初始化ServiceConfig时先获取的protocol只是一个代理Protocol类,程序运行时再通过传入的Url来判断具体使用哪个Protocol实现类。这才是SPI自适应扩展的精髓所在。

除此之外,在通过getExtension方法获取最终实现类时,还要经过wrapper类的包装。详见ExtensionLoader类中的如下方法:

 private T createExtension(String name) {
Class<?> clazz = getExtensionClasses().get(name);
if (clazz == null) {
throw findException(name);
}
try {
T instance = (T) EXTENSION_INSTANCES.get(clazz);
if (instance == null) {
EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
instance = (T) EXTENSION_INSTANCES.get(clazz);
}
injectExtension(instance);
Set<Class<?>> wrapperClasses = cachedWrapperClasses;
if (CollectionUtils.isNotEmpty(wrapperClasses)) {
for (Class<?> wrapperClass : wrapperClasses) {
instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
}
}
initExtension(instance);
return instance;
} catch (Throwable t) {
throw new IllegalStateException("Extension instance (name: " + name + ", class: " +
type + ") couldn't be instantiated: " + t.getMessage(), t);
}
}

如果接口存在包装类,则在第16行进行wrapper类的处理,将当前instance封装进包装类中,再返回包装类的实例,即通过这一行代码实现了扩展类的装饰器模式改造。

此处同样以Protocol类为例,Url中的协议是registry,那么我最终执行到RegistryProtocol的export方法时栈调用路径是这样的:

即中间经过了三层Wrapper的封装,每层都有自己特定的功能,且各层之间互不影响。Dubbo在很多自适应扩展接口处加了类似这样的装饰扩展,程序的可扩展设计还可以这样玩,Interesting!

服务引入的流程大体是这样的:消费端从注册中心获取服务端信息,封装成Invoker,再封装成代理类注入消费端Spring容器。流程比较简单,可自行根据上一节的内容debug调试。

三、服务调用的疑问

之前未看Dubbo源码时一直有一个疑问:dubbo的消费端代理类调用服务端接口进行消费时,是通过netty将消息发送过去的,服务端在接收到消息后,是如何调用的服务端目标类中的方法?反射吗?反射可以调用到方法,但是没法解决依赖的问题,而且正常情况服务端调用应该也是Spring容器中已经实例化好的的服务对象,那是如何通过netty的消息找到Spring中的对象的?

实际dubbo处理的很简单,只要在服务暴露的时候将暴露的服务自己存起来就好了,等消费端传过来消息的时候,直接去map里面取,取到的就是Spring中封装的那个服务对象,very easy。

                                    服务调用过程草图

如上图所示,服务调用的流程大体是这样的:调用之后通过client远程连接到server,在server端维护了暴露服务的一个map,服务端接收到请求后去map获取Exporter,exporter中有服务端封装好的Invoker,持有Spring中的服务bean,最终完成调用。中间还涉及很多细节,比如netty的封装与调用,序列化反序列化,负载均衡和容错处理等。

小结

Dubbo作为一个优秀的rpc服务框架,其优势不止在于它的rpc过程,还在于更多细节模块的实现以及可扩展的设计,比如序列化处理、负载均衡、容错、netty的线程调度、路由、字典...   内容挺多的,后面打算针对dubbo的四大负载均衡算法做一下研究,浅尝辄止,不求甚解!

【2020-03-28】Dubbo源码杂谈的更多相关文章

  1. dubbo面试题,会这些说明你真正看懂了dubbo源码

    整理了一些dubbo可能会被面试的面试题,感觉非常不错.如果你基本能回答说明你看懂了dubbo源码,对dubbo了解的足够全面.你可以尝试看能不能回答下.我们一起看下有哪些问题吧? 1.dubbo中& ...

  2. dubbo源码分析6-telnet方式的管理实现

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  3. dubbo源码分析1-reference bean创建

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  4. dubbo源码分析2-reference bean发起服务方法调用

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  5. dubbo源码分析3-service bean的创建与发布

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  6. dubbo源码分析4-基于netty的dubbo协议的server

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  7. dubbo源码分析5-dubbo的扩展点机制

    dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...

  8. dubbo源码之四——服务发布二

    dubbo版本:2.5.4 2. 服务提供者暴露一个服务的详细过程 上图是服务提供者暴露服务的主过程: 首先ServiceConfig类拿到对外提供服务的实际类ref(如:HelloWorldImpl ...

  9. dubbo源码之二——dubbo入口

    dubbo源码版本:2.5.4 dubbo-contaner-api com.alibaba.dubbo.container dubbo-demo-consumer com.alibaba.dubbo ...

随机推荐

  1. 使用apache mail发送邮件错误解决办法

    今天在写发送邮件的程序时发现了以下两个些错误,贴出来跟大家分享分享 希望对大家有帮助. 错误一: Exception in thread "main" java.lang.NoCl ...

  2. MySQL之单表多表查询

    #1.单表查询 #单表查询语法 select <字段1,字段2....> from <表名> where <表达式> group by field 分组 havin ...

  3. LeetCode 278.First Bad Version(E)(P)

    题目: You are a product manager and currently leading a team to develop a new product. Unfortunately, ...

  4. Service Mesh - gRPC 本地联调远程服务

    Description Service Mesh 架构下,服务间调用会通过服务名(Service Name)互相调用,比如在 Kubernetes .Docker Swarm 集群中,服务 IP 均由 ...

  5. Web 认证配置流程

    AC配置 Radius配置 Portal配置

  6. String字符串位置移动

    有规律的String字符串位置移动 1.自定义一个有规律的String字符串 String numstr = "1,2,3,x,y,4,5"; 2.按逗号拆分numstr字符串 S ...

  7. 使用JS检测自定义协议是否存在

    [该博客是拼接他人的,原因我们这边PC的开发人员问我,有没有关于js某个对象直接能检测手机或者电脑的自定义协议的,我上网搜了下,貌似移动端的解决比较多] 最终解决方案:还是需要github上面大神写的 ...

  8. AlphaGo、人工智能、深度学习解读以及应用

    经过比拼,AlphaGo最终还是胜出,创造了人机大战历史上的一个新的里程碑.几乎所有的人都在谈论这件事情,这使得把“人工智能”.“深度学习”的热潮推向了新的一个高潮.AlphaGo就像科幻电影里具有人 ...

  9. WEB渗透 - HTTP协议基础

    年初八 星灯花 https只能提高传输层安全 每一次客户端和服务端的通信都是独立的过程 cookie包括了sessionID和其他信息 重要的header S - C Set-Cookie:服务器发给 ...

  10. OpenGL 实践之贝塞尔曲线绘制

    说到贝塞尔曲线,大家肯定都不陌生,网上有很多关于介绍和理解贝塞尔曲线的优秀文章和动态图. 以下两个是比较经典的动图了. 二阶贝塞尔曲线: 三阶贝塞尔曲线: 由于在工作中经常要和贝塞尔曲线打交道,所以简 ...