dubbo SPI 机制简介
所属分类 dubbo
浏览量 928
jdk SPI Service Provider Interface
java.util.ServiceLoader
加载META-INF/service/ 下的配置文件
/src/main/resources/META-INF/services/dyyx.Pet
dyyx.Dog
协议 调用拦截 路由扩展
模块之间 基于接口编程 可拔插原则
dubbo SPI机制 与 JDK SPI 区别
JDK SPI 遍历查找扩展点和实例化,可能导致一次性加载所有的扩展点
配置文件中扩展实现的格式修改
/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Filter
monitor=com.alibaba.dubbo.monitor.support.MonitorFilter
trace=com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter
exception=com.alibaba.dubbo.rpc.filter.ExceptionFilter
executelimit=com.alibaba.dubbo.rpc.filter.ExecuteLimitFilter
增加对IOC AOP的支持,一个扩展点可以直接通过setter注入到其他扩展点
com.alibaba.dubbo.common.extension.ExtensionLoader
@SPI注解
dubbo:service dubbo:reference
@SPI("dubbo")
public interface Protocol
/META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol
dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
@SPI("netty")
public interface Transporter {
@Adaptive({Constants.SERVER_KEY, Constants.TRANSPORTER_KEY})
Server bind(URL url, ChannelHandler handler) throws RemotingException;
@Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY})
Client connect(URL url, ChannelHandler handler) throws RemotingException;
@Adaptive注解
bind和connect方法上都有@Adaptive注解,有该注解的方法的参数必须包含URL
ExtensionLoader 通过createAdaptiveExtensionClassCode方法动态生成一个Transporter$Adaptive类
扩展点 依赖 URL携带的配置信息
根据URL中的配置来选择对应的扩展实现
dubbo以URL为总线,运行过程中的状态数据可以通过URL来获取
比如采用的 序列化协议 负载均衡策略等信息
通过对应的Key从URL的参数列表中获取
@Activate注解
用条件来控制该扩展点实现是否被自动激活加载
可以设置两个参数 group和value
@Activate(group = Constants.PROVIDER)
public class ExceptionFilter implements Filter
@Activate(group = Constants.PROVIDER, value = Constants.ACCESS_LOG_KEY)
public class AccessLogFilter implements Filter
ExtensionFactory
SpiExtensionFactory
SpringExtensionFactory
ExtensionLoader
private static final String SERVICES_DIRECTORY = "META-INF/services/";
private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";
private static final String DUBBO_INTERNAL_DIRECTORY = DUBBO_DIRECTORY + "internal/";
META-INF/services/
META-INF/dubbo/
META-INF/dubbo/internal/
public static ExtensionLoader getExtensionLoader(Class type)
public T getExtension(String name)
public T getAdaptiveExtension()
LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(loadbalanceName)
RouterFactory routerFactory = ExtensionLoader.getExtensionLoader(RouterFactory.class).getAdaptiveExtension()
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(StatusChecker.class);
List checkers = extensionLoader.getActivateExtension(channel.getUrl(), "status");
ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(url.getParameter(Constants.SERIALIZATION_KEY, "java"));
List filters = ExtensionLoader.getExtensionLoader(Filter.class).getActivateExtension(invoker.getUrl(), key, group);
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME);
ServiceLoader机制及实例
上一篇
下一篇
BlockingQueue add offer put 区别
dubbo注册过程简介
dubbo获取线程池等信息
dubbo各个模块简介
基于netty的RESTFUL框架
Java动态代理InvocationHandler例子