Introduction to Dubbo - build the simplest Demo framework

Posted by sebastienp on Thu, 10 Mar 2022 07:44:24 +0100

Dubbo background and introduction

Dubbo began with e-commerce system, so let's start with the evolution of e-commerce system.

Single application framework (ORM)
When the website traffic is very small, only one application is needed to deploy all functions together, such as the following single payment, so as to reduce the deployment nodes and costs.
Disadvantages: a single system architecture makes it more and more resources occupied in the development process, and it is more and more difficult to maintain with the increase of traffic




Vertical application framework (MVC)
The vertical application architecture solves the capacity expansion problem faced by a single application architecture. The traffic can be dispersed to each subsystem, and the volume of the system is controllable. To a certain extent, it reduces the cost of coordination and maintenance among developers and improves the development efficiency.  
Disadvantages: however, in the vertical architecture, the same logic code needs to be copied continuously and cannot be reused.



Distributed application architecture (RPC)
When there are more and more vertical applications, the interaction between applications is inevitable. Extract the core business as an independent service and gradually form a stable service center



Mobile computing architecture (SOA)
With the further development of service-oriented, there are more and more services, and the invocation and dependency between services are becoming more and more complex. Service-oriented architecture (SOA) was born. Therefore, a series of corresponding technologies were derived, such as service provision, service invocation, connection processing, communication protocol, serialization method, service discovery, service routing Service framework encapsulated by log output and other behaviors

From the above evolution of e-commerce system, we can see the evolution process of Architecture:



Single application architecture

• it will only take one hour to deploy all applications and functions together.
• at this time, the data access framework (ORM) used to simplify the workload of addition, deletion, modification and query is the key.

Vertical application architecture

• when the number of visits increases gradually, the acceleration brought by a single application increases and the machine decreases. Split the application into several unrelated applications to improve efficiency.
• at this time, the Web framework (MVC) for accelerating front-end page development is the key.

Distributed service architecture
• when there are more and more vertical applications, the interaction between applications is inevitable. Extract the core business as an independent service, and gradually form a stable service center, so that the front-end application can respond to the changing market demand more quickly.
• at this time, the distributed service framework (RPC) for improving business reuse and integration is the key.
Flow computing architecture

• when there are more and more services, problems such as capacity evaluation and waste of small service resources gradually appear. At this time, it is necessary to add a dispatching center to manage the cluster capacity in real time based on the access pressure and improve the cluster utilization.
• at this time, the resource scheduling and Governance Center (SOA) for improving machine utilization is the key.

Here is an introduction to RPC:
RPC(Remote Procedure Call Protocol): remote procedure call:
Two servers A and B deploy different applications A and B respectively. When server A wants to call the function or method provided by application B on server B, it cannot be called directly because it is not in A memory space. It needs to express the semantics of the call and convey the data of the call through the network.
To put it bluntly, if you write a program on your machine, I can't call it directly. At this time, there is a concept of remote service call.

RPC It is a protocol that requests services from remote computer programs through the network without understanding the underlying network technology. RPC The protocol assumes the existence of some transmission protocols, such as TCP or UDP,Carry information data between communication programs. stay OSI In the network communication model, RPC It spans the transport layer and application layer. RPC It makes it easier to develop applications including network distributed multi programs. 
RPC Adopt client/Server mode. The requester is a client, and the service provider is a server. First, the client invokes the process to send a call information with process parameters to the service process, and then waits for the response information. On the server side, the process stays asleep until the call information arrives. When a calling message arrives, the server obtains the process parameters, calculates the result, sends the reply information, and then waits for the next call information. Finally, the client calls the process to receive the reply information, gets the process result, and then calls the execution to proceed.

Problems to be solved by RPC:
(you can learn more about it and check other blog posts for details)

