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

What is dubbo's idea of spi?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the spi thought of dubbo". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the spi thought of dubbo?"

What is spi?

Spi, to put it simply, service provider interface, what does it mean to put it bluntly, for example, you have an interface, and now this interface has three implementation classes, so which implementation class should you choose for this interface when the system is running? This requires spi, which needs to find the corresponding implementation class to load in according to the specified configuration or default configuration, and then use the instance object of the implementation class.

Give me a chestnut.

You have an interface A. A1/A2/A3 are different implementations of interface A. If you implement A2 by configuring interface A =, then when the system is actually running, your configuration will be loaded and an object will be instantiated with A2 to provide services.

Where is the spi mechanism generally used? Plug-in extension scenario, for example, if you develop an open source framework for others to use, if you want others to write a plug-in and plug it into your open source framework to extend a certain function, the spi idea will be used at this time.

The embodiment of Java spi's thought

The classic idea of spi is usually used by everyone, such as jdbc.

Java defines a set of interfaces for jdbc, but Java does not provide an implementation class for jdbc.

But which implementation classes of the jdbc interface should be used when the project is actually running? Generally speaking, according to the database we use, such as mysql, you introduce mysql-jdbc-connector.jar; oracle, you introduce oracle-jdbc-connector.jar.

When the system is running, when you encounter the interface that you use jdbc, it will use the implementation class provided in the jar you introduced at the bottom.

Dubbo's thought of spi

Dubbo also uses the idea of spi, but does not use the spi mechanism of jdk. It is a set of spi mechanism implemented by itself.

Protocol protocol = ExtensionLoader.getExtensionLoader (Protocol.class). GetAdaptiveExtension ()

Protocol interface, when the system is running, dubbo will determine which implementation class of this Protocol interface should be selected to instantiate the object to use.

It will find a Protocol that you configure, load your configured Protocol implementation class into jvm, and then instantiate the object, just use your Protocol implementation class.

The above line of code is widely used in dubbo, that is, for many components, it retains an interface and multiple implementations, and then dynamically finds the corresponding implementation class according to the configuration while the system is running. If you do not have the configuration, then go to the default implementation, no problem.

SPI ("dubbo") public interface Protocol {int getDefaultPort (); @ Adaptive Exporter export (Invoker invoker) throws RpcException; @ Adaptive Invoker refer (Class type, URL url) throws RpcException; void destroy ();}

In dubbo's own jar, in the / META_INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol file:

Dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocolhttp=com.alibaba.dubbo.rpc.protocol.http.HttpProtocolhessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol

So, this shows how dubbo's spi mechanism is played by default. In fact, it is the Protocol interface. @ SPI ("dubbo") says that the implementation class is provided through the SPI mechanism. The implementation class is found in the configuration file through dubbo as the default key. The configuration file name is the same as the fully qualified name of the interface, and the default implementation class can be found through dubbo as key.

If you want to dynamically replace the default implementation class, you need to use the @ Adaptive interface. In the Protocol interface, there are two methods annotated with @ Adaptive, that is, those two interfaces will be implemented by the proxy.

What do you mean?

For example, the Protocol interface has two @ Adaptive annotated methods that generate proxy classes for Protocol at run time. There will be proxy code in the two methods of this proxy class. The proxy code will dynamically obtain that key according to the protocol in url at run time. The default is dubbo. You can also specify it yourself. If you specify another key, you will get instances of other implementation classes.

How to extend the components in dubbo yourself

Let's talk about how to extend the components in dubbo yourself.

Write a project by yourself. if it can be made into a jar package, make a META-INF/services in the src/main/resources directory inside, with a file called com.alibaba.dubbo.rpc.Protocol and a my=com.bingo.MyProtocol in the file. Get jar into the nexus server yourself.

Then create your own dubbo provider project, in which you rely on your own jar, and then give a configuration in the spring configuration file:

When provider starts, it will be loaded into the line configuration of my=com.bingo.MyProtocol in our jar package, and then you will use your defined MyProtocol according to your configuration. This is a simple explanation. In the above way, you can replace a large number of internal components of dubbo, that is, throw your own jar package and configure it.

Dubbo provides a large number of extension points similar to the above, that is, if you want to extend something, just write a jar, let your consumer or provider project, rely on your jar, and configure the file corresponding to the interface name under the specified directory in your jar, which implements the class through key=.

Then for the corresponding components, similar to using your key corresponding implementation class to implement an interface, you can extend the various functions of dubbo and provide your own implementation.

At this point, I believe you have a deeper understanding of "what is the spi idea of 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