Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of SPI in Dubbo

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the introduction of SPI in Dubbo". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction to SPI in Dubbo.

Why design adaptive? What is the difference between annotations on classes and annotations on methods?

Adaptive is designed to identify fixed known classes and extended unknown classes.

1. Note on the class: represents the manual implementation, implements a decoration class (decoration pattern in the design pattern), which mainly acts on the fixed known class

At present, there are only two in the whole system, AdaptiveCompiler and AdaptiveExtensionFactory.

a. Why is the class AdaptiveCompiler always known? Because the entire framework only supports Javassist and JdkCompiler.

b. Why is the class AdaptiveExtensionFactory always known? Because the whole framework only supports two objFactory, one is spi, the other is spring

two。 Note in the method: represents the automatic generation and compilation of a dynamic Adpative class, it is mainly used for SPI, because the spi class is not fixed, unknown extension class, so the design of a dynamic $Adaptive class.

For example, Protocol's spi class has injvm dubbo registry filter listener and so on, and many extension unknown classes

It designs Protocol$Adaptive classes to extract objects through ExtensionLoader.getExtensionLoader (Protocol.class) .getExtension (spike class);

Why would dubbo design his own SPI?

This is the original JDK spi code.

ServiceLoader serviceLoader=ServiceLoader.load (Command.class)

For (Command command:serviceLoader) {

Command.execute ()

}

Dubbo has designed the following functions based on the original

1. The original JDK spi does not support caching; dubbo designed the cache object: the key and value of spi are cached in the cachedInstances object, which is a ConcurrentMap

two。 The original JDK spi does not support default values. Dubbo design defaults: @ SPI ("dubbo") represents the default spi object, for example, @ SPI ("dubbo") of Protocol is DubboProtocol.

Through ExtensionLoader.getExtensionLoader (Protocol.class). GetDefaultExtension () the default object

3.jdk uses for loop to judge objects. Dubbo designs getExtension flexibly and conveniently, and dynamically acquires spi objects.

For example, ExtensionLoader.getExtensionLoader (Protocol.class) .getExtension (key of spi) to extract objects

4. The original JDK spi does not support AOP function, dubbo design adds AOP function, in cachedWrapperClasses, in the original spi class, wraps XxxxFilterWrapper XxxxListenerWrapper

5. The original JDK spi does not support IOC function. IOC is added to the dubbo design, through constructor injection, the code is: wrapperClass.getConstructor (type) .newInstance (instance)

The purpose of dubbo spi: to get an object that specifies the implementation class.

Approach: ExtensionLoader.getExtension (String name)

Implementation path:

GetExtensionLoader (Class type) is to new an ExtensionLoader for this interface and then cache it.

GetAdaptiveExtension () gets an extension class if the @ Adaptive annotation is a decoration class on the class, and if the annotation is a dynamic proxy class on the method, such as the Protocol$Adaptive object.

GetExtension (String name) gets a specified object.

-ExtensionLoader.getExtensionLoader (Class type)

ExtensionLoader.getExtensionLoader (Container.class)

-- > this.type = type

-> objectFactory = (type = = ExtensionFactory.class? Null: ExtensionLoader.getExtensionLoader (ExtensionFactory.class). GetAdaptiveExtension ()

-> ExtensionLoader.getExtensionLoader (ExtensionFactory.class) .getAdaptiveExtension ()

-- > this.type = type

-- > objectFactory = null

The initialization of two attributes is completed by executing the above code.

1. Each ExtensionLoader contains two values type and objectFactory

The interface name to be obtained when the Class type;// constructor initializes

AdaptiveExtensionFactory at ExtensionFactory objectFactory// constructor initialization [SpiExtensionFactory,SpringExtensionFactory]

2.new An ExtensionLoader is stored in ConcurrentMap > EXTENSION_LOADERS

Some details about this objectFactory:

1.objectFactory is ExtensionFactory, and it is also implemented through ExtensionLoader.getExtensionLoader (ExtensionFactory.class), but its objectFactory=null

The 2.objectFactory function, which provides all the objects for dubbo's IOC.

-getAdaptiveExtension ()

-- > getAdaptiveExtension () / / assign values to cachedAdaptiveInstance

-> createAdaptiveExtension ()

-> getAdaptiveExtensionClass ()

-- > getExtensionClasses () / / assign values to cachedClasses

-> loadExtensionClasses ()

-> loadFile

-- > createAdaptiveExtensionClass () / / automatically generate and compile a dynamic adpative class, which is a proxy class

-> ExtensionLoader.getExtensionLoader (com.alibaba.dubbo.common.compiler.Compiler.class) .getAdaptiveExtension ()

-- > compiler.compile (code, classLoader)

-- > injectExtension () / / function: enter the reverse control mode of IOC to realize dynamic injection

Some details about loadfile

Purpose: by storing the contents of the configuration file META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol in the cache variable.

CachedAdaptiveClass// assigns a value if the class contains adative annotations, such as ExtensionFactory, while Protocol is not available in this section.

CachedWrapperClasses// only if the class has no adative annotations and the constructor contains the target interface (type) type

For example, only ProtocolFilterWrapper and ProtocolListenerWrapper can hit the spi in protocol.

The rest of the cachedActivates// classes, including Activate annotations

The rest of the cachedNames// classes are stored here.

-getExtension (String name)

GetExtension (String name) / / specifies that the object is cached in the wrapper object that comes out of cachedInstances;get. For example, protocol is one of ProtocolFilterWrapper and ProtocolListenerWrapper.

-- > createExtension (String name)

-> getExtensionClasses ()

-- > IOC inversion control of injectExtension (T instance) / / dubbo, which extracts object assignments from spi and spring.

-- > objectFactory.getExtension (pt, property)

-- > SpiExtensionFactory.getExtension (type, name)

-- > ExtensionLoader.getExtensionLoader (type)

-> loader.getAdaptiveExtension ()

-- > SpringExtensionFactory.getExtension (type, name)

-- > context.getBean (name)

-- > simple design of injectExtension ((T) wrapperClass.getConstructor (type) .newInstance (instance)) / / AOP

At this point, I believe you have a deeper understanding of the "introduction to SPI in Dubbo". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report