Below you will find pages that utilize the taxonomy term “Dubbo”
Postsread more
dubbo的spi
dubbo的spi相比于jdk的spi而言,提供了更为强大的功能,主要来说是帮助我们更好的面对多个服务互相依赖的场景,并且做了一定优化。(例如:按需加载)
按需加载
jdk的spi的配置文件
org.example.ToyotaCar
org.example.HondaCar
dubbo的spi的配置文件
toyota=org.example.ToyotaCar
honda=org.example.HondaCar
wrapper=org.example.aop.CarWrapper1
wrapper=org.example.aop.CarWrapper2
Race=org.example.ioc.RaceRes
red=org.example.ioc.PenRes
二者的区别在于dubbo中使用键值,可以实现按需加载,请注意,该加载并非指配置文件的加载,而加载配置文件之后的对于服务对象的实例化。
示例
jdk的spi
ServiceLoader<Car> load = ServiceLoader.load(Car.class);
// 获取迭代器遍历
Iterator<Car> iterator = load.iterator();
while (iterator.hasNext()){
Car registry = iterator.next();
registry.run();
}
追溯源码可以看到迭代器内部。
关键成员变量
Iterator pending ;//配置文件读取的数据 该变量也是迭代器 String nextName ;//下一个将要读取的配置
存在关键方法
hasNextService:用于判断下一个服务名称,即配置文件当中的全限定类名。
private boolean hasNextService() {
if (nextName != null) {
return true;
}
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
if (loader == null)
configs = ClassLoader.getSystemResources(fullName);
else
configs = loader.getResources(fullName);
} catch (IOException x) {
fail(service, "Error locating configuration files", x);
}
}
while ((pending == null) || !pending.hasNext()) {
if (!configs.hasMoreElements()) {
return false;
}
pending = parse(service, configs.nextElement());
}
nextName = pending.next();//获取下一个元素,
return true;
}
nextService: