Dubbo Microservices Deployment: Step-by-Step Guide
Introduction
This guide walks you through the process of deploying a microservices application using Dubbo, a microservices framework. It covers the setup, understanding microservices calls, and utilizing Dubbo's capabilities.
Background
Dubbo focuses on enabling cross-process Remote Procedure Calls (RPC) in a microservices architecture. Communication between service consumers and providers involves a sequence of steps facilitated by the Dubbo framework.
Aims
The primary objective is to understand the intricacies of microservices calls and leverage Dubbo's capabilities.
Difficulty Level
The difficulty level of this guide is categorized as low.
Environmental Requirements
System: Windows, Linux, MacOS
Java Development Kit (JDK): Version 8 and above (JDK17 recommended)
Git
Docker: Optional
Practice
1. Getting Test Engineering
Before starting the tutorial, clone the Dubbo test code from the apache/dubbo-samples
repository using the following command:
git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git
2. Dubbo Samples Project Structure
Upon cloning the repository, the project structure becomes important. It consists of several sections:
codestyle
: Configuration files for development.1-basic
: Basic entry examples.2-advanced
: Advanced usage examples.3-extensions
: Examples demonstrating extension usage.4-governance
: Examples showcasing service governance.10-task
: Examples related to Dubbo learning series.99-integration
: Integration testing.test
: Additional test-related code.tools
: Tools for quick startup of third-party components.
3. Starting a Simple Registration Center
To deploy a microservices application, a registration center is crucial. Dubbo provides a simple starter based on Apache Zookeeper. Execute the following commands based on your operating system:
Windows:
./mvnw.cmd clean compile exec:java -pl tools/embedded-zookeeper
Linux / MacOS:
./mvnw clean compile exec:java -pl tools/embedded-zookeeper
Docker:
docker run --name some-zookeeper -p 2181:2181 --restart always -d zookeeper
Make sure to run the command in a separate terminal to keep it continuously executing.
4. Starting Service Provider
After starting the registration center, the next step is to initiate a service provider. Use the following commands:
Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api "-Dexec.mainClass=org.apache.dubbo.samples.provider.Application"
Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.provider.Application"
Ensure to run this command in a separate terminal.
5. Starting Service Consumer
The final step is to start a service consumer that calls the service provider. This is the core of RPC call. Use the following commands:
Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api "-Dexec.mainClass=org.apache.dubbo.samples.client.Application"
Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.Application"
After executing the command, wait for the log to show "hi, dubbo," indicating a successful service call.
Extended Reading
1. How did the consumer end find the service end?
In this example, a Zookeeper registration center is activated, and service providers write their addresses to the registration center for service consumers to obtain.
Dubbo will store the connection information of the service provider in Zookeeper under /dubbo/interfaceName
with /services/appName
.
Example of data on Zookeeper:
[zk: localhost:2181(CONNECTED) 5] ls /dubbo/org.apache.dubbo.samples.api.GreetingsService/providers
[dubbo%3A%2F%2F30.221.146.35%3A20880%2Forg.apache.dubbo.samples.api.GreetingsService%3Fanyhost%3Dtrue%26application%3Dfirst-dubbo-provider%26...]
2. How did the consumer end initiate the request?
Dubbo's call model involves the interface as a bridge connecting service consumers and providers.
In this example, the GreetingsService
interface has a method called sayHi
. Consumers get this interface through Dubbo's API and then call the method as a regular Java interface:
GreetingsService service = reference.get();
String message = service.sayHi("dubbo");
3. Can the service end deploy multiple?
Yes, the guide demonstrates how to start a cluster of service ends:
Start a registration center (refer to Section 3).
Modify the data returned by the service provider to return a different message.
Start the first service provider (refer to Section 4).
Modify the data returned by the second service provider.
Start the second service provider.
Start service consumers.
A consumer application (AlwaysApplication
) that initiates regular calls is provided and can be activated by the following commands:
Windows:
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-api "-Dexec.mainClass=org.apache.dubbo.samples.client.AlwaysApplication"
Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-api -Dexec.mainClass="org.apache.dubbo.samples.client.AlwaysApplication"
After starting, you'll see logs indicating random transfers between service providers.
4. Is this example complicated?
No, Dubbo requires simple configurations for stable and efficient remote calls.
Here's a simple example of a service provider:
ServiceConfig<GreetingsService> service = new ServiceConfig<>();
service.setInterface(GreetingsService.class);
service.setRef(new GreetingsServiceImpl());
DubboBootstrap.getInstance()
.application("first-dubbo-provider")
.registry(new RegistryConfig(ZOOKEEPER_ADDRESS))
.protocol(new ProtocolConfig("dubbo", -1))
.service(service)
.start();
And a service consumer:
ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
reference.setInterface(GreetingsService.class);
DubboBootstrap.getInstance()
.application("first-dubbo-consumer")
.registry(new RegistryConfig(ZOOKEEPER_ADDRESS))
.reference(reference)
.start();
GreetingsService service = reference.get();
String message = service.sayHi("dubbo");
Subscribe to my newsletter
Read articles from Harshit Nagpal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by