extension module for dubbo source code research

   The extension of dubbo is implemented by the spi mechanism. spi (Service Provider Interface) refers to some classes, interfaces or methods that provide you with inheritance, extension, and custom functions. The spi passes control to the caller, and the caller decides which implementation of the spi to use.
   The core class of dubbo extension mechanism is ExtensionLoader, which obtains an ExtensionLoader instance of a specified interface through the static method getExtensionLoader.
 
    @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;
    }


   This method requires that the @spi annotation must be included on the interface implemented by spi, and the ExtensionLoader of an interface is unique and stored in the static container EXTENSION_LOADERS (ConcurrentHashMap).
   ExtensionLoader provides the instance method getExtension to obtain the concrete implementation of this interface.
 
  	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;
	}


  The specific process is as follows



  . This method optimizes performance through a large number of cache containers, and each extension point exists as a single instance, so when extending the dubbo framework, pay attention to the thread safety of this extension point.
  ExtensionFactory provides injected properties for the spi interface implementation instance when injecting properties (injectExtension).

  The factory has three implementations, which respectively support obtaining objects from spring, spi, and Adaptive, and injecting them into Extension objects.

 

   

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326565079&siteId=291194637