Building a Simple Distributed Service with SpringBoot and Dubbo
This tutorial explains key concepts such as distributed systems, RPC, and Dubbo, then guides you through installing Zookeeper, creating Maven modules, configuring SpringBoot and Dubbo, implementing service interfaces, providers, and consumers, and testing a simple HelloWorld distributed service.
Before the hands‑on part, the article introduces essential concepts: what a distributed system is, the role of RPC, and an overview of Apache Dubbo as a high‑performance Java RPC framework.
It then describes Dubbo's architecture, listing the Provider, Consumer, Registry, Monitor, and Container nodes, and explains the service registration and invocation flow.
Next, the tutorial walks through setting up Zookeeper as the service registry on a CentOS 7.4 server, covering download, extraction, configuration of the data directory, and starting the Zookeeper server.
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz tar -zxvf zookeeper-3.4.12-alpha.tar.gz mv zookeeper-3.4.12 zookeeper rm -rf zookeeper-3.4.12.tar.gz mkdir data cp zoo_sample.cfg zoo.cfg vim zoo.cfg # set dataDir=/usr/local/zookeeper/data ./zkServer.sh startAfter Zookeeper is ready, the guide creates the dubbo‑interface Maven module, defines a simple HelloService interface, and packages it as a JAR.
package top.snailclimb.service;
public interface HelloService {
String sayHello(String name);
}The dubbo‑provider project is then created as a SpringBoot application, adding dependencies for Dubbo, Zookeeper, and the interface module. The application.properties file configures the Dubbo application name, registry address, and server port.
# configuration
server.port=8333
spring.dubbo.application.name=dubbo-provider
spring.dubbo.application.registry=zookeeper://<ip>:2181The provider implements the interface and annotates the class with Dubbo's @Service and Spring's @Component:
package top.snailclimb.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import top.snailclimb.service.HelloService;
@Component
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}The main class enables Dubbo auto‑configuration:
package top.snailclimb;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}Similarly, the dubbo‑consumer project is set up, adding the same dependencies and configuring Dubbo. A simple REST controller uses @Reference to inject the remote HelloService and expose an endpoint:
package top.snailclimb.dubboconsumer;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.snailclimb.service.HelloService;
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/hello")
public String hello() {
String hello = helloService.sayHello("world");
System.out.println(helloService.sayHello("SnailClimb"));
return hello;
}
}The consumer's main class also enables Dubbo:
package top.snailclimb.dubboconsumer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}Finally, accessing http://localhost:8330/hello returns "Hello world" and the console prints "Hello SnailClimb", confirming that the SpringBoot‑Dubbo distributed service works as expected.
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