• communication problem: mainly by establishing a TCP connection between the client and the server, all the exchanged data of remote procedure calls are transmitted in this connection. The connection can be an on-demand connection, which is broken after the call, or a long connection. Multiple remote procedure calls share the same connection.
• addressing problem: how does the application on server A tell the underlying RPC framework, how to connect to server B (such as host or IP address) and specific port, and what is the name of the method, so as to complete the call. For example, RPC based on Web service protocol stack needs to provide an endpoint URI or find it from UDDI services. If it is called by RMI, an RMI Registry is also required to register the address of the service.
• serialization and deserialization: when an application on server A initiates A remote procedure call, the parameters of the method need to be passed to server B through the underlying network protocol such as TCP. Because the network protocol is based on binary, the values of the parameters in memory should be serialized into binary form, that is, Serialize or marshal, The serialized binary is sent to the B server through addressing and transmission.
Similarly, server B receives parameters and deserializes them. The results returned by the application of server B after calling its own method for processing shall also be serialized to server a, and the reception of server A shall also go through the process of deserialization.

What is Dubbo

Dubbo is:

• a distributed service framework
• high performance and transparent RPC remote service invocation scheme
• SOA Service governance solutions
It provides more than 3 billion visits to more than 2000 services every day, and is widely used in the business of member sites of Alibaba group and other companies.

Dubbo architecture




Provider: the service provider that exposes the service.
Consumer: the service consumer that calls the remote service.
Registry: Registry for service registration and discovery.
Monitor: the monitoring center that counts the number and time of service calls.

Call process
0. The service container is responsible for starting, loading and running the service provider.
1. When the service provider starts, it registers its own services with the registry.
2. Service consumers subscribe to the services they need from the registry when they start.
3. The registration center returns the address list of service providers to consumers. If there is any change, the registration center will push the change data to consumers based on the long connection.
4. From the provider address list, the service consumer selects one provider to call based on the soft load balancing algorithm. If the call fails, select another one to call.
5. Service consumers and providers accumulate call times and call times in memory, and regularly send statistical data to the monitoring center every minute

Dubbo registry

For service providers, they need to publish services, and the number and type of services are expanding due to the complexity of the application system;
For the service consumer, it is most concerned about how to obtain the services it needs. In the face of complex application systems, it needs to manage a large number of service calls.
Moreover, for service providers and service consumers, they may have both roles, that is, they need to provide services and consume services.

Through the unified management of services, the process and management of service release / use by internal applications can be effectively optimized. The service registry can unify the external services through specific agreements.

There are several types of registries available from Dubbo:

Multicast registry
Zookeeper Registration Center
Redis registry
Simple registry
Dubbo advantages and disadvantages


Transparent remote method invocation
-Call remote methods as if they were local methods; Simply configure without any API intrusion.
Soft load balancing and fault tolerance mechanism
It can replace hardware load balancers such as nginx lvs in the intranet.
Service registry automatic registration & Configuration Management
-There is no need to write the service provider address, and the registry automatically queries the provider ip based on the interface name.
Using distributed coordination services like zookeeper as the service registry, you can move most of the project configurations into the zookeeper cluster.
Service interface monitoring and Governance
-Dubbo admin and Dubbo monitor provide perfect service interface management and monitoring functions. For different interfaces of different applications, multi version, multi protocol and multi registry management can be carried out.

Only JAVA language is supported
Dubbo introduction Demo

After understanding Dubbo, it is natural to build a simple Demo implementation. This paper adopts the integration of Dubbo, Zookeeper and Spring framework.

The main steps are as follows:
1. Install Zookeeper and start it;
2. Create MAVEN project and build a simple Demo implemented by Dubbo+Zookeeper+Spring;
3. Install Dubbo admin to realize monitoring.

1. Introduction and installation of zookeeper

The Dubbo registry in this Demo uses Zookeeper. Why Zookeeper?

Zookeeper It is a distributed service framework. It is the data storage of tree directory service. It can manage data in a cluster. It can be used as a good example here Dubbo Service registry.
Dubbo Can and Zookeeper Cluster deployment is realized. When the provider has abnormal shutdown such as power failure, Zookeeper The registration center can automatically delete the provider information. When the provider restarts, it can automatically restore the registration data and subscribe to the request


