Dubbo source study notes the extension mechanism and ExtensionLoader class

 Read it again before dubbo code for a long time, individuals are lazy, I do not take notes summarize, many have forgotten. Recently time to carefully study the next source record summary, a deeper impression.

 surroundings:

  Source Version: 2.7.3-SNAPSHOT 

      IDE: idea Intelij 

 

   1. extension mechanism with class ExtensionLoader

      Dubbo using micro-kernel extensions + excellent design, the design draws on the extension point of JDK ServiceLoader mechanism, core implementations are within ExtensionLoader.class, understand this class, to read, debug code is critical.

     ExtensiionLoader class member variables are as follows:

    // =============== basic data =============== 
    Private String cachedDefaultName; // default extension name of the SPI 
    private final Class <?> type; // the type of the currently loaded SPI class 
    private final ExtensionFactory objectFactory; SPI // the object factory extension extension for injecting property (set Object method) 
    
    // class cache == ========= ======= 
    Private volatile cachedAdaptiveClass = null Class <?>; 
    ? Private Final Holder <the Map <String, Class <= >>> cachedClasses new new Holder <> (); 
    Private the Set <Class <>> cachedWrapperClasses?; // Warp extension class collection. If an extension constructor parameter type for the current expansion, then this extension to Wrap type 

    // ========== Instance cache ================= 
    Private final Holder <Object> cachedAdaptiveInstance = new Holder <> (); // Adaptive cached instance
    private final ConcurrentMap <String, Holder < Object >> cachedInstances = new ConcurrentHashMap <> (); // generated extended cached instance 
    
    private final Map <String, Object> cachedActivates = new ConcurrentHashMap <> (); // Activate extended instance caching 

    // ============= ============= generated content running 
    private final ConcurrentMap <Class <?> , String> cachedNames = new ConcurrentHashMap <> ( ); // cache name mapping extension points Class- 
    Private volatile Throwable createAdaptiveInstanceError; 
    Private the map <String, IllegalStateException> Exceptions and new new ConcurrentHashMap = <> (); 

    <?> Private ExtensionLoader (Class of the type) { 
        this.type = of the type; 
        / / each ExtensionLoader (except ExtensionFactory) needs a factory, still using the extension mechanism loads
        objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());
    }

  The member variables into a data base, Class cache, Instance cache, the other four kinds of data generated at runtime.

  When the execution loadExtensionClasses () method, through all the expansion achieved in the loadClass () method, the

An extension will be divided into adaptive, common to achieve a wrap extension of:

   1. Adaptive extension, in the above class with Adaptive annotation, up to 1 (if not, automatically generated when a call getAdaptiveExtension ()), for example: AdaptiveExtensionFactory class

   2. Ordinary extension, extension point embodied, for example, extended DubboProtocol Protocol extension.

   3. wrap extension, the type of extension, the extension has a constructor point, e.g. ProtocolFilterWrapper Protocol is extended, there is a constructor, a parameter Protocol type. wrap extension is the extension of the ordinary package, when generating common extension will cycle again wrap class, the entire package again.

 

 2.  important ways

  ExtensionLoader There are two important ways: getAdaptiveExtension () and getActivateExtension ()

  getAdaptiveExtension :

     adaptive extension is a wrapper (proxy) class, it will pass key-value based on the value of url inside the runtime, to determine the actual expansion of the final call.

    Internal logic is as follows:

      If the member variable has a value adaptiveClass, directly with Class.newInstance () one example out,

      If no value, then use a String adaptiveClass source configured to use real-time complier compiler generates adaptive instance.

   

 getActivateExtension:

      activate extension is an extension of conditional && value when the packet satisfies the condition, it will be loaded into.

      For example, the call Invokcation Fitler, Group Consumer divided provider and, when the packet is privider Filter value, the server side will be constructed.

// group for the provider, the service only for publishing 
// value has a value, which need configuration, the accesslog configuration items property value 
@Activate (Group = the PROVIDER, value = ACCESS_LOG_KEY) 
public class AccessLogFilter the implements the Filter {

 

3. Extended instantiation

  In dubbo inside, are generally extended constructor with no arguments, in addition to wrap extended, this is a constructor extension point.

  Examples of the extension point, will inject property. Traversing the beginning of the set, the method lookup parameter type, and to acquire a corresponding type of an object through the objectFactory, complete injection.

  Injecting property, it is easier to see in ExchangeServer, Transporter which, at the beginning Debug trace the source, we often do not know what time is this property, I feel somehow.

 

 

  More than a few points, the basic method which is ExtensionLoader more critical part, to clarify a few points above, the operating mechanism for micro-kernel, can be very clear. Follow-up process is assembled.

Guess you like

Origin www.cnblogs.com/keep-code/p/11058662.html