Build a Spring Boot 2.7 Dubbo RPC Service: Step‑by‑Step Guide
This tutorial walks through creating a complete Spring Boot 2.7 project that integrates Apache Dubbo for RPC, covering environment setup, Maven dependency management, interface definition, provider and consumer implementations, configuration with Nacos, and testing the end‑to‑end service.
1. Introduction
Apache Dubbo is an RPC framework that solves service governance and communication in micro‑service architectures. It supports multiple languages and provides features such as service discovery, load balancing, and traffic routing. The following example demonstrates how to integrate Dubbo with Spring Boot 2.7.
Environment: SpringBoot 2.7.16, Dubbo 3.2.7, JDK 17.
2. Project Structure
The demo consists of three modules:
dubbo-spring-boot-interface : defines the service interface.
dubbo-spring-boot-provider : implements the interface and exposes the service.
dubbo-spring-boot-consumer : consumes the remote service via Dubbo.
2.1 Parent Project Dependency Management
<properties>
<java.version>17</java.version>
<dubbo.version>3.2.10</dubbo.version>
<nacos.client.version/>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>com.pack</groupId>
<artifactId>dubbo-spring-boot-interface</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
</dependencies>
</dependencyManagement>2.2 Interface Definition
public interface DemoService {
String sayHello(String name);
}2.3 Provider Implementation
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}The @DubboService annotation exposes the service.
Provider Application
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}The @EnableDubbo annotation registers Dubbo components as Spring beans.
Provider Configuration (application.yml)
spring:
application:
name: dubbo-provider
---
server:
port: 8081
---
dubbo:
application:
name: ${spring.application.name}
protocol:
name: dubbo
port: -1
registry:
address: nacos://127.0.0.1:8848
parameters:
username: nacos
password: nacos
group: dubbo
namespace: a4be0946-9e32-4414-b844-7c91299f5d78Provider setup is now complete.
2.4 Consumer Implementation
Consumer Dependencies
<dependency>
<groupId>com.pack</groupId>
<artifactId>dubbo-spring-boot-interface</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>Consumer Application
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication implements CommandLineRunner {
@DubboReference
private DemoService demoService;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(this.demoService.sayHello("张三"));
}
}The @DubboReference annotation injects a proxy for the remote service.
Consumer Configuration (application.yml)
spring:
application:
name: dubbo-consumer
---
server:
port: 8082
---
dubbo:
application:
name: ${spring.application.name}
qos-port: 33333
protocol:
name: dubbo
port: -1
registry:
address: nacos://127.0.0.1:8848
parameters:
username: nacos
password: nacos
group: dubbo
namespace: a4be0946-9e32-4414-b844-7c91299f5d78
register-consumer-url: true2.5 Testing
Start the provider module first. If a JVM error occurs, add the following VM option: --add-opens java.base/java.lang=ALL-UNNAMED After the provider starts, you can see it registered in the Nacos console:
Next, start the consumer module. The console shows the remote call result:
The consumer successfully invokes the provider’s sayHello method.
This completes a full end‑to‑end Spring Boot + Dubbo integration example.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
