dubbo service execution process source code analysis

In the previous explanation, we illustrate the integration and use of springboot dubbo, but for many students, it may also not clear execution flow inside the dubbo, if you want to figure out dubbo of running processes, then you need to track the source code for analysis this is a time-consuming process, but if you figure out the principle of which is the framework for our understanding dubbo good help,

First, we affix the official two graphs dubbo,

The first figure is a basic block diagram of dubbo, also called abstraction of the logical structure,
Here Insert Picture Description

Can we explain this architecture diagram:

Consumer service consumers, Provider service provider.
Container service container. Consumption of course invoke provider, invoke this solid line on the graph of course follow the synchronization point. But in the course of the actual call, the position is for Consumer Provider that is transparent, the last call service location (IP address) and the next call to the service location, is uncertain. This place would need to use the registry to achieve soft load, we usually use the registry as a zookeeper, which is the official recommendation;

Register
the service provider first start start, then register registration service. Consumer subscribe subscription service, if not subscribe to the services they want to get, and it will continue to try to subscribe. After registering a new service to the registry, the registry will notify these services to consumers.

Monitor
This is a monitoring, a dashed line indicates that Consumer and Provider send messages asynchronously to Monitor, Consumer and Provider information will be stored on the local disk, the average 1min sends a message. Monitor is optional (the dashed line is not an optional means) throughout the architecture, Monitor function needs to be configured separately, not configured or configuration later, Monitor hang up and call does not affect the service.

The second map is dubbo run flow analysis chart, also dubbo call link diagram,
Here Insert Picture Description

This figure too many things involved, at first glance more strenuous, but if you figure out the key modules inside, basically understand,

Dubbo frame design divided a total of 10 layers, the top layer is left to the Service actually want to use the developer Dubbo development of distributed services implement business logic interface layer. The figure for the consumer to use the service interface, the right light green left light blue background interface for service providers to use the background, located in central axis used for both interfaces.
Now, in conjunction with Dubbo official documents, we were understanding about the framework layered architecture, the various levels of design points:

  1. Interface Layer Service (Service): This layer is associated with the actual business logic, according to a service provider and the service consumer interface design and implementation of services corresponding.

  2. Layer configuration (Config): External configuration interface to ServiceConfig ReferenceConfig center and can be directly new configuration class, the configuration may be arranged to generate class by parsing spring.

  3. Service proxy layer (Proxy): transparent proxy service interfaces, generating a service customer terminal and the server Skeleton Stub, in center ServiceProxy expansion interface ProxyFactory.

  4. Service registration layer (Registry): registration and discovery package service address, to serve as the center URL, extended interface is RegistryFactory, Registry and RegistryService. It may not be registered service center, the Service Provider directly exposed services.

  5. Cluster layer (Cluster): encapsulating a plurality of providers routing and load balancing, and bridging the registration center to center Invoker expansion interface Cluster, Directory, Router and LoadBalance. The combination of multiple service providers as a service provider, to achieve transparency of service consumers, providers only need to interact with a service.
    Monitoring layer (Monitor): RPC number of calls and call time monitoring to Statistics as the center, the expansion interface MonitorFactory, Monitor and MonitorService.

  6. Remote call level (Protocol): The RPC call letters to Invocation and Result for the center, the expansion interface Protocol, Invoker and Exporter. Protocol is a service domain, which is the main entrance features Invoker exposure and reference, which is responsible for the life cycle management of Invoker. Invoker is the entity domain, it is Dubbo's core model, other models are scrambling to rely on it, or convert it, it represents an executable body, can invoke call to initiate it, it is possible that a local implementation, it may is a remote implementation is also possible to achieve a cluster.

  7. Information exchange layer (Exchange): encapsulation request response mode, synchronous asynchronous transfer, to the center Request and Response, extended interface Exchanger, ExchangeChannel, ExchangeClient and ExchangeServer.

  8. Network transport layer (Transport): abstract mina and netty a unified interface for Message-centric, expansion interfaces for the Channel, Transporter, Client, Server and Codec.

  9. Data sequence layer (Serialize): some tools may be multiplexed, expansion interface Serialization, ObjectInput, ObjectOutput and ThreadPool.

