Dubbo内核实现之SPI简单介绍
2018-08-16 09:27
543 查看
Dubbo采用微内核+插件体系,使得设计优雅,扩展性强。那所谓的微内核+插件体系是如何实现的呢!即我们定义了服务接口标准,让厂商去实现(如果不了解spi的请谷歌百度下), jdk通过ServiceLoader类实现spi机制的服务查找功能。
JDK实现spi服务查找: ServiceLoader
首先定义下示例接口
ServiceLoader会遍历所有jar查找META-INF/services/com.example.Spi文件
在A厂商提供的jar包中的META-INF/services/com.example.Spi文件内容为:
com.a.example.SpiAImpl #厂商A的spi实现全路径类名
B厂商提供实现
在B厂商提供的jar包中的META-INF/services/com.example.Spi文件内容为:
com.b.example.SpiBImpl #厂商B的spi实现全路径类名
ServiceLoader.load(Spi.class)读取厂商A、B提供jar包中的文件,ServiceLoader实现了Iterable接口可通过while for循环语句遍历出所有实现。
一个接口多种实现,就如策略模式一样提供了策略的实现,但是没有提供策略的选择, 使用方可以根据isSupport方法根据业务传入厂商名来选择具体的厂商。
SPI接口定义
定义了@SPI注解
public @interface SPI {
Stringvalue() default ""; //指定默认的扩展点
}
只有在接口打了@SPI注解的接口类才会去查找扩展点实现,会依次从这几个文件中读取扩展点
META-INF/dubbo/internal/ //dubbo内部实现的各种扩展都放在了这个目录了
META-INF/dubbo/
META-INF/services/
我们以Protocol接口为例, 接口上打上SPI注解,默认扩展点名字为dubbo
@SPI("dubbo")
public interface Protocol{
}
具体实现的类有:
所以说:Remoting实现是Dubbo协议的实现。
JDK实现spi服务查找: ServiceLoader
首先定义下示例接口
package com.example; public interface Spi { booleanisSupport(String name); String sayHello(); }
ServiceLoader会遍历所有jar查找META-INF/services/com.example.Spi文件
package com.a.example; public class SpiAImpl implements Spi { publicboolean isSupport(String name) { return"SPIA".equalsIgnoreCase(name.trim()); } public String syaHello() { return “hello 我是厂商A”; } }
在A厂商提供的jar包中的META-INF/services/com.example.Spi文件内容为:
com.a.example.SpiAImpl #厂商A的spi实现全路径类名
B厂商提供实现
package com.b.example; public class SpiBImpl implements Spi { publicboolean isSupport(String name) { return"SPIB".equalsIgnoreCase(name.trim()); } public String syaHello() { return “hello 我是厂商B”; } }
在B厂商提供的jar包中的META-INF/services/com.example.Spi文件内容为:
com.b.example.SpiBImpl #厂商B的spi实现全路径类名
ServiceLoader.load(Spi.class)读取厂商A、B提供jar包中的文件,ServiceLoader实现了Iterable接口可通过while for循环语句遍历出所有实现。
一个接口多种实现,就如策略模式一样提供了策略的实现,但是没有提供策略的选择, 使用方可以根据isSupport方法根据业务传入厂商名来选择具体的厂商。
public class SpiFactory { //读取配置获取所有实现 privatestatic ServiceLoader spiLoader = ServiceLoader.load(Spi.class); //根据名字选取对应实现 publicstatic Spi getSpi(String name) { for(Spi spi : spiLoader) { if(spi.isSupport(name) ) { returnspi; } } returnnull; } }
SPI接口定义
定义了@SPI注解
public @interface SPI {
Stringvalue() default ""; //指定默认的扩展点
}
只有在接口打了@SPI注解的接口类才会去查找扩展点实现,会依次从这几个文件中读取扩展点
META-INF/dubbo/internal/ //dubbo内部实现的各种扩展都放在了这个目录了
META-INF/dubbo/
META-INF/services/
我们以Protocol接口为例, 接口上打上SPI注解,默认扩展点名字为dubbo
@SPI("dubbo")
public interface Protocol{
}
具体实现的类有:
所以说:Remoting实现是Dubbo协议的实现。
相关文章推荐
- 【DUBBO】Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- Dubbo内核实现之SPI简单介绍
- Dubbo源码分析(五):Dubbo内核实现之SPI简单介绍
- 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- Dubbo 原理解析-Dubbo 内核实现之 SPI 简单介绍
- Dubbo内核实现之SPI简单介绍
- 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍
- Dubbo学习过程、使用经验分享及实现原理简单介绍
- Dubbo学习过程、使用经验分享及实现原理简单介绍
- dubbo实现原理简单介绍
- dubbo学习过程、使用经验分享及实现原理简单介绍
- dubbo学习过程、使用经验分享及实现原理简单介绍
- dubbo学习过程、使用经验分享及实现原理简单介绍,dubbo经验分享
- dubbo学习过程、使用经验分享及实现原理简单介绍
- dubbo学习过程、使用经验分享及实现原理简单介绍
- 2. Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现
- Dubbo学习过程、使用经验分享及实现原理简单介绍
- 使用经验分享及实现原理简单介绍,dubbo经验分享