当前位置:网站首页>SPI服务发现机制
SPI服务发现机制
2022-07-17 14:17:00 【西魏陶渊明】
作者: 西魏陶渊明
博客: https://blog.springlearn.cn/
西魏陶渊明
莫笑少年江湖梦,谁不少年梦江湖
一、什么是SPI
SPI ,全称为 Service Provider Interface,是一种服务发现机制。JDK中的SPI是通过在ClassPath路径下的META-INF/services文件夹查找扩展文件,自动加载文件里所定义的类。
在小编的理解来,觉得它更是一种思想。即找到服务的接口, 美其名曰: 服务发现机制思想。很多开源框架都有借用这种思想,比如dubbo、jdbc。
二、SPI在JDK中如何使用
SPI在JDK中,我们可以使用 ServiceLoader
类进行使用。
1. 前提准备
public interface SpiService {
String say();
}
两个实现类
public class ASpiServiceImpl implements SpiService {
static {
System.out.println("static init a");
}
{
System.out.println("init a");
}
@Override
public String say() {
return "A";
}
}
public class BSpiServiceImpl implements SpiService {
static {
System.out.println("static init b");
}
{
System.out.println("init b");
}
@Override
public String say() {
return "B";
}
}
2. 进行配置
在resources中创建META-INF/services目录
│ └── resources
│ └── META-INF
│ └── services
│ └── com.github.easylog.spi.SpiService
com.github.easylog.spi.SpiService文件内容
com.github.easylog.spi.impl.ASpiServiceImpl
com.github.easylog.spi.impl.BSpiServiceImpl
3. 使用
通过ServiceLoader类我们可以加载到所有配置的实现类,并对实现类进行处理。需要注意一点的是,看4使用注意。
public class SpiTester {
public static void main(String[] args) {
ServiceLoader<SpiService> spiServices = ServiceLoader.load(SpiService.class);
Iterator<SpiService> iterator = spiServices.iterator();
while (iterator.hasNext()) {
SpiService next = iterator.next();
System.out.println(next.say());
}
}
}
4. 使用注意
可以看下小编前面声明的两个实现类,都定义了静态代码块和非静态代码块。正常情况当这个字节码被加载,就会执行静态代码块里面的内容,但是实际运行时候却没有执行, 其实是有原因的。
可以看到第二个参数是false。即加载时候不进行初始化。
三、Dubbo中服务发现思想
服务发现这种思想的特点是: 代码不是硬编码的方式,而是可配置的。只要将要支持的实现类放到指定配置文件下面,就会自动被加载起来了。然后代码中只关心使用即可。我们可以利用这种思想来实现, 框架的扩展,比如前面说了。Dubbo会利用SPI的思想进行,加载用户自定义的过滤器。
这种思想特别适合做服务扩展。现在大多数开源框架中都会使用到这种思想。
1. 定义过滤器
@Activate(group = { Constants.PROVIDER })
public class ProviderHelloFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
System.out.pringln("hello ok!");
return invoker.invoke(invocation);
}
}
2. 添加配置文件
META-INF/dubbo/Interal/com.alibaba.dubbo.rpc.Filter
默认支持的过滤器
利用SPI原理,我们自定义一个过滤器
3. 使用
其实API跟JDK中使用ServiceLoader的方式,非常类同。唯一不同的是Dubbo中是使用ExtensionLoader。因为dubbo中做了一些特殊的增强处理。比如在配置文件中支持自定义一个别名key。如上图hello就是key。通过getExtension(“hello”)就能获取指定的实现类。
public class SpiTester {
public static void main(String[] args) throws Exception{
ExtensionLoader<Filter> filterExtensionLoader = ExtensionLoader.getExtensionLoader(Filter.class);
Set<String> supportedExtensions = filterExtensionLoader.getSupportedExtensions();
System.out.println(supportedExtensions);
//[accesslog, activelimit, cache...]
Filter hello = filterExtensionLoader.getExtension("hello");
//[email protected]
System.out.println(hello);
}
}
**那么这种思想你学会了吗? **
最后求关注,求订阅,谢谢你的阅读!
边栏推荐
- Environment variable configuration of win10
- 【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码
- Mysql优化系列之limit查询
- 常用getshell工具的下载
- Pytoch framework learning record 1 cifar-10 classification
- A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
- 军品研制过程所需文件-进阶版
- Integrated network architecture and network slicing technology of air, earth and sea
- Pytoch and weight decay (L2 norm)
- Loj#2324-「清华集训 2017」小 Y 和二叉树
猜你喜欢
Google Earth engine - Hansen global forest change v1.8 (2000-2020) forest coverage and forest loss data set
How does unity3d use the asset store to download some useful resource packages
LeetCode 2249. Count the number of grid points in the circle
Win10 start key click no response
Download of common getshell tools
Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
[in vivado middle note ILA IP core]
Unity高版本退回低版本报错问题
antd 下拉多选传值到后台做查询操作
leetcode-08
随机推荐
Unity高版本退回低版本报错问题
XSS.haozi.me刷题
8.固定收益投资
Scala's dosing in idea
NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
设置cmd命令提示符窗口的界面语言为英文
[in vivado middle note ILA IP core]
pjudge#21652-[PR #4]到底有没有九【数位dp】
Configuration of vscode+unity3d
input number 纯数字输入 限制长度 限制 最大值
要想组建敏捷团队,这些方法不可少
Establishment of redis cluster, one master, two slave and three Sentinels
Today's sleep quality record 79 points
Unity high version returned low version error
微服务上线规范
火箭大机动运动欧拉角解算的探讨
leetcode-08
数据库锁的介绍与InnoDB共享,排他锁
At5147-[agc036d]negative cycle [DP, model conversion]
UE4 understanding of animation blueprint