Master Dubbo with Spring Cloud Alibaba: A Step‑by‑Step Guide
This tutorial walks through integrating Dubbo into a Spring Cloud Alibaba project using Nacos as the service registry, covering interface definition, provider and consumer setup, Maven dependencies, configuration details, testing, and the benefits of combining Dubbo's RPC performance with Spring Cloud's ecosystem.
Earlier I wrote an article comparing Spring Cloud and Dubbo, noting that Spring Cloud is a complete architecture solution while Dubbo is only a service‑governance and RPC implementation, which makes a direct comparison unfair.
Because Dubbo has a huge user base in China but lacks a complete ecosystem, many developers try to combine it with Spring Cloud. Before Spring Cloud Alibaba, integration was cumbersome due to mismatched registration centers (Dubbo uses ZooKeeper, Spring Cloud originally did not). Spring Cloud Alibaba solves this by providing Nacos as a unified service registry.
Getting Started Example
We will use Nacos as the service registry to demonstrate a simple Dubbo provider and consumer.
Define Service Interface
public interface HelloService {
String hello(String name);
}Provider Setup
Step 1: Create a Spring Boot project and add the following Maven dependencies (ensure spring-boot-starter-actuator is present and use the correct groupId for the Dubbo starter).
<dependencies>
<!-- API package built in the first step -->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>alibaba-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Alibaba Dubbo starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>Step 2: Implement the Dubbo service. Note that the @Service annotation comes from org.apache.dubbo.config.annotation.Service, not Spring.
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "hello " + name;
}
}Step 3: Add Dubbo configuration (application.yml or properties). Example:
spring.application.name=alibaba-dubbo-server
server.port=8001
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.scan.base-packages=com.didispace.alibaba.dubbo.server
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhostIf you use Spring Boot 2.1 or later, also set:
spring.main.allow-bean-definition-overriding=trueConsumer Setup
Step 1: Create another Spring Boot project with the same dependencies as the provider. (same <dependencies> block as above) Step 2: Configure the consumer (different application name and port).
spring.application.name=alibaba-dubbo-client
server.port=8002
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
dubbo.cloud.subscribed-services=alibaba-dubbo-serverStep 3: Create the main class and a REST controller that references the Dubbo service.
@EnableDiscoveryClient
@SpringBootApplication
public class DubboClientApplication {
public static void main(String[] args) {
SpringApplication.run(DubboClientApplication.class, args);
}
@RestController
static class TestController {
@Reference
HelloService helloService;
@GetMapping("/test")
public String test() {
return helloService.hello("didispace.com");
}
}
}Note that the @Reference annotation also comes from org.apache.dubbo.config.annotation.Reference.
Testing
Start Nacos, then the provider application, then the consumer application. Access http://localhost:8002/test – you should see the response: hello didispace.com. In the Nacos console you can see the two services registered:
Conclusion
With Spring Cloud Alibaba, you no longer need to manage separate registries like Eureka and ZooKeeper; Nacos alone handles service registration for both Spring Cloud and Dubbo. The configuration is straightforward, and the code remains almost identical to traditional Dubbo code. This integration lets Dubbo users enjoy RPC performance while benefiting from the rich Spring Cloud ecosystem, and gives Spring Cloud users an additional, high‑performance service‑governance option.
Reference: official documentation.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
