Getting Started with Apache Dubbo: Basic Demo without Registry and with Zookeeper Registry
This article introduces Apache Dubbo, explains its core concepts and RPC fundamentals, and provides step‑by‑step demo projects—first a simple setup without a service registry using direct URL calls, then an enhanced version that registers services with Zookeeper, including all necessary Maven dependencies, Spring XML configurations, Java code, and execution results.
In this tutorial the author, "Lao Tian", presents a practical introduction to Apache Dubbo, a high‑performance, lightweight open‑source RPC framework originally developed by Alibaba and now part of the Apache ecosystem.
What is Dubbo? Dubbo is a Java‑based RPC framework that enables remote method invocation as if calling local methods. It offers six core capabilities: high‑performance interface‑based RPC, intelligent fault‑tolerance and load balancing, automatic service registration and discovery, strong extensibility, runtime traffic scheduling, and visual service governance.
What is RPC? RPC (Remote Procedure Call) allows a program on one server to invoke methods on another server over the network, abstracting the communication details.
Besides Dubbo, other popular RPC frameworks include gRPC, Thrift, Motan, etc.
Dubbo Core Roles
Registry – registers service providers and allows consumers to discover them.
Provider – the service implementation that registers its address with the registry.
Consumer – looks up provider URLs from the registry and invokes remote methods.
Monitor – collects call count and latency statistics.
Container – a simple Java main container (instead of a web container) that starts the Dubbo service.
The following sections demonstrate two complete demo projects.
Demo 1: No Registry (Direct URL)
Project structure: dubbo-demo, dubbo-demo-api, dubbo-demo-provider, dubbo-demo-consumer.
pom.xml dependencies
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>com.tian.dubbo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>API module defines the service interface:
package com.tian.dubbo.service;
public interface DemoService {
String sayHello(String msg);
}Provider configuration (application.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-provider"/>
<dubbo:registry address="N/A"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.tian.dubbo.service.DemoService" ref="demoService"/>
<bean id="demoService" class="com.tian.dubbo.service.DemoServiceImpl"/>
</beans>Provider implementation
package com.tian.dubbo.service;
public class DemoServiceImpl implements DemoService {
public String sayHello(String msg) {
System.out.println("msg= " + msg);
return "SUCCESS";
}
}Provider entry point
package com.tian.dubbo;
import org.apache.dubbo.container.Main;
public class ProviderMain {
public static void main(String[] args) {
Main.main(args);
}
}Running ProviderMain starts the provider and prints startup logs confirming the service is listening on port 20880.
Consumer configuration (application.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer"/>
<dubbo:reference id="demoService"
interface="com.tian.dubbo.service.DemoService"
url="dubbo://127.0.0.1:20880/com.tian.dubbo.service.DemoService"/>
</beans>Consumer entry point
package com.tian.dubbo;
import com.tian.dubbo.service.DemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumerMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/application.xml");
DemoService demoService = context.getBean(DemoService.class);
System.out.println(demoService.sayHello("tian"));
}
}Running ConsumerMain prints "SUCCESS" and the provider logs the received message, confirming a successful direct‑URL call.
Demo 2: Using Zookeeper Registry
To enable service discovery, add the Zookeeper dependency to both provider and consumer modules:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.0.4</version>
<type>pom</type>
</dependency>Update the provider application.xml registry entry:
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="10000"/>Start the provider; it registers itself to Zookeeper (visible via Zookeeper UI).
Modify the consumer application.xml similarly, and remove the explicit url attribute from the dubbo:reference element, letting the consumer discover the provider through Zookeeper:
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="10000"/>
<dubbo:reference id="demoService" interface="com.tian.dubbo.service.DemoService"/>Running ConsumerMain now obtains the provider address from Zookeeper and successfully invokes the remote method, with additional registry‑related logs displayed.
The same approach works with other registries such as Nacos by changing the address attribute to nacos://127.0.0.1:8848.
Conclusion
The article demonstrates two introductory Dubbo scenarios: a minimal setup using direct URLs and a more realistic setup that leverages Zookeeper for service registration and discovery, providing complete Maven, Spring XML, and Java code examples.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
