dubbo之ExtensionFactory获取适应扩展过程分析

开篇

 这篇文章尝试分析dubbo中getAdaptiveExtension过程,且以ExtensionFactory作为例子进行分析。

 在这篇文章中我们会了解ExtensionFactory的几个对象AdaptiveExtensionFactory、SpringExtensionFactory、SpiExtensionFactory的初始化过程,然后在这个过程中一窥getAdaptiveExtension的全貌。


引用例子

说明:

  • ExtensionLoader的使用分两步走,先getExtensionLoader后getAdaptiveExtension。
  • getExtensionLoader返回对应类的ExtensionLoader。
  • getAdaptiveExtension返回对应类的AdaptiveExtension。
  • Protocol会返回Protocol$Adaptive的扩展类.
public class ServiceConfig<T> extends AbstractServiceConfig {
    private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
}


ExtensionLoader介绍

核心变量说明:

  • EXTENSION_LOADERS保存ExtensionLoader所有的loader对象。
  • DIRECTORY目录保存所有需要访问SPI文件的目录。
  • cachedAdaptiveClass保存type对应的AdaptiveClass。
  • cachedClasses保存type对应的cachedClasses。
  • type需要加载的类,如ExtensionFactory.class或Protocol.class。
  • objectFactory保存ExtensionFactory对象。

过程说明:

  • getExtensionLoader内部最终都会执行ExtensionLoader.getExtensionLoader(ExtensionFactory.class)
    .getAdaptiveExtension()。
  • ExtensionLoader.getExtensionLoader(ExtensionFactory.class)生成type为ExtensionFactory的ExtensionLoader。
  • getAdaptiveExtension()最终执行ExtensionFactory的ExtensionLoader的getAdaptiveExtension()方法。
public class ExtensionLoader<T> {

    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/";

    private static final Pattern NAME_SEPARATOR = Pattern.compile("\\s*[,]+\\s*");

    private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<Class<?>, ExtensionLoader<?>>();

    private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<Class<?>, Object>();

    // ==============================

    private final Class<?> type;

    //todo objectFactory保存的是ExtensionFactory
    private final ExtensionFactory objectFactory;

    private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<Class<?>, String>();

    private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<Map<String, Class<?>>>();

    private final Map<String, Activate> cachedActivates = new ConcurrentHashMap<String, Activate>();
    private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<String, Holder<Object>>();
    private final Holder<Object> cachedAdaptiveInstance = new Holder<Object>();
    private volatile Class<?> cachedAdaptiveClass = null;
    private String cachedDefaultName;
    private volatile Throwable createAdaptiveInstanceError;

    private Set<Class<?>> cachedWrapperClasses;

    private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<String, IllegalStateException>();

    private ExtensionLoader(Class<?> type) {
        this.type = type;
        objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
    }

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

        ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        if (loader == null) {
            EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type));
            loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
        }
        return loader;
    }
}


getAdaptiveExtension

说明:

  • 按照getAdaptiveExtension => createAdaptiveExtension => getAdaptiveExtensionClass().newInstance()流程进行分析。
public class ExtensionLoader<T> {
    public T getAdaptiveExtension() {
        Object instance = cachedAdaptiveInstance.get();
        if (instance == null) {
            if (createAdaptiveInstanceError == null) {
                synchronized (cachedAdaptiveInstance) {
                    instance = cachedAdaptiveInstance.get();
                    if (instance == null) {
                        try {
                            instance = createAdaptiveExtension();
                            cachedAdaptiveInstance.set(instance);
                        } catch (Throwable t) {
                            createAdaptiveInstanceError = t;
                            throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t);
                        }
                    }
                }
            } else {
                throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
            }
        }

        return (T) instance;
    }

    private T createAdaptiveExtension() {
        try {
            return injectExtension((T) getAdaptiveExtensionClass().newInstance());
        } catch (Exception e) {
            throw new IllegalStateException("Can not create adaptive extension " + type + ", cause: " + e.getMessage(), e);
        }
    }
}


