In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 introduces "what is the spi thought of dubbo in java". In the daily operation, I believe that many people have doubts about the spi thought of dubbo in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "what is the spi thought of dubbo in java?" Next, please follow the editor to study!
Interview questions
What is dubbo's idea of spi?
Psychological analysis of interviewer
Continue to ask in depth, the previous some basic things have been asked, make sure you should all ok, understand some basic things of dubbo, then ask a slightly more difficult question, that is, spi, first ask you what is spi? Then ask you how the spi of dubbo is implemented?
It's just to see how well you have mastered dubbo.
What is the spi for analyzing the interview questions?
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 configure interface A = to implement A2, your configuration will be loaded when the system is actually running, and an object will be instantiated to provide services with the implementation A2.
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.
At this point, the study of "what is the spi idea of dubbo in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.