The specific installation methods are not described here. Please refer to the blog:

After installation, enter the bin directory and start zkserver CMD, a java process will be started in this script:
(Note: you need to start zookeeper first and then run the subsequent dubbo demo code to use the function of zookeeper Registry)


2 create MAVEN project

Project structure:
It is mainly divided into three modules:
Dubbo API: store public interfaces;  
Dubbo consumer: call remote service;  
Dubbo provider: provides remote services.


The code construction process will be described in detail below.
1) First, build the MAVEN project and import the required jar package dependencies.
jar packages such as spring, Dubbo and zookeeper need to be imported.
(refer to the project code provided later for details)

2) Create a MAVEN project for Dubbo API (there is a separate pom.xml, which is packaged for use by providers and consumers).
Define service interface in the project: the interface needs to be packaged separately and shared between service providers and consumers.  


import java.util.List;

public interface DemoService {
    List<String> getPermissions(Long id);

3) create the MAVEN project of Dubbo provider (there is an independent pom.xml, which is used to package for consumers).  
Implement the public interface, which is hidden from consumers:



import java.util.ArrayList;
import java.util.List;
public class DemoServiceImpl implements DemoService {
    public List<String> getPermissions(Long id) {
        List<String> demo = new ArrayList<String>();
        demo.add(String.format("Permission_%d", id - 1));
        demo.add(String.format("Permission_%d", id));
        demo.add(String.format("Permission_%d", id + 1));
        return demo;

The dependency of the public interface needs to be added


Exposing services with Spring configuration declaration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <!--The application information of the provider is defined to calculate the dependency relationship; stay dubbo-admin or dubbo-monitor This name will be displayed for easy identification-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--use zookeeper The service of the registration center should be opened first zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- use dubbo The protocol exposes services on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--use dubbo Protocol implementation defined api.PermissionService Interface-->
    <dubbo:service interface="" ref="demoService" protocol="dubbo" />
    <!--Specific implementation of the interface bean-->
    <bean id="demoService" class=""/>

Start remote service:


public class Provider {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
            System.out.println(context.getDisplayName() + ": here");
            System.out.println("Service started...");


4) Create a MAVEN project of Dubbo consumer (there can be multiple consumers, but they need to be configured).  
Call the required remote service:

Reference remote services through Spring configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--towards zookeeper subscribe provider Address of, by zookeeper Timed push-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--use dubbo Protocol call defined api.PermissionService Interface-->
    <dubbo:reference id="permissionService" interface=""/>

Start the Consumer and call the remote service:

public class Consumer {
    public static void main(String[] args) {
        //Testing General Services
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        System.out.println("consumer start");
        DemoService demoService = context.getBean(DemoService.class);

5) to run the project, ensure that the provider has been run before starting the consumer module:
Operation provider:


The consumer successfully invoked the remote service provided by the provider:


Of course, this is just a simulation project. In reality, there are many providers and consumers, which is much more complex than this. Of course, only in this way can we reflect the characteristics of dubbo.

Introduction to Dubbo management console

Management console functions

Routing rules, dynamic configuration, service degradation 
Access control, weight adjustment 
load balancing  


Download Dubbo admin and install it according to the online introduction. The general approach is to replace the contents of a folder in Dubbo admin with the conf of tomcat, and then run Tomcat. However, in practice, I found that JDK8 could not run. Later, I found a Dubbo admin version that JDK8 could implement. Download address: .

After the user name and password root are successfully opened, you can enter the console home page to view the consumer provider:
View provider:


View consumer:


At present, Alibaba has started to update again. If you are interested, you can check:

The code of the whole project has been uploaded to my GitHub , welcome to view.
(the pictures in the project on github are the contents of the blog and can be deleted completely).

reference resources:


Topics: Middleware