Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Time:2021-4-21

Write on the front

In the last article《The glacier is starting to attack Dubbo!》In this article, we focus on why we need to learn Dubbo, and we also need to deeply understand the principle and core source code of Dubbo. Since we are going to write a series of special topics on in-depth analysis of Dubbo’s source code, the first thing we need to do is to build a source code environment for Dubbo, just as the saying goes, “if you want to do a good job, you must first sharpen your tools.”. However, in order to better understand Dubbo, I divide this article into three partsThe core role of Dubbo, building Dubbo source environment, Dubbo core module description, running Dubbo sample programThere are four parts. Do what you say and focus on it.

Note: this series is based on Dubbo 2 . seven . 8 version of the source code analysis.

The article has been included in:

https://github.com/sunshinelyz/technology-binghe

https://gitee.com/binghe001/technology-binghe

The core role of Dubbo

To better illustrate the core role of Dubbo, I will directly refer to a dependency diagram of Dubbo.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Note: the picture is from Dubbo website.

From the dependency diagram of Dubbo, we can see that Dubbo is mainly composed of four partsRegistry, provider, consumer and monitor. Next, we will give a brief introduction to these four parts.

  • Registry, registry. In Dubbo, the registry is responsible for the registration and discovery of services, mainly for the registration and search of service address. It is worth mentioning that in Dubbo, providers and consumers interact with the registry only when the service is started. After that, the registry perceives the existence of the provider in the form of long connection. If the provider is down or unavailable, the registry will immediately push related events to inform the consumer.
  • Provider, service provider. When the provider starts, it will register its own related services with the registry, mainly by encapsulating its own service address and related configuration information into a URL and adding it to services such as zookeeper.
  • Consumers, consumers of services. When a consumer starts up, it will subscribe to the services it cares about from the registry. It is mainly to obtain the URL registered by the provider from services such as zookeeper, and add the corresponding listener in services such as zookeeper. After obtaining the URL registered by the provider, the consumer will select one of the obtained providers through the load balancing algorithm, establish a connection with it, and initiate an RPC call. If the provider registered in services such as zookeeper changes, the consumer will get the latest provider information through the listener added in the registry. Moreover, the consumer will cache the information of the provider. Once the connection between the consumer and the provider is established, even if the registry is down or unavailable, the interaction between the consumer and the provider will not be affected.
  • Monitor: Monitoring Center. It is mainly used to count the call times and call time of Dubbo service. In the core architecture of Dubbo, the monitoring center is not necessary. The downtime or unavailability of the monitoring center will not affect the overall service of Dubbo.

OK, so far as the core role of Dubbo is concerned. For more information, please refer to Dubbo’s official documents.

Building Dubbo source code environment

We can use the following command to download the source code of GitHub locally.

git clone https://github.com/apache/dubbo.git

Next, switch the source code of Dubbo to 2 . seven . eight

git checkout -b dubbo-2.7.8 dubbo-2.7.8

Compiling with maven

mvn clean install -Dmaven.test.skip=true 

Convert to idea project, here I use idea analysis Dubbo source code.

mvn idea:idea

Next, we can import the Dubbo source code into idea.

Having said so much, there is another way to download the source code of Dubbo 2.7.8 directly to the local through the browser.

Open link in browser:https://github.com/apache/dubbo/releases/tag/dubbo-2.7.8Download Dubbo source code.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Download the zip package and tar here . GZ package can be downloaded to the local decompression, it can be imported into the idea.

After the import, we see the project structure as follows.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Next, we will give a brief introduction to the core modules in Dubbo source code.

Dubbo core module description

Dubbo common module

The public module of Dubbo provides general functions such as implementation of Dubbo SPI, implementation of time wheel and dynamic compilation.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo remoting module

Among them, Dubbo remoting API is the core abstraction of the whole module, and other sub modules implement Dubbo remoting API based on other open source frameworks.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo RPC module

Dubbo’s RPC module relies on Dubbo remoting module. Among them, Dubbo remoting API is the core abstraction of the whole Dubbo RPC module, and other modules are the implementation of Dubbo remoting API.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo registry module

The module in Dubbo that interacts with the registry. Among them, Dubbo registry API is the core abstraction of the whole Dubbo registry, and other modules are the concrete implementation of Dubbo registry API.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo config module

The module in Dubbo that resolves the exposed configuration. Among them, Dubbo – config – The API sub module is responsible for handling the relevant configuration when using Dubbo in the way of API – config – The spring sub module is responsible for dealing with the relevant configuration methods when integrated with spring.
Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo metadata module

