Integrating Dubbo with SpringBoot Using gRPC: Project Structure, Maven Configuration, and Code Samples
This article provides a step‑by‑step guide on how to integrate Dubbo with SpringBoot to enable the gRPC protocol, covering project structure, Maven BOM dependencies, protobuf generation, service implementation, configuration details, and the differences between Dubbo‑enabled gRPC and native gRPC.
Preface – Dubbo 2.7.5+ supports native gRPC, making it suitable for HTTP/2 communication and service governance; this guide shows how to set up a SpringBoot project with Dubbo‑gRPC.
Project Structure
The multi‑module Maven project consists of a parent pom, an api module for protobuf definitions, and a service module for business logic.
Code Example
Three main steps are required:
Add gRPC and Dubbo BOMs to the parent pom.xml.
In the api module, add gRPC dependencies, the protobuf plugin, and the .proto file.
In the service module, add Dubbo and SpringBoot dependencies and implement the service.
Parent pom.xml
<?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>
<groupId>com.demo</groupId>
<artifactId>nava</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<dubbo.version>3.1.7</dubbo.version>
<grpc.version>1.44.1</grpc.version>
<spring-boot.version>2.6.11</spring-boot.version>
</properties>
<modules>
<module>nava-api</module>
<module>nava-service</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-bom</artifactId>
<version>${grpc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>API module pom.xml
<?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>
<groupId>com.demo</groupId>
<artifactId>nava</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>nava-api</artifactId>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http2</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-handler-proxy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<id>os-maven-plugin</id>
<phase>initialize</phase>
<goals><goal>detect</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>dubbo-grpc</id>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-compiler</artifactId>
<version>0.0.1</version>
<mainClass>org.apache.dubbo.gen.grpc.DubboGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<executions>
<execution>
<id>protobuf-maven-plugin</id>
<goals><goal>compile</goal><goal>compile-custom</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>Proto Definition (DemoService.proto)
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.demo.nava";
option java_outer_classname = "DemoServiceProto";
option objc_class_prefix = "DSP";
service DemoService {
rpc service (RequestData) returns (ResponseData) {}
}
message RequestData {
string name = 1;
}
message ResponseData {
string message = 1;
}Service Implementation (DemoServiceImpl.java)
package com.demo.nava.service;
import com.demo.nava.DubboDemoServiceGrpc;
import com.demo.nava.RequestData;
import com.demo.nava.ResponseData;
import io.grpc.stub.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoServiceImpl extends DubboDemoServiceGrpc.DemoServiceImplBase implements DubboDemoServiceGrpc.IDemoService {
@Override
public void service(RequestData request, StreamObserver<ResponseData> responseObserver) {
ResponseData reply = ResponseData.newBuilder()
.setMessage("Hello " + request.getName())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}Configuration
Add the following to application.properties to enable Dubbo gRPC transport and Nacos registry:
# Set Dubbo transport protocol
dubbo.protocol.name=grpc
dubbo.protocol.port=-1
# Dubbo Nacos registry (see Dubbo docs for details)
dubbo.registry.address=nacos://nacos:nacos@${nacos.address:127.0.0.1}:8848Annotate the SpringBoot main class with @EnableDubbo to activate Dubbo.
Notes
After configuring the project, run maven install to generate protobuf classes, then maven reload (or re‑import the modules) so that the generated API code becomes visible to the service module. Finally, start the application.
Differences Between Dubbo‑Enabled gRPC and Native gRPC
The Maven plugin adds a Dubbo‑specific plugin that generates extra code for Dubbo integration.
Service implementation extends Dubbo‑generated base classes, differing from pure gRPC stubs.
RPC calls follow Dubbo’s interface‑based style while still using gRPC under the hood.
Following these steps yields a functional SpringBoot application that uses Dubbo to provide gRPC services with full service‑governance capabilities.
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.
