摘要:是蚂蚁金服开源的一个框架,相比它没有这么多历史的包袱,代码更加简洁,设计思路更加清晰,更加容易去理解其中的代码。
原文链接:https://www.cnblogs.com/luozh...
这几天离职在家,正好没事可以疯狂的输出一下,本来想写DUBBO的源码解析的,但是发现写DUBBO源码的太多了,所以找一个写的不那么多的框架,所以就选中SOFARPC这个框架了。
SOFARPC是蚂蚁金服开源的一个RPC框架,相比DUBBO它没有这么多历史的包袱,代码更加简洁,设计思路更加清晰,更加容易去理解其中的代码。
那么为什么要去重写原生的SPI呢?官方给出了如下解释:
按需加载
可以有别名
可以有优先级进行排序和覆盖
可以控制是否单例
可以在某些场景下使用编码
可以指定扩展配置位置
可以排斥其他扩展点
整个流程如下:
我们以ConsumerBootstrap为例:
先要有一个抽象类:
@Extensible(singleton = false) public abstract class ConsumerBootstrap{ .... }
指定扩展实现类:
@Extension("sofa") public class DefaultConsumerBootstrapextends ConsumerBootstrap { ... }
扩展描述文件META-INF/services/sofa-rpc/com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap
sofa=com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap
当这些准备完成后,直接调用即可。
ConsumerBootstrap sofa = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension("sofa");
接下来我们看看ExtensionLoaderFactory的源码
/** * All extension loader {Class : ExtensionLoader} * 这个map里面装的是所有ExtensionLoader */ private static final ConcurrentMapLOADER_MAP = new ConcurrentHashMap (); public static ExtensionLoader getExtensionLoader(Class clazz, ExtensionLoaderListener listener) { ExtensionLoader loader = LOADER_MAP.get(clazz); if (loader == null) { //get不到则加上锁 synchronized (ExtensionLoaderFactory.class) { //防止其他线程操作再get一次 loader = LOADER_MAP.get(clazz); if (loader == null) { loader = new ExtensionLoader (clazz, listener); LOADER_MAP.put(clazz, loader); } } } return loader; }
然后我们看一下ExtensionLoader这个类的构造器
protected ExtensionLoader(ClassinterfaceClass, boolean autoLoad, ExtensionLoaderListener listener) { //如果正在执行关闭,则将属性置空后直接返回 if (RpcRunningState.isShuttingDown()) { this.interfaceClass = null; this.interfaceName = null; this.listener = null; this.factory = null; this.extensible = null; this.all = null; return; } // 接口为空,既不是接口,也不是抽象类 if (interfaceClass == null || !(interfaceClass.isInterface() || Modifier.isAbstract(interfaceClass.getModifiers()))) { throw new IllegalArgumentException("Extensible class must be interface or abstract class!"); } //当前加载的接口类名 this.interfaceClass = interfaceClass; //接口名字 this.interfaceName = ClassTypeUtils.getTypeStr(interfaceClass); this.listener = listener; //接口上必须要有Extensible注解 Extensible extensible = interfaceClass.getAnnotation(Extensible.class); if (extensible == null) { throw new IllegalArgumentException( "Error when load extensible interface " + interfaceName + ", must add annotation @Extensible."); } else { this.extensible = extensible; } // 如果是单例,那么factory不为空 this.factory = extensible.singleton() ? new ConcurrentHashMap () : null; //这个属性里面是这个接口的所有实现类 this.all = new ConcurrentHashMap >(); if (autoLoad) { //获取到扩展点加载的路径 List paths = RpcConfigs.getListValue(RpcOptions.EXTENSION_LOAD_PATH); for (String path : paths) { //根据路径加载文件 loadFromFile(path); } } }
拿到所有的扩展点加载的路径后进入到loadFromFile中进行文件的加载
protected synchronized void loadFromFile(String path) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Loading extension of extensible {} from path: {}", interfaceName, path); } // 默认如果不指定文件名字,就是接口名 String file = StringUtils.isBlank(extensible.file()) ? interfaceName : extensible.file().trim(); String fullFileName = path + file; try { ClassLoader classLoader = ClassLoaderUtils.getClassLoader(getClass()); loadFromClassLoader(classLoader, fullFileName); } catch (Throwable t) { if (LOGGER.isErrorEnabled()) { LOGGER.error("Failed to load extension of extensible " + interfaceName + " from path:" + fullFileName, t); } } } protected void loadFromClassLoader(ClassLoader classLoader, String fullFileName) throws Throwable { Enumerationurls = classLoader != null ? classLoader.getResources(fullFileName) : ClassLoader.getSystemResources(fullFileName); // 可能存在多个文件。 if (urls != null) { while (urls.hasMoreElements()) { // 读取一个文件 URL url = urls.nextElement(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Loading extension of extensible {} from classloader: {} and file: {}", interfaceName, classLoader, url); } BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); String line; while ((line = reader.readLine()) != null) { readLine(url, line); } } catch (Throwable t) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Failed to load extension of extensible " + interfaceName + " from classloader: " + classLoader + " and file:" + url, t); } } finally { if (reader != null) { reader.close(); } } } } }
接下来进入到readLine,这个方法主要是读取prop文件里面的每一行记录,并加载该实现类的类文件校验完后将文件添加到all属性中
protected void readLine(URL url, String line) { //读取文件里面的一行记录,并将这行记录用=号分割 String[] aliasAndClassName = parseAliasAndClassName(line); if (aliasAndClassName == null || aliasAndClassName.length != 2) { return; } //别名 String alias = aliasAndClassName[0]; //包名 String className = aliasAndClassName[1]; // 读取配置的实现类 Class tmp; try { tmp = ClassUtils.forName(className, false); } catch (Throwable e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Extension {} of extensible {} is disabled, cause by: {}", className, interfaceName, ExceptionUtils.toShortString(e, 2)); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extension " + className + " of extensible " + interfaceName + " is disabled.", e); } return; } if (!interfaceClass.isAssignableFrom(tmp)) { throw new IllegalArgumentException("Error when load extension of extensible " + interfaceName + " from file:" + url + ", " + className + " is not subtype of interface."); } Class extends T> implClass = (Class extends T>) tmp; // 检查是否有可扩展标识 Extension extension = implClass.getAnnotation(Extension.class); if (extension == null) { throw new IllegalArgumentException("Error when load extension of extensible " + interfaceName + " from file:" + url + ", " + className + " must add annotation @Extension."); } else { String aliasInCode = extension.value(); if (StringUtils.isBlank(aliasInCode)) { // 扩展实现类未配置@Extension 标签 throw new IllegalArgumentException("Error when load extension of extensible " + interfaceClass + " from file:" + url + ", " + className + ""s alias of @Extension is blank"); } if (alias == null) { // spi文件里没配置,用代码里的 alias = aliasInCode; } else { // spi文件里配置的和代码里的不一致 if (!aliasInCode.equals(alias)) { throw new IllegalArgumentException("Error when load extension of extensible " + interfaceName + " from file:" + url + ", aliases of " + className + " are " + "not equal between " + aliasInCode + "(code) and " + alias + "(file)."); } } // 接口需要编号,实现类没设置 if (extensible.coded() && extension.code() < 0) { throw new IllegalArgumentException("Error when load extension of extensible " + interfaceName + " from file:" + url + ", code of @Extension must >=0 at " + className + "."); } } // 不可以是default和* if (StringUtils.DEFAULT.equals(alias) || StringUtils.ALL.equals(alias)) { throw new IllegalArgumentException("Error when load extension of extensible " + interfaceName + " from file:" + url + ", alias of @Extension must not "default" and "*" at " + className + "."); } // 检查是否有存在同名的 ExtensionClass old = all.get(alias); ExtensionClassextensionClass = null; if (old != null) { // 如果当前扩展可以覆盖其它同名扩展 if (extension.override()) { // 如果优先级还没有旧的高,则忽略 if (extension.order() < old.getOrder()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Extension of extensible {} with alias {} override from {} to {} failure, " + "cause by: order of old extension is higher", interfaceName, alias, old.getClazz(), implClass); } } else { if (LOGGER.isInfoEnabled()) { LOGGER.info("Extension of extensible {} with alias {}: {} has been override to {}", interfaceName, alias, old.getClazz(), implClass); } // 如果当前扩展可以覆盖其它同名扩展 extensionClass = buildClass(extension, implClass, alias); } } // 如果旧扩展是可覆盖的 else { if (old.isOverride() && old.getOrder() >= extension.order()) { // 如果已加载覆盖扩展,再加载到原始扩展 if (LOGGER.isInfoEnabled()) { LOGGER.info("Extension of extensible {} with alias {}: {} has been loaded, ignore origin {}", interfaceName, alias, old.getClazz(), implClass); } } else { // 如果不能被覆盖,抛出已存在异常 throw new IllegalStateException( "Error when load extension of extensible " + interfaceClass + " from file:" + url + ", Duplicate class with same alias: " + alias + ", " + old.getClazz() + " and " + implClass); } } } else { extensionClass = buildClass(extension, implClass, alias); } if (extensionClass != null) { // 检查是否有互斥的扩展点 for (Map.Entry > entry : all.entrySet()) { ExtensionClass existed = entry.getValue(); if (extensionClass.getOrder() >= existed.getOrder()) { // 新的优先级 >= 老的优先级,检查新的扩展是否排除老的扩展 String[] rejection = extensionClass.getRejection(); if (CommonUtils.isNotEmpty(rejection)) { for (String rej : rejection) { existed = all.get(rej); if (existed == null || extensionClass.getOrder() < existed.getOrder()) { continue; } ExtensionClass removed = all.remove(rej); if (removed != null) { if (LOGGER.isInfoEnabled()) { LOGGER.info( "Extension of extensible {} with alias {}: {} has been reject by new {}", interfaceName, removed.getAlias(), removed.getClazz(), implClass); } } } } } else { String[] rejection = existed.getRejection(); if (CommonUtils.isNotEmpty(rejection)) { for (String rej : rejection) { if (rej.equals(extensionClass.getAlias())) { // 被其它扩展排掉 if (LOGGER.isInfoEnabled()) { LOGGER.info( "Extension of extensible {} with alias {}: {} has been reject by old {}", interfaceName, alias, implClass, existed.getClazz()); return; } } } } } } loadSuccess(alias, extensionClass); } }
加载完文件后我们再回到
ConsumerBootstrap sofa = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension("sofa");
进入到getExtension方法中
public ExtensionClassgetExtensionClass(String alias) { return all == null ? null : all.get(alias); } public T getExtension(String alias) { //从all属性中拿到加载的class ExtensionClass extensionClass = getExtensionClass(alias); if (extensionClass == null) { throw new SofaRpcRuntimeException("Not found extension of " + interfaceName + " named: "" + alias + ""!"); } else { //在加载class的时候,校验了是否是单例,如果是单例,那么factory不为null if (extensible.singleton() && factory != null) { T t = factory.get(alias); if (t == null) { synchronized (this) { t = factory.get(alias); if (t == null) { //实例化 t = extensionClass.getExtInstance(); //放入到factory,单例的class下次直接拿就好了,不需要重新创建 factory.put(alias, t); } } } return t; } else { //实例化 return extensionClass.getExtInstance(); } } }
我们进入到ExtensionClass看看getExtInstance方法
/** * 服务端实例对象(只在是单例的时候保留) * 用volatile修饰,保证了可见性 */ private volatile transient T instance; /** * 得到服务端实例对象,如果是单例则返回单例对象,如果不是则返回新创建的实例对象 * * @param argTypes 构造函数参数类型 * @param args 构造函数参数值 * @return 扩展点对象实例 ext instance */ public T getExtInstance(Class[] argTypes, Object[] args) { if (clazz != null) { try { if (singleton) { // 如果是单例 if (instance == null) { synchronized (this) { if (instance == null) { //通过反射创建实例 instance = ClassUtils.newInstanceWithArgs(clazz, argTypes, args); } } } return instance; // 保留单例 } else { //通过反射创建实例 return ClassUtils.newInstanceWithArgs(clazz, argTypes, args); } } catch (Exception e) { throw new SofaRpcRuntimeException("create " + clazz.getCanonicalName() + " instance error", e); } } throw new SofaRpcRuntimeException("Class of ExtensionClass is null"); }
看完了SOFARPC的扩展类实现后感觉代码写的非常的整洁,逻辑非常的清晰,里面有很多可以学习的地方,比如线程安全用到了双重检查锁和volatile保证可见性。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/76179.html
摘要:二注解该注解为了保证在内部调用具体实现的时候不是硬编码来指定引用哪个实现,也就是为了适配一个接口的多种实现,这样做符合模块接口设计的可插拔原则,也增加了整个框架的灵活性,该注解也实现了扩展点自动装配的特性。 Dubbo扩展机制SPI 前一篇文章《dubbo源码解析(一)Hello,Dubbo》是对dubbo整个项目大体的介绍,而从这篇文章开始,我将会从源码来解读dubbo再各个模块的实...
摘要:在蚂蚁金服内部是被所有在线应用的使用的服务调用框架,截止年双十一,已经被蚂蚁多个系统所使用,生产环境发布的服务数量超过了个。 原文地址:http://www.sohu.com/a/2288043... 我们很高兴地宣布,今天蚂蚁金服启动分布式中间件(Scalable Open Financial Architecture,以下简称 SOFA 中间件)的开源计划! SOFA 是蚂蚁金服自...
摘要:今天我想聊聊的另一个很棒的特性就是它的可扩展性。的扩展机制在的官网上,描述自己是一个高性能的框架。接下来的章节中我们会慢慢揭开扩展机制的神秘面纱。扩展扩展点的实现类。的定义在配置文件中可以看到文件中定义了个的扩展实现。 摘要: 在Dubbo的官网上,Dubbo描述自己是一个高性能的RPC框架。今天我想聊聊Dubbo的另一个很棒的特性, 就是它的可扩展性。 Dubbo的扩展机制 在Dub...
阅读 3295·2021-11-12 10:36
阅读 2444·2021-11-02 14:43
阅读 2126·2019-08-30 14:23
阅读 3444·2019-08-30 13:08
阅读 906·2019-08-28 18:09
阅读 3109·2019-08-26 12:22
阅读 3081·2019-08-23 18:24
阅读 1992·2019-08-23 18:17