Cluster自适应扩展类:
我们在代码中不可能写死使用哪个扩展点,所以才会生成自适应扩展类,如果某个某个实现类被 Adaptive 注解修饰了,那么该类就是自适应扩展类了,不然会生成自适应扩展类。
看看导出的其中一个服务,像protocol等,如果配置了就使用配置的,如果没有配置就使用dubbo
我们那cluster举例,
生成的类名:Cluster$Adpative
从url取配置的策略,如果没有配置默认就是使用failover。
String extName = url.getParameter("cluster", "failover");
根据extName动态生成扩展点
com.alibaba.dubbo.rpc.cluster.Cluster extension = (com.alibaba.dubbo.rpc.cluster.Cluster)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.rpc.cluster.Cluster.class).getExtension(extName);
?xml version="1.0" encoding="UTF-8"?
package com.alibaba.dubbo.rpc.cluster;
importcom.alibaba.dubbo.common.extension.ExtensionLoader;
public class Cluster$Adpative implements com.alibaba.dubbo.rpc.cluster.Cluster {
public com.alibaba.dubbo.rpc.Invoker join(com.alibaba.dubbo.rpc.cluster.Directory arg0) throws com.alibaba.dubbo.rpc.cluster.Directory {
if (arg0 == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.cluster.Directory argument == null");
if (arg0.getUrl() == null) throw new IllegalArgumentException("com.alibaba.dubbo.rpc.cluster.Directory argument getUrl() == null");
com.alibaba.dubbo.common.URLurl = arg0.getUrl();
String extName = url.getParameter("cluster", "failover");
if(extName == null) throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.rpc.cluster.Cluster) name from url(" + url.toString() + ") use keys([cluster])");
com.alibaba.dubbo.rpc.cluster.Cluster extension = (com.alibaba.dubbo.rpc.cluster.Cluster)ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.r pc.cluster.Cluster.class).getExtension(extName);
return extension.join(arg0);
}
}
网友评论