In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how is the Dubbo registry designed". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the Dubbo registry is designed.
With regard to the analysis of source code and principle, we all need to find an entry point. The premise of finding an entry point is that you need to know what the function of the registry is. The registry believes that everyone is familiar with it. Every general registry needs to provide two basic functional points:
The service provider registers its exposed services with the registry
Service consumers can get the services they need from the registry.
First of all, we need to determine the location of Dubbo's source package for the registry: org.apache.dubbo.registry. Let's take a look at the directory structure diagram of the package:
The nine packages in the red box above correspond to the nine registry implementations in Dubbo, and the specific registry implementation can also be seen from the package name. The implementation of the registry recommended by the Dubbo official website is based on the registry of ZooKeeper. So let's analyze how a ZooKeeper-based registry is implemented in Dubbo.
Dubbo registry design
In fact, in the process of learning open source frameworks, in addition to learning how to use them, we can also learn the design of these open source frameworks by analyzing the source code. the implementation of each open source framework is basically easy to expand. this extensible feature benefits from excellent design patterns. Let's first take a look at how Dubbo is designed to support a variety of registries in the registry implementation.
SPI
SPI (Service Provider Interface) is a service discovery mechanism provided by Java. Generally speaking, SPI uses a specific interface implementation class to dynamically replace the interface at run time.
The specific implementation in Java is very simple, you only need to define an interface, and then use the specific implementation class to implement this interface, and then create a file named by the fully qualified name of the interface in META-INFO/services, the content of which is the fully qualified name of the specific interface implementation class.
There is a java.util.ServiceLoader in Java that looks up the specific implementation of the service.
Dubbo SPI
Instead of directly using Java's native SPI, Dubbo has reimplemented a more powerful SPI mechanism. The related logic of Dubbo SPI is encapsulated in the ExtensionLoader class. For more information, please see the official website. Here is only a brief explanation of the basic process of dynamic replacement of extension classes.
First, you need to get the ExtensionLoader instance (call ExtensionLoader.getExtensionLoader (Class type)), where type is the interface class.
Call the getExtension (String name) method through the ExtensionLoader instance to obtain the specific instance (name is the key value of the extension class configured in Dubbo). This step first gets the specific extension class instance from the cache, and returns it directly if it is obtained (this indicates that the extension class instance has been loaded) If not, you need to create it (call the createExtension (String name) method) and store it in the cache (double detection + lock is used in this step to avoid concurrency problems. For more information, please see the source code)
Before creating an instance, we first need to find the specific implementation class corresponding to the corresponding extension class (getExtensionClasses (). Get (name)) according to name. In order to find the specific implementation class, we first need to obtain the concrete implementation class (getExtensionClasses ()) of all the extension classes in the application.
Before getting, check to see if there is a @ SPI annotation on the interface class represented by type. If so, parse and cache the default key (the value value of @ SPI annotation), then load the implementation class of the specific extension class. The implementation class of the specific extension class is also obtained based on the cache. If it is found that a collection of implementation classes can be obtained from the cache, it will be returned directly. Otherwise, you need to load the concrete implementation classes of all the extension classes in the application and store them in the cache. Key is the first part of the file, and value is the concrete Class represented after =.
The name value passed in in the second step then gets the Class from Map. If the value is empty, an exception is thrown, otherwise it will be successful.
After getting the specific Class, first verify whether the specific object of the Class has been cached in the cache. If there is no cache, create a new object and cache it.
After obtaining the objects that extend the implementation class, we need to use reflection to assign values (call the injectExtension method) to inject dependencies. This part of the function needs to rely on the Dubbo IOC function.
Dubbo IOC
Dubbo first gets all the setter methods through reflection, then gets the dependent object through ExtensionFactory, and finally sets the dependency to the target object through reflection calls. The ExtensionFactory used in ExtensionLoader is an ExtensionFactory list maintained internally by AdaptiveExtensionFactory,AdaptiveExtensionFactory to store other types of ExtensionFactory. Dubbo currently offers two kinds of ExtensionFactory, SpiExtensionFactory and SpringExtensionFactory. The former is used to create adaptive extensions, and the latter is used to get the required extensions from Spring's IOC container.
At this point, I believe you have a deeper understanding of "how the Dubbo registry is designed". 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.
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.