Reading Dubbo Source Code - 1. Setup the Environment and Run Demo

07/31/2020 posted in  Server and RPC

0. New Series

I'll start a new series on Reading Dubbo Source Code.
It might not be as popular as Spring Cloud when used in building microservices. But I think its source code is the best learning material for architecting high performance RPC framework.

1. High level architecture and usage

Dubbo is a Registry based RPC framework. Provider (service) registers endpoint and metadata to Registry. Consumer (client of service) subscribes and caches the service endpoint from Registry.

To use the RPC framework, Service side provides a java interface to the client. Client uses that interface to call the methods.
Dubbo Spring integration provides a bean whose type is the interface and the implementation is a proxy (getting endpoint from Registry center and calling provider service).

2. Import to IntelliJ

git clone

IntellJ -> Open -> find your dubbo folder
IntellJ will automatically recognize it as a maven project and downloads dependencies.

3. Dubbo Modules (Artifacts)

4. Local Debug

4.1 Setup Zookeeper as Registry Center

Any version will do - we can use release line 3.6 because ZooKeeper clients from 3.4 and 3.5 branch are fully compatible with 3.6 servers.

The start script uses zoo.cfg as config file.
The default path is ./conf. As a local test environment, I will just copy the provided ./conf/zoo_sample.cfg to ./conf/zoo.cfg to make the start script work. This is a basic config that setup port to 2181 for this single host server and store data into /tmp folder.

cp ./conf/zoo_sample.cfg ./conf/zoo.cfg
./bin/ start

4.2 Provider and Consumer

dubbo-demo module provides three flavors to use Dubbo. The service provider is DemoService and the consumer is just a main method. Let's take the XML configured Spring as example.

Service provider creates the actual serviceImpl bean, and pass it to interface definition:

<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/> 
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>

Service provider needs to register itself to Registry Center.

<dubbo:registry address="zookeeper://"/> 

Then the client side can use the interface class to create a proxy bean, and call the methods of the bean like local methods.

<dubbo:reference id="demoService" check="false"  

Client side code:

DemoService demoService = context.getBean("demoService", DemoService.class);
CompletableFuture<String> hello = demoService.sayHelloAsync("world");

4.3 Run

We use maven to build Dubbo. It can be done in CLI or in IntelliJ maven plugin.

mvn clean install -Dmaven.test.skip=true 

Then we can start server and client just by running their main methods (Click the run on main() in IntelliJ, or use the CLI given in dubbo-demo/ which is the basic way to run a jar java -jar jarName.jar). Yes, the server is not using any Servlet container. Dubbo uses Netty to serve dubbo protocol!

Logs from server shows it automatically register its current IP address (my laptop ip in LAN) and port (default 20880) to zookeeper (, with supported methods and interface name.

[01/08/20 01:29:26:649 PDT] main  INFO config.ServiceConfig:  
[DUBBO] Register dubbo service org.apache.dubbo.demo.DemoService url 
&metadata-type=remote &methods=sayHello,sayHelloAsync&pid=88546&qos.port=22222&release=
&side=provider&timestamp=1596270566561 to registry registry://
application=demo-provider &dubbo=2.0.2&metadata-type=remote&pid=88546&qos.port=22222
&registry=zookeeper&timestamp=1596270566555, dubbo version: , 
current host:

[01/08/20 01:29:26:927 PDT] main  INFO transport.AbstractServer:  
[DUBBO] Start NettyServer bind /, 
export /, dubbo version: , current host:

Client side log shows it successfully gets the endpoint of service from Zookeeper.

[01/08/20 01:41:52:344 PDT] main  INFO zookeeper.ZookeeperRegistry:  
[DUBBO] Register: consumer://
dubbo version: , current host:

[01/08/20 01:41:52:372 PDT] main  INFO zookeeper.ZookeeperRegistry:  
[DUBBO] Subscribe: consumer://
dubbo version: , current host:

[01/08/20 01:41:52:712 PDT] NettyClientWorker-1-1  INFO netty4.NettyClientHandler:  
[DUBBO] The connection of / -> / is established., 
dubbo version: , current host:

4.4 Zookeeper Review

$ bin/ -server

[zk: 1] ls /
[dubbo, zookeeper]

[zk: 2] ls -R /dubbo


(I only list leaf nodes in above ls -R /dubbo results.)

The last item is the Dubbo URL which also got logged when service starts - "Register dubbo service...".

5. Dubbo URL

URL is used as data structure of protocol. The metadata are stored as key-value pairs in URL parameters.

&metadata-type=remote &methods=sayHello,sayHelloAsync&pid=88546&qos.port=22222&release=

6. Further questions that I don't have the answers yet

  1. How does the client configure retry and timeout?
  2. Does the client use any connection pool? How to configure the pool size and idle connection timeout?
  3. How does the client authenticate itself when calling server? or How does the server protect itself being called by unauthorized clients?
  4. What is the minimal dependency for the client and server to use dubbo? Is it <artifactId>dubbo-dependencies-bom</artifactId>)?
  5. What is the difference between <artifactId>dubbo-dependencies-bom</artifactId> and <artifactId>dubbo-bom</artifactId>?