getAdaptiveExtensionClass

说明:

  • 类加载按照getExtensionClasses => loadExtensionClasses => loadFile流程。
  • getExtensionClasses会判断是否有缓存,没缓存则走loadExtensionClasses加载类。
  • getAdaptiveExtensionClass会判断是否有缓存,有则返回cachedAdaptiveClass,没有走createAdaptiveExtensionClass流程。
  • ExtensionFactory的对应的类型不会走createAdaptiveExtensionClass过程,但其他如创建Protocol.class的工程则会走createAdaptiveExtensionClass流程。
  • ExtensionFactory的getAdaptiveExtensionClass()方法返回AdaptiveExtensionFactory。
public class ExtensionLoader<T> {

    private Class<?> getAdaptiveExtensionClass() {
        //todo 核心在于读取文件当中所有的类定义并将这些类进行加载
        getExtensionClasses();
        if (cachedAdaptiveClass != null) {
            return cachedAdaptiveClass;
        }

        //todo 这里创建了cachedAdaptiveClass,直接通过编码创建的AdaptiveClass
        return cachedAdaptiveClass = createAdaptiveExtensionClass();
    }


    private Map<String, Class<?>> getExtensionClasses() {
        Map<String, Class<?>> classes = cachedClasses.get();
        if (classes == null) {
            synchronized (cachedClasses) {
                classes = cachedClasses.get();
                if (classes == null) {
                    //todo 这里也会加载文件
                    classes = loadExtensionClasses();
                    cachedClasses.set(classes);
                }
            }
        }
        return classes;
    }
}


loadExtensionClasses

说明:

  • loadExtensionClasses的内部会通过loadFile加载对应SPI对应的资源文件。
  • loadFile读取对应的SPI资源文件并按行解析。
  • loadFile过程中会初始化cachedAdaptiveClass 、cachedActivates、cachedNames。
  • ExtensionFactory的SPI文件中包含cachedAdaptiveClass为AdaptiveExtensionFactory。
  • ExtensionFactory对应有cachedAdaptiveClass,所以不会走createAdaptiveExtensionClass过程。
文件名:
  META-INF/dubbo/internal/com.alibaba.dubbo.common.extension.ExtensionFactory
文件内容
  spring=com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory

文件名:
  META-INF/dubbo/internal/com.alibaba.dubbo.common.extension.ExtensionFactory
文件内容:
  adaptive=com.alibaba.dubbo.common.extension.factory.AdaptiveExtensionFactory
  spi=com.alibaba.dubbo.common.extension.factory.SpiExtensionFactory
public class ExtensionLoader<T> {

    private Map<String, Class<?>> loadExtensionClasses() {
        final SPI defaultAnnotation = type.getAnnotation(SPI.class);
        if (defaultAnnotation != null) {
            String value = defaultAnnotation.value();
            if (value != null && (value = value.trim()).length() > 0) {
                String[] names = NAME_SEPARATOR.split(value);
                if (names.length > 1) {
                    throw new IllegalStateException("more than 1 default extension name on extension " + type.getName()
                            + ": " + Arrays.toString(names));
                }
                if (names.length == 1) cachedDefaultName = names[0];
            }
        }

        Map<String, Class<?>> extensionClasses = new HashMap<String, Class<?>>();
        loadFile(extensionClasses, DUBBO_INTERNAL_DIRECTORY);
        loadFile(extensionClasses, DUBBO_DIRECTORY);
        loadFile(extensionClasses, SERVICES_DIRECTORY);
        return extensionClasses;
    }