According to the official, the relationship between the respective layers described above, as follows:

  • In RPC, Protocol is the core layer, that is, as long as the Protocol + Invoker + Exporter can complete non-transparent RPC call, and then intercept point on the main course Invoker of Filter.

  • The figure of Consumer and Provider is an abstraction, just want Figure more intuitive understanding of what classes belong to the client and the server, not the cause of Client and Server are many scenes in Dubbo use Provider, Consumer, Registry, Monitor Top node partitioning logic to maintain a unified concept.

  • The Cluster is a peripheral concept, the Cluster aims to more disguised as a Invoker Invoker, so that other people can just focus Protocol layer Invoker, remove or add Cluster Cluster will not affect the other layers, since only one offer when who do not need the Cluster.

  • Proxy layer encapsulates all interfaces transparency agent, while in other layers to Invoker as the center, and only when exposed to the user, only with Proxy will Invoker turn into an interface, or interface turn into Invoker, i.e. remove Proxy Run the RPC layer can be, but not so transparent, less looks like a tune called up local service remote service.

  • The Remoting implementation is achieved Dubbo agreement, if you choose RMI protocol, will not spend the whole Remoting, internal Remoting then designated as the transport layer and Exchange Transport information exchange layer, Transport layer is responsible only for one-way message transmission, is Mina, Netty, Grizzly abstraction, it can also extend over UDP, and the Exchange layer above the transport layer encapsulates Request-Response semantics.

  • Registry Monitor and in fact not one, but an independent node, just to global overview draw together by way of layers.

The above is the basic concepts and terminology dubbo mainly for us to have a global structure dubbo understood more readily understood when the interpretation of the source, below, we dubbo exposed from one end of service, which is one of the producers, to illustrate how the services are exposed to,

In the spring or springboot and dubbo integration, we start a service, when the spring-loaded containers will be to find the appropriate bean, if using xml integration dubbo form of spring, the xml configuration file will be loaded resolved,

Here Insert Picture Description

That is the beginning of such a label would be resolved spring dubbo tag parser as a figure above a bean, so the first step is to expose the service when the vessel started to create and dubbo corresponding tag parser, we know spring there is a very important tag parser, called BeanDefinitionParser, in its implementation classes, you go to resolve all types of modules corresponding to the parser, which we see is called a parser DubboBeanDefinitionParser, this is used to parse the dubbo configuration tab file, that those labels to dubbo beginning,

Here Insert Picture Description

In this tag parser, by entering first break parse this method, i.e. analytical methods xml tag,
Here Insert Picture Description

The role of this method is doing it? By this method we enter a breakpoint, and execute again the process, summarize, the purpose of this method is to parse xml file in those bean beginning dubbo, parsed out, it was stored to the corresponding entity class, which is dubbo advance entity class preset in

Here Insert Picture Description

The initialization process executed when the parser will execute the constructor, xml file parsing each label, and then inside each bean properties and corresponding values ​​into the corresponding config entities for later use,

Here Insert Picture Description

Then parse this method is actually done later work to resolve each one corresponding to the tag is then removed bean property value, the classification into each corresponding entity class, parses, return BeanDefinition;

In the above-mentioned bean parse each tab, there is a very important bean, is serviceBean, this bean is our service call when the bean, so when analyzing the need to start from the bean,

We look into the serviceBean,
Here Insert Picture Description

This serviceBean achieved in InitializingBean afterPropertiesSet method, that is, when the spring loaded container will execute this method, we look at what has been done afterPropertiesSet this method,

Here Insert Picture Description

By breakpoints to see, in fact you can see, this place is mainly to resolve the Configuration tab on bean provider, and then parsed entity class attribute values ​​into the preset for later use, save the entity is serviceBean, resolve after the completion of the property value and save the tag, break into the onApplicationEvent this method,

This is also well understood, because serviceBean also implements ApplicationContextAware this method, the boot is completed it will trigger the callback method in the spring container,
Here Insert Picture Description

We look into the onApplicationEvent this method,
Here Insert Picture Description

The intention of this approach is obvious, that is, when the label bean producers parsed completely, container and refresh this callback method, the interface will expose tags corresponding to the producers, how exposed it? We look directly into this export method,

Implementation of this method in ServiceConfig, the break to go along, we locate the doExport this method, which is the method of exposed concrete implementation services, how exposed it? We continue to look at,
Here Insert Picture Description

