1、 Dubbo registry and service registration source analysis


1Overall design

Dubbo overall design and call with link reference to the official website http://dubbo.apache.org/zh-cn/docs/dev/design.html

2Dubbo’s registry

  1、Please refer to the official website of the registration center  http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html

   2、ZK Registration Center

    2.1 directory structure

    +- dubbo

     +- com.demo.service.HelloService

     +- consumers

      +- consumer://***

     +- providers

      +- dubbo://***
     +- configuration
     +- routers



(1) When the service starts, Dubbo will create a Dubbo directory under the root directory of ZK
(2) Under, Dubbo and node are the current interface names. If there are multiple interfaces, they will be expanded in the form of multiple child nodes
(3) There are four configuration items under each service
1) , consumers: list of all consumers under the current service (URL)
2) , providers: list of all providers (URLs) under the current service
3) , configuration: for the configuration information under the current service, the provider or consumer will obtain the configuration information by reading the configuration information here
4) , routers: when consumers obtain providers, they will adapt the matching rules through the configured routes
    2.2. URL in Dubbo
From 2.1, it is not difficult for us to find that both service providers and service consumers are saved in the form of URL in the registration center ZK for resource location, so let’s take a specific look at the URL in Dubbo
Rules: protocol://host :port/path?key=value&key=value
Example: refer to consumer or producer in the directory structure in 2.1
(1) Corresponding source code (construction method)

(2) Main components

1) Protocol: generally, our provider or consumer are man-made specific protocols

2) , host: the specific address of the current provider or other protocols. For example, the host specified by the override protocol is, which means that all machines are effective

3) port: the same as above, representing the port number to be processed

4) path: service path, which represents our real business interface in provider or consumer

5) , key = value: these represent specific parameters, which can be understood as the configuration of this address. For example, the service application name of a specific machine in our provider can be set in a configuration way
(3) The difference between URL in Dubbo and URL in Java
1) The URL in Dubbo supports the dynamic increase and decrease of parameters



2.3 service local cache




As can be seen from the above two figures, service providers will actively register their services in the registry when they start, while service consumers will also go to the registry to obtain their referenced service list locally and save it to a file when they start, and need to subscribe to the corresponding service provider node. When the service provider has information change, the consumer will receive the node change notification, and then get the latest service list and the new local cache file.

Dubbo will create a registry object and load the local cache file in the process of service reference. It will give priority to subscribe to the registry. If the subscription registry fails, it will access the content of the local cache file to obtain service provision information
Now let’s take a look at the specific source code
1) , top level interface org.apache.dubbo . registry.Registry

You can see that there are two methods: 1. Register; 2. Cancel.

org.apache.dubbo . registry.RegistryService This class is mainly used to register, log off, listen, cancel listening and query the specified path. It is also the most basic class in the registry

Abstract registry is the encapsulation of the registry, which mainly encapsulates the local registration address. Its main function is to use the local registry when the remote registry is not available

Failbackregistry can automatically recover from failure, record failed requests in the background, and resend them regularly

Next, let’s look at his implementation class org.apache.dubbo . registry.support.AbstractRegistry#AbstractRegistry The concrete realization of the construction method of




In fact, the main thing here is to cache the URL to the local file


    2.4、Service registrationprocess analysis

    1. Service registration sequence diagram



2. Service registration process


Note: the process of service registration can be roughly divided into two processes: 1. A specific service is converted into an invocker; 2. An invocker is converted into an exporter

As can be seen from the above two figures, Dubbo first obtains the actual class ref (such as helloserviceimpl) from the serviceconfig class, then implements the getinvoker method in the class through the proxyfactory interface, and uses ref to generate an abstract proxyinvoker instance. At this step, it completes the transformation from specific services to invokers. The next step is to convert the invoker to the exporter
So when is the service registration triggered? Let’s take a look first org.apache.dubbo . config.ServiceConfig And the inheritance of its subclasses


As you can see from the picture above, org.apache.dubbo . config.spring.ServiceBean It inherits serviceconfig and implements a bunch of spring components. Students familiar with spring should know that in the life cycle of spring container, these components will be initialized.


Next, let’s focus on the proxyfactory, protocol and ref properties in serviceconfig


Next, let’s look at the protocol interface

We can see that the top-level interface of the protocol provides an interface for exposing and referencing services. It has many implementation classes, but we register to use org.apache.dubbo . registry.integration.RegistryProtocol

Next, let’s take a look at how to convert a serviceconfig to an invoker. Because the method is long, we won’t take a detailed screenshot





Now let’s look back org.apache.dubbo . registry.integration.RegistryProtocol#export This method

This method mainly does five things

1. Get the URL of the registry: zookeeper:// :2181/ org.apache.dubbo . registry.RegistryService?application= * * *

2. Get the URL of the service provider: dubbo:// :20880/ com.demo.service .HelloService?anyhost=**

3. Get the registry

4. Register the service provider in the registry

5. Returns the exposed object

Now let’s look at it again org.apache.dubbo . registry.integration.RegistryProtocol . destroyableexporter class

He mainly provides a method to obtain the invocker

Let’s look at the registration method

You can see that the factory mode is used to obtain the specific factory. Next, let’s take a look at the factory interface of the registry

org.apache.dubbo . registry.RegistryFactory It is a registry factory. In this way, you can also ensure that multiple registries can be used in an application. You can see that different protocols are selected through different protocol parameters

Now let’s take a look org.apache.dubbo . registry.support.FailbackRegistry#register


There are three main things to do here

1. Put the current URL into the registered list

2. Process URL for registration failure

3. Actually handle the registration of the current URL

If we look at the registration method that actually processes the current URL, we will find that this is a template method, which needs subclasses to provide specific implementation

Since Dubbo uses ZK as the registry by default, let’s take a look at the implementation of ZK


See here, the process of service registration is OK.