This article was first published in the public number [watch code to go to work], it is recommended to pay attention to the public number, read the latest article in time.

mp.weixin.qq.com/s?__biz=M…

Hi, I’m Tin, and this is my 16th original article

Tin started sharing the source code for Dubbo! (This article source based on 2.7, Dubbo3 is not considered)

Before we really dive into Dubbo’s principles and core source code, today we will talk about some basic understanding of Dubbo and how Dubbo works. Without further ado, the contents:

  • What is Dubbo? Come from?

    Dubbo’s four main characters

  • Dubbo source code download

    4. Write and run Dubbo service

    Five,conclusion

What’s a Dubbo? Come from?

Dubbo is an open source high-performance RPC service framework of Alibaba, which is mainly used to support high-performance remote communication between application services. Simply put, it means data transmission between two services.

The most familiar browser visits A web site to obtain data, which eventually corresponds to A specific back-end service A. However, service A may need to obtain data from service B. In this case, the data communication between A and B can use the Dubbo framework.

Dubbo’s official website is dubbo.apache.org/zh/

Source code address: github.com/apache/dubb…

Dubbo had a rough ride, being abandoned and almost dying.

On October 27, 2011, Dubbo, alibaba’s open source servitization governance framework, was launched. At that time, it was the initial stage of the implementation of the design concept of service-oriented governance in China.

On October 23, 2012, Alibaba released Dubbo version 2.5.3. Just after the one-year anniversary of Dubbo’s open source, Alibaba announced to stop the major upgrade of Dubbo, so far, Dubbo is basically in the “abandoned” stage.

Alibaba upgraded Dubbo2.4 twice in 2013 and 2014, and then stopped all maintenance work.

While Alibaba stopped maintaining Dubbo, Dangdang began maintaining its own Dubbo branch version, Dubbox. Before 2017, many Internet companies (like ours) were basically using Dubboxes.

On September 7, 2017, Ali resumed maintenance of Dubbo and quietly released version 2.5.4, nearly five years after the previous version 2.5.3. In the remaining four months of 2017, Alibaba released Dubbo versions 2.5.5, 2.5.6, 2.5.7, and 2.5.8.

Dubbo released version 2.6.0 on January 8, 2018. The new version merges Dubbox, which is open source of Dangdang.com, so far, Dubbo has completed a complete unification.

On February 15, 2018, Ali donated Dubbo to the Apache Software Foundation, resulting in the current Apache Dubbo.

On May 20, 2019, the Apache Software Foundation announced Dubbo’s formal graduation as an Apache Top-level project.

On June 13, 2021, Dubbo3 in the cloud native environment was officially released. Now, the latest version of Dubbo3 is 3.0.5.

Dubbo’s four main characters

Based on the above figure, the basic working process of Dubbo is firstly described, which is mainly divided into four steps:

  • ① The Provider registers service information with the registry.
  • ② The Consumer subscribes to the provider service from the registry;
  • ③ The Consumer invokes the Provider through the service URL.
  • ④ The Consumer and Provider asynchronously report statistics to the monitoring center.

The four modules involved in the workflow are:

  • Registry
  • Service Provider
  • Service Consumer
  • Monitoring Center

Each module represents a role and can exist independently, forming an interdependent relationship between them.

Registry

As a core component of service discovery, the main responsibility is to register and save the service address and other information and search quickly, and it is the intermediate hub of other modules. Registries have several characteristics:

  • (1) Dynamic joining: when a service Provider is newly added to the system, the registry can dynamically perceive and notify the Provider information to all service consumers.
  • (2) Dynamic discovery Similar to the dynamic join feature, the service Consumer is dynamically aware of service configuration changes without doing anything. Therefore, the Consumer only needs to pull the Provider information from the registry once at startup and cache it locally. When the Provider changes, the registry notifies the Consumer to make the change.
  • (3) Unified configuration: because it is a registry, unified configuration can be managed to avoid the problem of inconsistent configuration of each service.

Dubbo supports multiple registries, as can be seen from the dubbo-Registry structure of the source package:But zooKeeper is the one we use most, and Dubbo officially recommends using ZooKeeper.

Service Provider

The Provider registers its related services with the registry at startup, and then communicates with the registry through a long link. If the Provider goes offline or other changes, the registry senses them in real time, and then notifies the Consumer.

