簡(jiǎn)單工廠(chǎng)、java spi、dubbo spi

前言:在日常的代碼中,我們經(jīng)常使用簡(jiǎn)單工廠(chǎng)來(lái)生成,某個(gè)接口不同實(shí)現(xiàn)的實(shí)例,但是其實(shí)還是有替代方案來(lái)完成,比如java SPI和dubbo SPI

1.java SPI

例子其實(shí)爛大街了,代碼可參見(jiàn)dubbo 官方文檔,拿來(lái)主義見(jiàn)https://github.com/somewaters/java_case
貼下關(guān)鍵代碼,可以看到,java spi的核心類(lèi)即ServiceLoader

@Test
    public void testJavaSPI(){
        ServiceLoader<Robot> serviceLoader = ServiceLoader.load(Robot.class);
        Iterator<Robot> it =  serviceLoader.iterator();
        while(it.hasNext()){
            Robot robot = it.next();
            robot.sayHello();
        }
    }

大眼一看,ServiceLoader做了兩個(gè)動(dòng)作,一個(gè)是load,一個(gè)是遍歷。看下源碼,在這兩個(gè)動(dòng)作中,究竟發(fā)生了什么呢?

public final class ServiceLoader<S> implements Iterable<S> {
    private static final String PREFIX = "META-INF/services/";
    private Class<S> service;
    private ClassLoader loader;
    private LinkedHashMap<String, S> providers = new LinkedHashMap();
    private ServiceLoader<S>.LazyIterator lookupIterator;

    public static <S> ServiceLoader<S> load(Class<S> var0) {
        ClassLoader var1 = Thread.currentThread().getContextClassLoader();
        return load(var0, var1);
    }

首先,這個(gè)類(lèi)實(shí)現(xiàn)了Iterable,那么后續(xù)自然可以看下其遍歷相關(guān)的方法。
其次,這個(gè)類(lèi)主要有五個(gè)屬性
1.PREFIX ,這個(gè)值眼熟吧,就是spi文件的路徑(jdk 1.8類(lèi)里面用到路徑的時(shí)候還是寫(xiě)的magic String)
2.Class<S> service ,一個(gè)泛型Class屬性
3.ClassLoader loader,一個(gè)類(lèi)加載器
4.LinkedHashMap<String, S> providers 一個(gè)map
5.一個(gè)自定義LazyIterator

回到開(kāi)頭兩個(gè)動(dòng)作的問(wèn)題,load()做了什么?load利用構(gòu)造函數(shù)實(shí)例化了一個(gè)ServiceLoader對(duì)象,且初始化了上述2,3,5三個(gè)屬性,關(guān)鍵代碼見(jiàn)下

public void reload() {
        this.providers.clear();
        this.lookupIterator = new ServiceLoader.LazyIterator(this.service, this.loader);
    }

    private ServiceLoader(Class<S> var1, ClassLoader var2) {
        this.service = (Class)Objects.requireNonNull(var1, "Service interface cannot be null");
        this.loader = var2 == null ? ClassLoader.getSystemClassLoader() : var2;
        this.reload();
    }

那么遍歷的時(shí)候做了什么呢?
主要做了三件事,1.加載類(lèi)文件2.生成實(shí)例3.將實(shí)例放入providers MAP中。

public S next() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            } else {
                String var1 = this.nextName;
                this.nextName = null;
                Class var2 = null;

                try {//@setp1,根據(jù)全限定名加載某一個(gè)實(shí)現(xiàn)類(lèi)
                    var2 = Class.forName(var1, false, this.loader);
                } catch (ClassNotFoundException var5) {
                    ServiceLoader.fail(this.service, "Provider " + var1 + " not found");
                }

                if (!this.service.isAssignableFrom(var2)) {
                    ServiceLoader.fail(this.service, "Provider " + var1 + " not a subtype");
                }

                try {
                    //@setp2,調(diào)用類(lèi)的newInstance方法生成一個(gè)實(shí)例
                    Object var3 = this.service.cast(var2.newInstance());
                   //@setp3,將生成的實(shí)例放入MAP providers 中
                    ServiceLoader.this.providers.put(var1, var3);
                    return var3;
                } catch (Throwable var4) {
                    ServiceLoader.fail(this.service, "Provider " + var1 + " could not be instantiated: " + var4, var4);
                    throw new Error();
                }
            }
        }

放入providers MAP中做什么呢?其實(shí)ServiceLoader暴露給用戶(hù)的接口,主要就在操作這個(gè)MAP來(lái)獲取相應(yīng)的service實(shí)現(xiàn)。

2.dubbo SPI