The metadata module in Dubbo. Among them, Dubbo – metadata – The API is for the entire Dubbo – The abstraction of metadata, and other modules are for Dubbo – metadata – The implementation of API.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

dubbo – Configcenter module

Dubbo’s configuration center module, which provides a variety of service discovery methods and access to a variety of service discovery components.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

dubbo – Monitor module

The monitoring module of Dubbo is mainly used to count the number of service calls, call time and the services to realize call chain tracking.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo cluster module

Dubbo’s cluster management module mainly provides load balancing, fault tolerance, routing and other functions.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Run the Dubbo sample program

In the Dubbo source code, there is a sample program module Dubbo demo. Before running the example in Dubbo demo module, we first start a zookeeper locally as the registry.

Note: you can download zookeeper from Apache website.

Dubbo sample program structure

The overall structure of the sample program provided by Dubbo is as follows.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Let’s take a look at Dubbo – What are the modules in demo.

  • Dubbo demo interface: the business interface defined by the Dubbo example.
  • Dubbo demo XML: provides an example of using spring XML.
  • Dubbo demo annotation: provides an example of using spring based annotation.
  • Dubbo demo API: provides an example of using Dubbo as an API.

Among them, Dubbo – demo – xml、dubbo – demo – Annotation and Dubbo – demo – API modules all rely on Dubbo – demo – Interface module.

Next, we will briefly introduce the core code of Dubbo demo interface module and Dubbo demo annotation module, and run relevant sample programs. Partners can analyze and run the sample programs in Dubbo demo XML and Dubbo demo API, and run the relevant code.

(1) Dubbo demo interface: defines the business interface.

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

The core code of demoservice interface is as follows.

package org.apache.dubbo.demo;
import java.util.concurrent.CompletableFuture;
public interface DemoService {
    //Synchronous call
    String sayHello(String name);
    //Asynchronous call
    default CompletableFuture<String> sayHelloAsync(String name) {
        return CompletableFuture.completedFuture(sayHello(name));
    }
}

(2) Dubbo demo annotation: provides a sample program based on spring annotation.

Provider code

Let’s start with the Dubbo demo annotation provider module, which is the service provider. The code for its demoserviceimpl is shown below.

@DubboService
public class DemoServiceImpl implements DemoService {
    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String sayHello(String name) {
        logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        return null;
    }
}

The code for the application class is as follows.

public class Application {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        context.start();
        System.in.read();
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.provider")
    @PropertySource("classpath:/spring/dubbo-provider.properties")
    static class ProviderConfiguration {
        @Bean
        public RegistryConfig registryConfig() {
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("zookeeper://127.0.0.1:2181");
            return registryConfig;
        }
    }
}

Consumer code

Next, let’s take a look at the code of the Dubbo demo annotation consumer module, which is the example code of the service consumer. The code of demoservicecomponent class is as follows.

@Component("demoServiceComponent")
public class DemoServiceComponent implements DemoService {
    @DubboReference
    private DemoService demoService;

    @Override
    public String sayHello(String name) {
        return demoService.sayHello(name);
    }
    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        return null;
    }
}

The code for the application class is as follows.

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        DemoService service = context.getBean("demoServiceComponent", DemoServiceComponent.class);
        String hello = service.sayHello("world");
        System.out.println("result :" + hello);
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.consumer.comp")
    @PropertySource("classpath:/spring/dubbo-consumer.properties")
    @ComponentScan(value = {"org.apache.dubbo.demo.consumer.comp"})
    static class ConsumerConfiguration {

    }
}

Run the Dubbo sample program

We start zookeeper locally, and then run the application class of Dubbo demo annotation provider module and Dubbo demo annotation consumer module respectively.

At this time, the console of idea will output the following information.

result :Hello world, response from provider: 192.168.0.5:20880

Overlooking the overall situation of Dubbo, you must master these before reading the source code!!

Dubbo summary

Here, we introduce the core role of Dubbo, how to build the source code environment of Dubbo, briefly explain the core module of Dubbo source code, and simply analyze the sample program of Dubbo and run the sample program. Among them, in the introduction and running of the sample program, we focus on the Dubbo demo annotation sample module. The partners can analyze and run other sample modules by themselves. When we analyze the source code in the following articles, we mainly use the example program of debug Dubbo.

OK, that’s all for today. I’m glacier. If you have any questions, you can leave a message below or add my wechat: sun_ shine_ Lyz, communicate technology, advance and force~~