Point in, we finally can locate the position of the above method, this method generally other thing to do is check the various judgments are interested in children's shoes can look into the points, followed by break we enter inside this method to doExportUrls ,
Here Insert Picture Description

Enter doExportUrls methods, breakpoints can see, this method go to load the information registry, registry information came from, but also our first step out of time tag resolution properties, ah, this is put RegistryConfig the entity class after obtaining the information registry, they begin to perform the service interface exposed,
Here Insert Picture Description

The problem here is to note that, if we configured with multiple registries, such as there are 20883,20884 and the like, in which the above-mentioned methods will be exposed to a for loop, this method is performed multiple times ah,
doExportUrlsFor1Protocol (protocolConfig, registryURLs)

Here Insert Picture Description

Enter doExportUrlsFor1Protocol this method, we find a way in key locations,
Here Insert Picture Description

Navigate to break through this position, the meaning of this code is quite clear, by proxy factory would we want to expose the service interface, the interface implementation class, and the address of the registered packaged together into a new object and then thrown out, threw this method is to perform, protocol.export (wrapperInvoker), we take a look into the protocol, which found a lot of this interface implementation class, we use here is dubbo the protocol, and RegistryProtocol,

Here Insert Picture Description

After the break, the first protocol will enter the export registry inside this method, this is well understood, exposed when the first registry to be captured,
Here Insert Picture Description

Inside this method is worth noting that there are two, one is doLocalExport () This method, namely the implementation of local exposure, the other is
ProviderConsumerRegTable.registerProvider (originInvoker, registryUrl, registedProviderUrl) ;
the role of this method is that the service provider information, save the list of services, such as information to be exposed to an address list for consumers to get back from the service address list,

Look doLocalExport () This method, break into the local exposure method to enter the final exposure method protocol of dubbo go,
Here Insert Picture Description

This place could we have a question, why not just let dubbo to perform the method of exposure it? Understandably, our service is the first to hang up the registry, if not directly linked to the registry exposed to, which is tantamount to direct, ah, and cluster registry can do, and then do a unified service management , coordination of services and high availability, that is, to the registration center to register, and then dubbo exposure method is called, dubbo of export () to do anything at all?

Here Insert Picture Description

And in front of the export similar, here it is the first interface in the front of packaging services invoker of the information taken out, and then to build a new exposure is, in fact, the most important thing the last sentence of code, openServer (url), well understood where, you always want to go out information dissemination through some channels, to publish it? Nature is a server connection channel ah,

This method is a look into the openServer,
Here Insert Picture Description

Here the process of creating a logical server, in fact, we see dependent jar package can also see that the underlying dubbo final call is netty service, as is the use of netty-server communication, continue to enter createServer this method,

Here Insert Picture Description

This method which has a critical piece of code, that is, through the exchanger Exchangers.bind (url, requestHandler)
to bind the corresponding server url, click on the go all the way, we can see,
Here Insert Picture Description

The last is called netty that the underlying methodology, about netty, do not do much to explain here, we are interested can find some information to learn, to do a lot of the underlying communications middleware has netty shadow, netty doing news transmission aspects of good performance,

Back inside the first RegistryProtocol export method, the implementation of the above finished doLocalExport this method after, dubbo opened a netty server connection channel, hang up the service address,
Here Insert Picture Description

The next service information will be registered by registerProvider this method to a registration list for consumers to look for, how to register it? We break into this method to see,
Here Insert Picture Description

In this method, will eventually have to do is packaged inside the invoker service information, address registryUrl registry, address providerUrl service providers stored together with a time to set the package invoker inside, behind the consumer end use, just from this invoker can get inside to get all of the information;

So far, more than we generally go again dubbo service exposure process, although a bit complicated, but the main is so short, figure out the key steps and principles, basically ok, with a timing diagram below simply services at the above summary eh exposure process,

Here Insert Picture Description

Also remaining on consumer call execution process, you can use the same way by breakpoints to debug it, Qinzhi feel the flow of calls, like, not repeat them here, I hope all of you have children's shoes to understand dubbo help deficiencies, please understand, finally, thank you for visiting!

He published 193 original articles · won praise 113 · Views 230,000 +

Guess you like

Origin blog.csdn.net/zhangcongyi420/article/details/85470082