In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the java development of distributed service framework Dubbo exposure service example analysis, I hope you will learn something after reading this article, let's discuss it together!
Preface to Dubbo service exposure mechanism
Before analyzing the mechanism of service exposure, we must talk about what URL is. URL is ubiquitous and runs through the whole process of Dubbo service exposure.
Generally speaking, URL refers to a uniform resource locator. The standard format is as follows:
Protocol://host:port/path?key1=value1&key2=value2
Dubbo uses this way of URL as the agreed parameter type, and services also use URL to interact with each other.
Dubbo uses URL as the configuration bus, which runs through the whole source code system.
The specific parameters of URL are as follows:
Various protocols in protocol:dubbo, such as dubbo, http, thrift
Host/port: host / port number
Path: interface name
Parameters: parameter key-value pair
Service exposure process
The general process is divided into three steps:
Detect the configuration, some are configured with null values and automatically populate and assemble as URL exposure services, including services that are exposed locally and remotely registered as services to the service registry
Source code parsing
Come to ServiceBean first.
You can see that this class implements ApplicationListener, so that the onApplicationEvent method is called after the Spring IOC container is refreshed, and what is done in this method is the service exposure.
From the if criteria, we can see that this.export () is executed if delayed exposure is not set in the configuration, and exposure is not set, and exposure is supported, and eventually the export method in the parent class ServiceConfig is called.
The main method above is to check the configuration, and then go to this.doExport () to expose the service. The first part of this method is some configuration checks, focusing on the final this.doExportUrls ().
From the above source code, we can see that Dubbo supports multiple registries and multiple protocols, and a service needs to be exposed if there are multiple protocols. This.loadRegistries () is assembled into registry-related URL according to configuration, as follows:
Registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-provider-one&dubbo=2.6.2&pid=29056®istry=zookeeper×tamp=1632309180780
Next, enter this.doExportUrlsFor1Protocol ()
The URL after the construction is completed is shown in the following figure:
The dubbo protocol is used, and the next thing to do is to expose the service according to URL
Local exposure
Take a look at exportLocal (). What this method does is make a local exposure, using the injvm protocol, in which the URL is modified to change the protocol to injvm.
Protocol.export () here is annotated with @ Adaptive annotation, and an adaptive extension is designed. After calling this method, a proxy class is generated. The proxy class knows the protocol used through the parameters in URL, and then implements the extension through the Dubbo SPI mechanism. The corresponding implementation class is selected for the export () method, and the method here selects export () in InjvmProtocol for exposure.
The converted export is as follows:
As shown in the figure, the concrete implementation classes are packaged layer by layer.
Why should it be encapsulated into invoker?
The invoker is encapsulated to shield the details of the call and expose an executable uniformly, which makes it convenient and easy for the caller to call.
Why use local exposure?
Because there may be calls between different local services, after local exposure, internal calls can be made directly, eliminating unnecessary network communication.
Here is a reference to the local exposure flowchart in blogger ao Bing's article:
Remote exposure
At this point, the local exposure has been completed, and now the remote exposure is started.
The process is similar to local exposure, which needs to be encapsulated as invoker and then further generate exporter. The following is the result of stitching URL with registryURL.addParameterAndEncoded () method:
You can see that first we follow the registry protocol, and then an export=dubbo://, in the parameters will follow the dubbo protocol at this time, so we know that SPI will first find the RegistryPotocol through the registry protocol and then execute export (), and then execute export () in the DubboPotocol.
Now let's go to the export () method in RegistryPotocol.
The above operation is mainly about transforming export into exporter, obtaining the relevant configuration of the registry, registering with the registry if you need to register, and recording the service provider information in ProviderConsumerRegTable, entering the source code, we found that the essence is to insert invoker,key into a ConcurrentHashMap which is the full class name of the service interface, and value is an invoker that has been stored and packaged in a set,set.
Then go inside the doLocalExport method
The method pointed to by the arrow will eventually call export () in DubboProtocol
Need to create server
In the above code, the key is created based on URL, and then the key and invoker are associated in exporterMap. If it is the first service exposure, you need to create a listening server. The default is NettyServer, and initialize various Handler, such as heartbeat detection, decoding, and so on.
At this point, the service exposure process ends.
Here is a reference to the local exposure flowchart in the blogger ao Bing's article:
After reading this article, I believe you have some understanding of "sample Analysis of java developing distributed Service Framework Dubbo exposure Service". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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: 230
*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.