    private void loadFile(Map<String, Class<?>> extensionClasses, String dir) {
        String fileName = dir + type.getName();
        try {
            Enumeration<java.net.URL> urls;
            ClassLoader classLoader = findClassLoader();
            if (classLoader != null) {
                urls = classLoader.getResources(fileName);
            } else {
                urls = ClassLoader.getSystemResources(fileName);
            }
            if (urls != null) {
                while (urls.hasMoreElements()) {
                    java.net.URL url = urls.nextElement();
                    try {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
                        try {
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                final int ci = line.indexOf('#');
                                if (ci >= 0) line = line.substring(0, ci);
                                line = line.trim();
                                if (line.length() > 0) {
                                    try {
                                        String name = null;
                                        int i = line.indexOf('=');
                                        if (i > 0) {
                                            name = line.substring(0, i).trim();
                                            line = line.substring(i + 1).trim();
                                        }
                                        if (line.length() > 0) {
                                            Class<?> clazz = Class.forName(line, true, classLoader);
                                            if (!type.isAssignableFrom(clazz)) {
                                                throw new IllegalStateException("Error when load extension class(interface: " +
                                                        type + ", class line: " + clazz.getName() + "), class "
                                                        + clazz.getName() + "is not subtype of interface.");
                                            }
                                            if (clazz.isAnnotationPresent(Adaptive.class)) {
                                                //todo 这个地方创建了cachedAdaptiveClass
                                                if (cachedAdaptiveClass == null) {
                                                    cachedAdaptiveClass = clazz;
                                                } else if (!cachedAdaptiveClass.equals(clazz)) {
                                                    throw new IllegalStateException("More than 1 adaptive class found: "
                                                            + cachedAdaptiveClass.getClass().getName()
                                                            + ", " + clazz.getClass().getName());
                                                }
                                            } else {
                                                try {
                                                    clazz.getConstructor(type);
                                                    Set<Class<?>> wrappers = cachedWrapperClasses;
                                                    if (wrappers == null) {
                                                        cachedWrapperClasses = new ConcurrentHashSet<Class<?>>();
                                                        wrappers = cachedWrapperClasses;
                                                    }
                                                    wrappers.add(clazz);
                                                } catch (NoSuchMethodException e) {
                                                    clazz.getConstructor();
                                                    if (name == null || name.length() == 0) {
                                                        name = findAnnotationName(clazz);
                                                        if (name == null || name.length() == 0) {
                                                            if (clazz.getSimpleName().length() > type.getSimpleName().length()
                                                                    && clazz.getSimpleName().endsWith(type.getSimpleName())) {
                                                                name = clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - type.getSimpleName().length()).toLowerCase();
                                                            } else {
                                                                throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + url);
                                                            }
                                                        }
                                                    }
                                                    String[] names = NAME_SEPARATOR.split(name);
                                                    if (names != null && names.length > 0) {
                                                        Activate activate = clazz.getAnnotation(Activate.class);
                                                        if (activate != null) {
                                                            cachedActivates.put(names[0], activate);
                                                        }
                                                        for (String n : names) {
                                                            if (!cachedNames.containsKey(clazz)) {
                                                                cachedNames.put(clazz, n);
                                                            }
                                                            Class<?> c = extensionClasses.get(n);
                                                            if (c == null) {
                                                                extensionClasses.put(n, clazz);
                                                            } else if (c != clazz) {
                                                                throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + c.getName() + " and " + clazz.getName());
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Throwable t) {
                                        IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + url + ", cause: " + t.getMessage(), t);
                                        exceptions.put(line, e);
                                    }
                                }
                            } // end of while read lines
                        } finally {
                            reader.close();
                        }
                    } catch (Throwable t) {
                        logger.error("Exception when load extension class(interface: " +
                                type + ", class file: " + url + ") in " + url, t);
                    }
                } // end of while urls
            }
        } catch (Throwable t) {
            logger.error("Exception when load extension class(interface: " +
                    type + ", description file: " + fileName + ").", t);
        }
    }
}


AdaptiveExtensionFactory

说明:

  • ExtensionLoader的getAdaptiveExtensionClass().newInstance()会执行AdaptiveExtensionFactory构造函数。
  • AdaptiveExtensionFactory构造函数通过ExtensionFactory的ExtensionLoader的getExtension获取扩展。
  • 扩展包括的name主要是spring和spi
    spring=com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory

spi=com.alibaba.dubbo.common.extension.factory.SpiExtensionFactory

