Integrating Dubbo with Spring Boot: Project Setup, Configuration, and Service Implementation
This tutorial walks through building a Spring Boot application that uses Alibaba's Dubbo framework, covering the RPC workflow, project structure, Maven dependencies, Spring Boot and XML configurations, service interface definition, provider and consumer implementations, and a RESTful endpoint for testing.
Overview
Dubbo is an Alibaba open‑source distributed service framework that adopts a layered architecture to achieve loose coupling between components. The typical RPC call flow involves four modules: Registry (usually Zookeeper), Provider, Consumer, and Monitor.
1. Project Construction
The project uses Spring Boot, JDK 8, Dubbo, and Zookeeper. A quick web project can be generated via http://start.spring.io/. The directory is split into two modules: spring-boot-dubbo (provider) and spring-boot-consumer (consumer).
2. Provider
2.1 pom.xml
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>2.2 Configuration (application.properties)
## Dubbo service provider configuration
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.jaycekon.dubbo.serviceAlternatively, the same settings can be expressed in XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- Application info -->
<dubbo:application name="provider"/>
<!-- Registry address -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1" check="false"/>
<!-- Dubbo protocol -->
<dubbo:protocol name="dubbo" port="-1" dispather="all" check="false"/>
<dubbo:provider timeout="10000" threads="10" threadpool="fixed" loadbalance="roundrobin"/>
<!-- Service interface to expose -->
<dubbo:service interface="com.jaycekon.dubbo.service" ref="userService"/>
</beans>2.3 Service Definition and Implementation
Interface:
public interface UserService {
User saveUser(User user);
}Implementation (using Dubbo @Service):
import com.alibaba.dubbo.config.annotation.Service;
import com.jaycekon.dubbo.domain.User;
import com.jaycekon.dubbo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Override
public User saveUser(User user) {
user.setId(1);
System.out.println(user.toString());
return user;
}
}3. Consumer
3.1 pom.xml
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>3.2 Configuration (application.properties)
# Avoid port conflict with provider
server.port=8081
# Dubbo consumer configuration
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.jaycekon.dubbo.serviceXML alternative is analogous to the provider configuration, with dubbo:application name="consumer" and a dubbo:reference for the service.
3.3 Consumer Service
import com.alibaba.dubbo.config.annotation.Reference;
import com.jaycekon.dubbo.domain.City;
import com.jaycekon.dubbo.domain.User;
import com.jaycekon.dubbo.service.CityDubboService;
import com.jaycekon.dubbo.service.UserService;
import org.springframework.stereotype.Component;
@Component
public class CityDubboConsumerService {
@Reference
private CityDubboService cityDubboService;
@Reference
private UserService userService;
public void printCity() {
String cityName = "广州";
City city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
}
public User saveUser() {
User user = new User();
user.setUsername("jaycekon").setPassword("jaycekong824");
return userService.saveUser(user);
}
}3.4 RESTful Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private CityDubboConsumerService service;
@RequestMapping("/save")
public Object saveUser() {
return service.saveUser();
}
}4. Summary
The article demonstrates how to integrate Dubbo into a Spring Boot environment, covering both provider and consumer sides, configuration options (properties and XML), Maven setup, and a simple REST endpoint for invoking Dubbo services. While Dubbo offers powerful RPC capabilities, the author notes that Spring Cloud provides a richer ecosystem and community support, which may be preferable for future projects.
GitHub repository: https://github.com/jaycekon/SpringBoot
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
