Java SPI
通过一个案例来看SPI
java
public interface DemoSPI {
void echo();
}
public class FirstImpl implements DemoSPI{
@Override
public void echo() {
System.out.println("first echo");
}
}
public class SecondImpl implements DemoSPI{
@Override
public void echo() {
System.out.println("second echo");
}
}
public class Main {
public static void main(String[] args) {
ServiceLoader<DemoSPI> loader = ServiceLoader.load(DemoSPI.class);
Iterator<DemoSPI> iterator = loader.iterator();
while (iterator.hasNext()) {
iterator.next().echo();
}
System.out.println("加载完毕!!!!!");
}
}
SPI :Service Provider Interface, 服务提供接口。
JDK SPI:ServiceLoader.load
使用load方法频率高,容易影响IO吞吐和内存消耗, 可以看到回去静态资源目录下读取文件。
使用load方法想要获取指定的实现类,需要自己进行遍历并编写各种比较代码,可能实现有多个所以需要比对。
Dubbo SPI: ApplicationModel.defaultModel().getExtensionLoader
增加缓存,来降低磁盘IO访问及减少对象的生成
使用Map的hash查找,来提升检索指定实现类的性能。
java
@SPI
public interface DubboSPI {
void echo();
}
public class DubboFirstSPI implements DubboSPI{
@Override
public void echo() {
System.out.println("first dubbo spi");
}
}
public class DubboSecondSPI implements DubboSPI{
@Override
public void echo() {
System.out.println("second spi");
}
}
public class DubboMain {
public static void main(String[] args) {
ExtensionLoader<DubboSPI> loader = ExtensionLoader.getExtensionLoader(DubboSPI.class);
DubboSPI firstf = loader.getExtension("first");
DubboSPI second = loader.getExtension("second");
firstf.echo();
second.echo();
}
}
com.liyong.leran.dubbospi.DubboSPI里面的内容:
first=com.liyong.leran.dubbospi.DubboFirstSPI
second=com.liyong.leran.dubbospi.DubboSecondSPI
java
public T getExtension(String name, boolean wrap) {
if (StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("Extension name == null");
}
if ("true".equals(name)) {
return getDefaultExtension();
}
final Holder<Object> holder = getOrCreateHolder(name);
Object instance = holder.get();
if (instance == null) {
synchronized (holder) {
instance = holder.get();
if (instance == null) {
instance = createExtension(name, wrap);
holder.set(instance);
}
}
}
return (T) instance;
}
java
// 走缓存的方法
private Holder<Object> getOrCreateHolder(String name) {
Holder<Object> holder = cachedInstances.get(name);
if (holder == null) {
cachedInstances.putIfAbsent(name, new Holder<>());
holder = cachedInstances.get(name);
}
return holder;
}