How to Seamlessly Integrate Spring Boot with Dubbo for Remote Service Calls
This tutorial walks through integrating Dubbo with Spring Boot using Nacos as the service registry, showing how to create a shared API module, configure provider and consumer Maven projects, annotate services with @Service and @Reference, and verify successful RPC calls via a simple REST endpoint.
Background
Dubbo is an open‑source high‑performance lightweight RPC framework from Alibaba, offering higher remote‑call efficiency than OpenFeign because it uses TCP instead of HTTP. Early versions only supported ZooKeeper as the registry, which made integration with Spring Cloud cumbersome. Spring Cloud Alibaba introduced Nacos support, allowing Dubbo to use Nacos as a registry and blend seamlessly into the Spring Cloud ecosystem.
1. Create Service Interface
Start a Maven project named nacos-dubbo-api and define the service contract:
public interface HelloService {
String hello(String name);
}2. Build Service Provider
Create a Maven project nacos-dubbo-provider. Add the following dependencies in pom.xml (excerpt):
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<!-- SpringBoot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Dubbo starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Link to the API module -->
<dependency>
<groupId>com.example</groupId>
<artifactId>nacos-dubbo-api</artifactId>
<version>3.0-SNAPSHOT</version>
</dependency>
</dependencies>Configure application.properties:
spring.application.name=nacos-dubbo-provider
server.port=9010
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.scan.base-packages=com.example.cloud.nacos.dubbo.provider.dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://${spring.cloud.nacos.discovery.server-addr}Implement the service:
import com.alibaba.dubbo.config.annotation.Service;
import com.example.cloud.nacos.dubbo.api.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
System.out.println("Received RPC request, param: " + name);
return "hello, I am the provider, received: " + name;
}
}Create the Spring Boot entry point:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Run the provider and open http://127.0.0.1:8848/nacos to see the Dubbo service listed in Nacos.
Click “Details” to view metadata.
3. Build Service Consumer
Create a Maven project nacos-dubbo-consumer with the same dependencies as the provider (omitted for brevity). Use a similar application.properties but with a different port and disable client‑side check:
spring.application.name=nacos-dubbo-consumer
server.port=9011
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.scan.base-packages=com.example.cloud.nacos.dubbo.provider.dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://${spring.cloud.nacos.discovery.server-addr}
dubbo.consumer.check=falseExpose a REST controller that references the Dubbo service:
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.cloud.nacos.dubbo.api.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@GetMapping("/hello")
public String hello(@RequestParam(value = "name") String name) {
String result = helloService.hello(name);
return "Result from Dubbo: " + result;
}
}Run the consumer and verify its registration in Nacos.
Invoke the remote call by browsing to http://127.0.0.1:9011/rpc?name=nacos. The response shows that the consumer successfully called the provider via Dubbo and received the expected string.
4. Key Takeaways
Only the @Service (from com.alibaba.dubbo.config.annotation.Service) and @Reference annotations are required to expose and consume Dubbo services. Be careful not to confuse the Dubbo @Service with Spring’s @Service. Also, avoid circular dependencies between API packages, as they can cause build‑time cycles.
With Nacos support, Dubbo can be integrated into Spring Cloud projects, providing a high‑performance RPC solution for microservice architectures.
References
https://nacos.io/zh-cn/docs/use-nacos-with-dubbo.html
https://spring.didispace.com/spring-cloud/spring-cloud-alibaba-dubbo-1.html
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.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
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.