  • 调用ExtensionFactory的ExtensionLoader的getExtension(name)去加载spring和spi的ExtensionFactory。
@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {

    //todo 这里会保存SpiExtensionFactory和SpringExtensionFactory
    private final List<ExtensionFactory> factories;

    public AdaptiveExtensionFactory() {
        ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
        List<ExtensionFactory> list = new ArrayList<ExtensionFactory>();
        for (String name : loader.getSupportedExtensions()) {
            list.add(loader.getExtension(name));
        }
        factories = Collections.unmodifiableList(list);
    }

    public <T> T getExtension(Class<T> type, String name) {
        for (ExtensionFactory factory : factories) {
            T extension = factory.getExtension(type, name);
            if (extension != null) {
                return extension;
            }
        }
        return null;
    }

}

说明:

  • ExtensionLoader的获取扩展按照getExtension => createExtension => clazz.newInstance()执行。
  • spring通过执行SpringExtensionFactory的构造函数生成ExtensionFactory对象。
  • spi通过执行SpiExtensionFactory的构造函数生成ExtensionFactory对象。
public class ExtensionLoader<T> {

    public Set<String> getSupportedExtensions() {
        Map<String, Class<?>> clazzes = getExtensionClasses();
        return Collections.unmodifiableSet(new TreeSet<>(clazzes.keySet()));
    }

    private Map<String, Class<?>> getExtensionClasses() {
        Map<String, Class<?>> classes = cachedClasses.get();
        if (classes == null) {
            synchronized (cachedClasses) {
                classes = cachedClasses.get();
                if (classes == null) {
                    classes = loadExtensionClasses();
                    cachedClasses.set(classes);
                }
            }
        }
        return classes;
    }


    public T getExtension(String name) {
        if (name == null || name.length() == 0)
            throw new IllegalArgumentException("Extension name == null");
        if ("true".equals(name)) {
            return getDefaultExtension();
        }
        Holder<Object> holder = cachedInstances.get(name);
        if (holder == null) {
            cachedInstances.putIfAbsent(name, new Holder<Object>());
            holder = cachedInstances.get(name);
        }
        Object instance = holder.get();
        if (instance == null) {
            synchronized (holder) {
                instance = holder.get();
                if (instance == null) {
                    instance = createExtension(name);
                    holder.set(instance);
                }
            }
        }
        return (T) instance;
    }


    private T createExtension(String name) {
        Class<?> clazz = getExtensionClasses().get(name);
        if (clazz == null) {
            throw findException(name);
        }
        try {
            T instance = (T) EXTENSION_INSTANCES.get(clazz);
            if (instance == null) {
                EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
                instance = (T) EXTENSION_INSTANCES.get(clazz);
            }
            //todo 这里会把set变量注入进去
            injectExtension(instance);
            //todo 这里会把两个wrapper函数进行包装,特别针对protocol协议来说更是这样子
            Set<Class<?>> wrapperClasses = cachedWrapperClasses;
            if (wrapperClasses != null && wrapperClasses.size() > 0) {
                for (Class<?> wrapperClass : wrapperClasses) {
                    instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
                }
            }
            return instance;
        } catch (Throwable t) {
            throw new IllegalStateException("Extension instance(name: " + name + ", class: " +
                    type + ")  could not be instantiated: " + t.getMessage(), t);
        }
    }
}


总结

 整个ExtensionFactory的加载扩展其实宏观分为以下几个步骤:

  • 1、LoadFile加载ExtensionFactory对应的SPI文件及内容。
  • 2、ExtensionFactory对应的AdaptiveExtensionFactory的对象实例化。
  • 3、AdaptiveExtensionFactory构造函数内部初始化SpringExtensionFactory和SpiExtensionFactory。
上一篇:Dubbo Provider export过程分析


下一篇:.Net网站不能预编译以及不能请求的调试过程