Skip to content

Commit 6362e62

Browse files
committed
[U] 更新DubboSPI文章
1 parent fdb4f16 commit 6362e62

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

note/Dubbo/Dubbo底层源码学习(二)—— Dubbo的SPI机制.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,41 @@ Java SPI的定义及使用步骤如下:
2424

2525
```
2626
public interface Printservice (
27-
void printlnfo();
27+
void printlnfo();
2828
}
2929
```
3030

31-
在构造函数中传入其他扩展实例:装饰器模式,《深入理解Apache Dubbo与实战》中的76页。
31+
```
32+
public class PrintServicelmpl implements Printservice {
33+
@Override
34+
public void printlnfo() {
35+
System.out.println("hello world");
36+
}
37+
}
38+
```
39+
40+
```
41+
public static void main(String[] args) (
42+
ServiceLoader<PrintService> serviceServiceLoader =
43+
ServiceLoader.load(PrintService.class);
44+
for (Printservice printservice : serviceServiceLoader) (
45+
//此处会输出:hello world 获取所有的SPI实现,循环调用
46+
printService.printInfo(); printlnfo()方法,会打印出 hello world
47+
}
48+
}
49+
```
50+
51+
在JDK SPI中,是通过ServiceLoader来获取所有接口实现的。
3252

3353
### 2. Dubbo SPI
3454

35-
Dubbo SPI没有直接使用Java SPL而是在它的思想上又做了一定的改进,形成了一套自己的配置规范和特性。同时,Dubbo SPI又兼容Java SPL服务在启动的时候,Dubbo就会查找这些扩展点的所有实现
55+
Dubbo SPI没有直接使用Java SPL而是在它的思想上又做了一定的改进,形成了一套自己的配置规范和特性。同时,Dubbo SPI又兼容Java SPL服务在启动的时候,Dubbo就会查找这些扩展点的所有实现
56+
57+
Dubbo SPI之于JDK SPI,做到了三点优化:
58+
1. 不同于JDK SPI会一次性实例化扩展点所有实现,因为JDK SPI有扩展实现,则初始化会很耗时,并且如果没有用上也要加载,则会很浪费资源。而Dubbo SPI只会加载扩展点,而不会对其进行初始化,并且Dubbo SPI中
59+
会根据不同的实现类来缓存到内存中,性能上得到了很大的优化。
60+
2. JDK SPI如果对扩展加载失败,则连扩展的名称都获取不到,并且失败原因会被吃掉,而Dubbo SPI则会将异常堆栈保留下来,方便后序
61+
对其异常信息进行分析。
62+
3. Dubbo SPI增加了对IOC和AOP的支持,在Dubbo中,一个扩展点可以通过setter来注入到其他扩展点中。
63+
64+
未完待续....

0 commit comments

Comments
 (0)