If you use a zookeeper as registry, the Provider information is saved in the zookeeper node, forms such as: dubbo/com. XXX. XXX. DemoService/will. Examples of nodes are shown below:In the Providers directory is the URL information of the service provider.

The service Consumer

The Consumer also subscribes to the registry at startup and casses a copy of the service provider’s information to local memory and local disk. The notification mechanism between the registry and the Consumer notifies the Consumer of any changes (such as ZooKeeper, The Consumer simply adds a listener on the corresponding node to listen for changes to the Provider.

Consumer’s caching mechanism is a space for time approach that avoids the problem of drastically reducing system performance by requiring the Consumer to go to the registry to pull a list of services on every remote call. At the same time, if the registry system goes down and becomes unavailable, it will not affect the Consumer to call the Provider to continue running.

Monitoring Center

As the name implies, Monitor is only used for monitoring. For example, when a Consumer makes a call, it asynchronously reports the call and statistics to the monitoring center. The information collected and displayed by the monitoring center includes but is not limited to service list, service configuration, and service invocation times.

Dubbo source code download

Dubbo’s source code has been given above, so let’s download Dubbo’s source code locally.

git clone https://github.com/apache/dubbo.git
Copy the code

Dubbo’s Github address shows that the latest version of Dubbo2.7 is 2.7.15We cut the Dubbo source to 2.7.15

Git checkout -b dubo-2.7.15 git checkout -b dubo-2.7.15 git checkoutCopy the code

The resulting Dubbo source code structure looks like this:Dubbo source modules split very many, but from the name of the basic can see the role of each module.

Dubbo-registry, for example, is the source code for the registry implementation

Dubbo – Remoting is the core implementation of Dubbo network communication, and its bottom layer is developed using NetTYDubbo – RPC is a remote call module for Dubbo that abstracts various protocols, as well as dynamic proxies, and contains only one-to-one calls with no concern for cluster managementDubbo -cmmon is the public logic module of Dubbo, including the Util class and the common modelDubo-cluster is the cluster module of Dubbo. Masquerade multiple service providers as one provider, including load balancing, fault tolerance, routing, etc. The cluster address list can be statically configured or delivered by the registryDubbo -monitor is the monitoring module of Dubbo. Count service invocation times, invocation time, and invocation chain tracing.

Write and run the Dubbo service

Dubbo source code package includes the corresponding demo, which is directly executable, open the source code package, find Dubbo – Demo module:These are all examples of dubbo programs, represented by several modules:

  • Dubo-demo-annotation: An annotation-based demo example
  • Dubo-demo-api: ApI-based demo example
  • Dubo-demo-interface: defines the demo interface
  • Dubo-demo-xml: Demo example of XML-based configuration

Let’s run a dubbo example using the XML configuration (dubo-Demo-xml package).

First, start the Provider

Find dubbo-demo-xmL-provider, and then find the dubbo-provider. XML file where the service provider configuration information is configured:Obviously, the registry that demo uses is ZooKeeper,To get the service running, you also need to start a ZooKeeper locally. Since I am using macOS M1, the steps for installing zK can be found in this article:

zhuanlan.zhihu.com/p/466902641

The DemoService interface is defined in the Dubo-Demo-interface module:

The interface implementation class DemoServiceImpl is provided by dubo-Demo-XML-providerFind the Application class under Dubo-demo-xmL-provider and run the main method:After the provider is successfully started, you can view the node information on ZooKeeper (in the /dubbo directory by default) :

ls /dubbo/org.apache.dubbo.demo.DemoService/providers
Copy the code

From the second figure we can see the URL information for the service provider.

Step 2, start Consumer

Go to dubbo-Demo-xmL-consumer, and then go to dubbo-consumer.xml, where the service consumer configuration is configured:

Dubo-demo-xml-consumer: dubo-demo-xmL-consumer:

After successful startup, corresponding consumer information can also be found on ZooKeeper:

ls /dubbo/org.apache.dubbo.demo.DemoService/consumers
Copy the code

You can also see a line of log printed on the Provider console indicating that a connection has been established with the consumer:The Consumer console keeps printing the result of calling the Provider:At this point, the Dubbo service is successful!

conclusion

I am Tin, an ordinary engineer who is trying to make himself better. My experience is limited, knowledge is shallow, if you find something wrong with the article, you are very welcome to add me, I will carefully review and modify.

Welcome to follow me

The original address is: [mp.weixin.qq.com/s?__biz=M…]

Summary and improvement

Be a happy siege lion

Build a world of your own