Integrating Dubbo with Nacos in Spring Boot: A Step‑by‑Step Example
This article provides a practical step‑by‑step guide to integrating Dubbo with Nacos in a Spring Boot project, covering environment setup, Maven configuration, module creation for public API, provider and consumer services, and testing the RPC call, while highlighting version compatibility and common pitfalls.
In this tutorial, we walk through a complete example of integrating dubbo with nacos using Spring Boot, focusing on practical implementation rather than theory.
Environment Preparation
Dubbo Overview
Dubbo’s core nodes communicate as follows: the service container starts and loads providers, providers register with the registry, consumers subscribe to services, the registry returns provider addresses, and consumers invoke services using load‑balancing and report metrics.
Nacos Setup
Start Nacos (version 1.4.3) and open its control panel.
Project Management Guidelines
Define version compatibility between Spring Boot, Spring Cloud Alibaba, Dubbo, and Nacos. Use a parent Maven project to manage dependencies.
Parent POM (dubbo‑nacos‑example)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-cloud-alibaba-example</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.12.RELEASE</version>
</parent>
<groupId>ah.wideth</groupId>
<artifactId>dubbo-nacos-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.13</dubbo.version>
<nacos.version>1.4.1</nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>Module: public‑api
This module defines a simple interface that both provider and consumer will use.
package ah.wideth.api;
/**
* Allows provider and consumer to share this contract.
*/
public interface InfoService {
String getInfo();
}Module: dubbo‑provider
POM adds Spring Boot web starter and depends on the public‑api module.
<project ...>
<parent>...</parent>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>public-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Configuration application.yml registers the service with Nacos and sets Dubbo protocol.
server:
port: 8180
spring:
application:
name: dubbo-provider
dubbo:
registry:
address: nacos://127.0.0.1:8848
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
scan:
base-packages: ah.wideth.impl
provider:
timeout: 30000Implementation of the service:
package ah.wideth.impl;
import ah.wideth.api.InfoService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@Component
@DubboService
public class InfoServiceImpl implements InfoService {
@Override
public String getInfo() {
return "hello,这里是dubbo-provider模块!";
}
}Provider application entry point:
package ah.wideth;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
System.out.println("dubbo服务提供者8180启动了");
}
}Module: dubbo‑consumer
POM mirrors the provider and also depends on public-api.
<project ...>
<parent>...</parent>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>public-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Consumer application.yml points to the same Nacos registry.
server:
port: 8181
spring:
application:
name: dubbo-consumer
dubbo:
registry:
address: nacos://127.0.0.1:8848
application:
name: dubbo-consumer
consumer:
timeout: 30000Controller that references the remote service:
package ah.wideth.controller;
import ah.wideth.api.InfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InfoController {
@DubboReference(check = false)
private InfoService infoService;
@GetMapping("/getInfo")
public String getInfo() {
return infoService.getInfo();
}
}Consumer application entry point:
package ah.wideth;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
System.out.println("dubbo服务消费者8181启动了");
}
}Testing the Integration
Start Nacos, then run the provider and consumer applications. Access http://localhost:8181/getInfo – the consumer calls the remote Dubbo service and receives the string returned by the provider. Verify the service appears in the Nacos console.
Conclusion
The example demonstrates a minimal yet complete Dubbo‑Nacos integration with Spring Boot, emphasizing proper Maven module structure, version alignment, and configuration to avoid common jar‑conflict issues.
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.