同樣貼下關(guān)鍵代碼,可以看到關(guān)鍵類(lèi)為ExtensionLoader,核心操作也有兩個(gè)
1.getExtensionLoader
2.getExtension

@Test
    public void testDubboSPI(){
        ExtensionLoader<Robot> extensionLoader = ExtensionLoader.getExtensionLoader(Robot.class);
        Robot robot = extensionLoader.getExtension("bumblebee");
        robot.sayHello();
    }

ExtensionLoader的屬性是相對(duì)比較多的,我只貼一下關(guān)鍵的幾個(gè),前面幾個(gè)路徑,可以對(duì)比java spi的相關(guān)路徑,就知道是做什么用的了。主要關(guān)注一下兩個(gè)ConcurrentMap EXTENSION_LOADERS和EXTENSION_INSTANCES ,看結(jié)構(gòu),一個(gè)是存ExtensionLoader本身,一個(gè)是用來(lái)存實(shí)例。

 private static final Logger logger = LoggerFactory.getLogger(ExtensionLoader.class);
    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 = "META-INF/dubbo/internal/";
    private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");
    private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap();
    private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap();

那么兩個(gè)核心操作,getExtensionLoader和getExtension都做了什么呢?只需要看最后一個(gè)else分支
就是去EXTENSION_LOADERS這個(gè)ConcurrentMap里面去取,取不到new 一個(gè)放進(jìn)去再取,比較簡(jiǎn)單就不分析了

public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
        if (type == null) {
            throw new IllegalArgumentException("Extension type == null");
        } else if (!type.isInterface()) {
            throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
        } else if (!withExtensionAnnotation(type)) {
            throw new IllegalArgumentException("Extension type(" + type + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
        } else {
            ExtensionLoader<T> loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
            if (loader == null) {
                EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
                loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
            }

            return loader;
        }
    }

第二個(gè)核心操作,getExtension做了什么呢?看下核心代碼。是同樣的操作,去map取實(shí)例,沒(méi)有的話(huà),實(shí)例化之后放進(jìn)去,然后再取。

T instance = EXTENSION_INSTANCES.get(clazz);
                if (instance == null) {
                    EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
                    instance = EXTENSION_INSTANCES.get(clazz);
                }

dubbo的所有核心功能,比如注冊(cè)中心,序列化,傳輸?shù)榷贾С质褂胹pi的方式進(jìn)行拓展,以負(fù)載均衡為例,dubbo內(nèi)置了四種,Random,RoundRorbin,LeastActive,ConsistentHash.那么在dubbo的jar包中,有一個(gè)這樣的文件,META-INF/dubbo/internal/com.alibaba.dubbo.rpc.cluster.LoadBalance,內(nèi)容見(jiàn)下

random=com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance
roundrobin=com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance
leastactive=com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance
consistenthash=com.alibaba.dubbo.rpc.cluster.loadbalance.ConsistentHashLoadBalance

在初始化的時(shí)候,可以看到,確實(shí)是通過(guò)dubbo spi,以getExtension的方式來(lái)獲取的LoadBalance實(shí)例。那么如果你需要實(shí)現(xiàn)自己開(kāi)發(fā)的LoadBalance算法,其實(shí)只需要新增一個(gè)實(shí)現(xiàn),然后在配置文件里面配上你自己的算法名稱(chēng),然后就可以初始化的時(shí)候使用你自己的負(fù)載算法了。

protected LoadBalance initLoadBalance(List<Invoker<T>> invokers, Invocation invocation) {
        if (CollectionUtils.isNotEmpty(invokers)) {
            return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(RpcUtils.getMethodName(invocation), LOADBALANCE_KEY, DEFAULT_LOADBALANCE));
        } else {
            return ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(DEFAULT_LOADBALANCE);
        }
    }

3 三者的聯(lián)系于區(qū)別是什么呢?

聯(lián)系:三者都可以實(shí)現(xiàn),根據(jù)client的不同輸入,生產(chǎn)不同的實(shí)例交給client。
區(qū)別:
1.簡(jiǎn)單工廠(chǎng)需要自己實(shí)現(xiàn)工廠(chǎng)類(lèi),而另外兩種相當(dāng)于有現(xiàn)成的工廠(chǎng)類(lèi)
2.java spi 需要在遍歷的過(guò)程中,進(jìn)行實(shí)例生成的工作。沒(méi)有辦法特定針對(duì)某一種實(shí)現(xiàn)直接生成實(shí)例,而dubbo spi就支持這一點(diǎn)。
3.dubbo spi的功能更豐富,還支持ioc,自適應(yīng)拓展等其他更為復(fù)雜的功能
可以根據(jù)業(yè)務(wù)需要進(jìn)行取舍。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。