Dubbo + Nacos Integration with Spring Boot: Step‑by‑Step Example

This tutorial provides a complete, code‑rich example of integrating Dubbo with Nacos using Spring Boot, covering environment setup, Maven project structure, module creation, configuration files, Java source code, and testing the RPC service call.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Dubbo + Nacos Integration with Spring Boot: Step‑by‑Step Example

In this tutorial we demonstrate a complete example of integrating Dubbo with Nacos using Spring Boot, covering environment setup, Dubbo basics, Nacos installation, project structure, Maven parent pom configuration, creation of a public API module, Dubbo provider and consumer modules, their respective configuration files, Java source code, and how to test the service call.

Environment Preparation

Dubbo Introduction

Illustrates the core node call relationships in Dubbo.

Node description

Call relationship description

Service container starts, loads, and runs service providers.

Providers register services to the registry on startup.

Consumers subscribe to needed services at startup.

Registry returns provider address list to consumers and pushes updates.

Consumers select a provider based on load‑balancing algorithm.

Consumers and providers report call statistics to monitoring center.

Nacos Environment Preparation

Start Nacos, version 1.4.3 used.
Open Nacos control panel.

Nacos and Dubbo Integration

Provides demo code for producer and consumer and the registry used.

Project Management Guidelines

Specifies version alignment between Spring Boot, Netflix, and Spring Cloud Alibaba, showing selected versions.

Shows version mapping between Dubbo and Spring Cloud Alibaba.

To simplify development with SpringCloud Alibaba, create a Maven parent project named spring-cloud-alibaba-example and modify its pom.

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    ...
</project>

All sub‑projects inherit the groupId and artifactId from this parent.

Dubbo‑Nacos Example

Creates the project directory structure for the example.

Module description
public-api

– shared interface module. dubbo-provider – service provider module, depends on public-api. dubbo-consumer – service consumer module, depends on public-api.

Consumer and provider communicate via the shared interface.

Parent project pom file
<?xml version="1.0" encoding="UTF-8"?>
<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>ah.wideth</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modules>
        <module>public-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>
    <artifactId>dubbo-nacos-example</artifactId>
    <name>dubbo-nacos-example</name>
    <description>Parent project for Dubbo and Nacos integration</description>
    <packaging>pom</packaging>
    ...
</project>

Create Public API Module

Pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <parent>
        <artifactId>dubbo-nacos-example</artifactId>
        <groupId>ah.wideth</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>public-api</artifactId>
    <name>public-api</name>
    <description>API public interface</description>
    <packaging>jar</packaging>
    ...
</project>

Java interface:

package ah.wideth.api;

public interface InfoService {
    String getInfo();
}

Create Service Provider Module

Pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <parent>
        <artifactId>dubbo-nacos-example</artifactId>
        <groupId>ah.wideth</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-provider</artifactId>
    <name>dubbo-provider</name>
    <description>Dubbo service provider module</description>
    ...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- reference public-api module -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>public-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

application.yml configuration:

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: 30000

Implementation class:

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, this is dubbo-provider module!";
    }
}

Provider application entry:

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 provider started on 8180");
    }
}

Create Service Consumer Module

Pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <parent>
        <artifactId>dubbo-nacos-example</artifactId>
        <groupId>ah.wideth</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-consumer</artifactId>
    <name>dubbo-consumer</name>
    <description>Dubbo service consumer module</description>
    ...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- reference public-api module -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>public-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

application.yml configuration:

server:
  port: 8181
spring:
  application:
    name: dubbo-consumer
dubbo:
  registry:
    address: nacos://127.0.0.1:8848
  application:
    name: dubbo-consumer
  consumer:
    timeout: 30000

Controller that calls the shared interface:

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:

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 consumer started on 8181");
    }
}

Service Call Test

Open the Nacos dashboard to view registered services, then start Nacos, the provider, and the consumer; invoking /getInfo on the consumer returns the response from the provider.

Conclusion

This article presented a practical example of integrating Dubbo with Nacos for RPC service calls, highlighting the need to manage jar conflicts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Microservicesservice discoveryDubbomavenNacosSpring Boot
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.