motan扩展机制

简介: motan第二篇,想写motan的rpc调用过程的,但项目中需要对motan进行扩展,所以本来就先记录下

motan第二篇,想写motan的rpc调用过程的,但项目中需要对motan进行扩展,所以本来就先记录下


引导


在一个框架或者在项目中提供编写一些服务时,会出现这种情况,会有一些默认的,但你知道只是很容易满足基本的个人需求,开发人员可能会实现扩展,想自己实现定义一个实现

处理方式

  1. 提供一个设置实现类的setter,开发者在初始化时调用一下
  2. 提供配置入口,给个密钥,配置上自定义类名
  3. 类似slf4j一样,提供桥接类


SPI


motan使用了spi的方式

SPI(Service Provider Interface),服务提供接口;也是一种服务发现机制

系统里抽象的各个模块,往往有很多不同的实现方案,
比如日志模块的方案,xml解析模块、jdbc模块的方案等。面向的对象的设计里,
我们一般推荐模块之间基于接口编程,模块之间不对实现类进行硬编码
。一旦代码里涉及具体的实现类,就违反了可拔插的原则,
如果需要替换一种实现,就需要修改代码。
为了实现在模块装配的时候能不在程序里动态指明,这就需要一种服务发现机制。
java spi就是提供这样的一个机制:为某个接口寻找服务实现的机制。
有点类似IOC的思想,就是将装配的控制权移到程序之外,在模块化设计中这个机制尤其重要。

SPI与API的区别

What is the difference between Service Provider Interface (SPI) and Application Programming Interface (API)?
More specifically, for Java libraries, what makes them an API and/or SPI?the API is the description of classes/interfaces/methods/... 
that you call and use to achieve a goalthe SPI is the description of classes/interfaces/methods/... 
that you extend and implement to achieve a goal
Put differently, the API tells you what a specific class/method does for you and the SPI tells you what you must do to conform.
Sometimes SPI and API overlap. 
For example in JDBC the Driver class is part of the SPI: 
If you simply want to use JDBC, you don't need to use it directly, but everyone who implements a JDBC driver must implement that class.
The Connection interface on the other hand is both SPI and API: 
You use it routinely when you use a JDBC driver and it needs to be implemented by the developer of the JDBC driver。


JDK SPI


使用jdk自带的ServiceLoader写一个示例:

一个接口

public interface Spi {    public void provide();
}

实现类

public class DefaultSpi implements Spi{    @Override
    public void provide() {
        System.out.println("默认spi实现");
    }
}

输入类

/**
 * 一个spi的demo
 *
 */
public class SpiDemoMain{
    public static void main( String[] args )
    {        ServiceLoader<Spi> spiServiceLoader = ServiceLoader.load(Spi.class);        Iterator<Spi> spiIterator = spiServiceLoader.iterator();        while ( spiIterator.hasNext()) {
            spiIterator.next().provide();
        }
    }
}

在META文件夹里面新建个服务文件夹,在services-IN文件夹里面新建一个com.jFk.spi.Spi文件

完整的代码可从https://github.com/zhuxingsheng/spidemo下载

ServiceLoader源码解析

原理很简单,一个类实现这个SPI机制

它的本源,也就是从某个地方加载服务实现类,文件名是服务接口名

定义文件的地方

private static final String PREFIX = "META-INF/services/";

类内部使用了延迟加载LazyIterator,在使用到了实现类的时候,才去实例化。

private S nextService() {            if (!hasNextService())                throw new NoSuchElementException();
            String cn = nextName;
            nextName = null;            Class<?> c = null;            try {
                c = Class.forName(cn, false, loader);
            } catch (ClassNotFoundException x) {
                fail(service,                     "Provider " + cn + " not found");
            }            if (!service.isAssignableFrom(c)) {
                fail(service,                     "Provider " + cn  + " not a subtype");
            }            try {            // 调用next方法时,才实例化
                S p = service.cast(c.newInstance());
                providers.put(cn, p);                return p;
            } catch (Throwable x) {
                fail(service,                     "Provider " + cn + " could not be instantiated",
                     x);
            }            throw new Error();          // This cannot happen
        }


莫坦斯皮


motan的spi,跟java spi差不多,但做了一些加大

先看官方文档

  1. 实现SPI扩展点接口
  2. 类添加注解 @Spi(scope = Scope.SINGLETON) //扩展形式,单例或多例 @SpiMeta(name = "motan")//name 表示扩展点的名称,根据名称加载扩展 @Activation( sequence = 100) //同类型支持扩展非生效顺序,部分扩展点。必填添加SPI实现${classpath}/MATA-INF/services/${SPI接口全名}文件添加SPI接口实现类全可参照motan-core模块/MATA-INF/services/下的配置

主要类就是com.weibo.api.motan.core.extension.ExtensionLoader 代码在https://github.com/zhuxingsheng/motan/blob/master/motan-core/src/main/java/com/weibo/api/ motan/core/extension/ExtensionLoader.java#100-99 其实很简单,需要额外的读取代码,请看懂

还有注解类

@Spi 指定类生命周期

@SpiMeta 给类一个别名,方便配置时使用

目录
相关文章
|
3月前
|
传感器 人工智能 JavaScript
Playwright实战:写UI自动化脚本,速度直接起飞
简介: 测试工程师老王因UI自动化问题深夜奋战,反映出传统测试工具的局限性。微软开源的Playwright凭借智能等待、跨域操作、移动端模拟与网络拦截等强大功能,正迅速取代Selenium,成为新一代自动化测试标准。其稳定高效的设计显著降低维护成本,助力企业构建高质量测试流程。
|
关系型数据库 Java Maven
|
Java
Java IntelliJ IDEA 不能显示项目里的文件结构
方法一: 关闭IDEA, 然后删除项目文件夹下的.idea文件夹 重新用IDEA工具打开项目 方法二: 菜单:File -> Invalidate Caches / Restart
4434 0
|
8天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。
|
人工智能 前端开发 API
前端接入通义千问(Qwen)API:5 分钟实现你的 AI 问答助手
本文介绍如何在5分钟内通过前端接入通义千问(Qwen)API,快速打造一个AI问答助手。涵盖API配置、界面设计、流式响应、历史管理、错误重试等核心功能,并提供安全与性能优化建议,助你轻松集成智能对话能力到前端应用中。
665 154
|
14天前
|
人工智能 数据可视化 Java
Spring AI Alibaba、Dify、LangGraph 与 LangChain 综合对比分析报告
本报告对比Spring AI Alibaba、Dify、LangGraph与LangChain四大AI开发框架,涵盖架构、性能、生态及适用场景。数据截至2025年10月,基于公开资料分析,实际发展可能随技术演进调整。
930 152