SpringBoot集成Dubbo啟用gRPC協(xié)議
前言
Dubbo 在 2.7.5 版本開(kāi)始支持原生 gRPC 協(xié)議,對(duì)于計(jì)劃使用 HTTP/2 通信或者期望 gRPC 協(xié)議支持服務(wù)治理能力的,都可以考慮接入 Dubbo 體系啟用 gRPC 協(xié)議。
由于官網(wǎng)給的代碼示例是基于spring現(xiàn)在基本上都是基于SpringBoot開(kāi)發(fā),所以本文提供一SpringBoot 的代碼示例。
此外還會(huì)簡(jiǎn)單說(shuō)明 Dubbo 支持的原生 gRPC 協(xié)議與原生 gRPC 協(xié)議在代碼開(kāi)發(fā)時(shí)的區(qū)別。
如果對(duì)gRPC協(xié)議不了解的,后續(xù)文章會(huì)有更新,請(qǐng)持續(xù)關(guān)注。
項(xiàng)目結(jié)構(gòu)
根據(jù)現(xiàn)在微服務(wù)開(kāi)發(fā)的常見(jiàn)方式,先搭建一個(gè)項(xiàng)目,結(jié)構(gòu)如下

這樣的項(xiàng)目結(jié)構(gòu)可以將服務(wù)的聲明和實(shí)現(xiàn)隔離開(kāi),如果有 client 調(diào)用,直接添加api module 的依賴即可。
代碼示例
項(xiàng)目結(jié)構(gòu)確定好后需要做三件事
- 在項(xiàng)目中需要用到 grpc 和 dubbo 相關(guān)依賴,所以在父工程中的 pom.xml 文件添加兩者的 BOM。
- gRPC 支持的序列化協(xié)議為 protobuf,我們?cè)?api module 下添加 gRPC 所需依賴、插件以及 proto IDL文件。
- 在 service module 添加相關(guān)配置并進(jìn)行 api service 的實(shí)現(xiàn)。
詳細(xì)代碼如下:
父工程
父工程中的 pom.xml 文件添加 grpc 和 dubbo 的 BOM。
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>
<name>nava</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.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>com.demo</groupId>
<artifactId>nava-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>nava-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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
在 api module 中的 pom.xml 文件添加 dubbo 、gRPC 所需依賴、插件。
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>
<name>nava-api</name>
<description>api 模塊,對(duì)外提供的 API</description>
<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>
在main文件夾下面創(chuàng)建proto文件夾,以及 DemoService.proto 文件。

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";
// The greeting service definition.
service DemoService {
// Sends a greeting
rpc service (RequestData) returns (ResponseData) {}
}
// The request message containing the user's name.
message RequestData {
string name = 1;
}
// The response message containing the greetings
message ResponseData {
string message = 1;
}
service module
在 service module 中的 pom.xml 文件添加 api module 的依賴以及 dubbo 其他依賴。
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-service</artifactId>
<name>nava-service</name>
<description>service 模塊,存放核心業(yè)務(wù)邏輯代碼</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>nava-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.11</version>
<configuration>
<mainClass>com.demo.nava.NavaApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在 application.properties 文件中添加 dubbo 相關(guān)配置
application.properties
# 設(shè)置dubbo傳輸協(xié)議
dubbo.protocol.name=grpc
dubbo.protocol.port=-1
# dubbo nacos注冊(cè)中心說(shuō)明 https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/registry/nacos/
dubbo.registry.address: nacos://nacos:nacos@${nacos.address:127.0.0.1}:8848
在 SpringBoot 啟動(dòng)類添加 @EnableDubbo 注解

添加 DemoServiceImpl 實(shí)現(xiàn)類進(jìn)行業(yè)務(wù)編碼。
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();
}
}
注意事項(xiàng)
經(jīng)過(guò)以上的步驟,一個(gè)簡(jiǎn)單的 SpringBoot 集成 Dubbo 啟用 gRPC 協(xié)議的示例就完成了。這個(gè)時(shí)候直接啟動(dòng)項(xiàng)目是會(huì)報(bào)錯(cuò)的,因?yàn)閜rotobuf相關(guān)的代碼還沒(méi)生成,我們需要對(duì)項(xiàng)目進(jìn)行 maven install 以及 maven reload 操作。
maven install 的目的是為了生成protobuf相關(guān)代碼,這個(gè)時(shí)候我們可以在 target 中看到

maven reload 的目的是為了更新加載 pom.xml 文件,從而將 api module 中生成的代碼加載到 service module。
操作后就可以成功啟動(dòng)項(xiàng)目了。
區(qū)別
在項(xiàng)目啟動(dòng)成功后可以回頭看下 Dubbo 支持的原生 gRPC 與原生 gRPC 在代碼編寫(xiě)過(guò)程中的區(qū)別
- maven plugin 的區(qū)別,dubbo 在原先的基礎(chǔ)上添加了 dubbo-grpc 的 plugin,目的是生成擴(kuò)展的代碼做到對(duì) grpc 的支持。

對(duì)應(yīng)生成的代碼如下

- service 實(shí)現(xiàn)區(qū)別,dubbo-grpc 的 plugin 生成了 dubbo 相關(guān)的 protobuf 的代碼,所以在實(shí)現(xiàn)上有所區(qū)別。

- RPC 調(diào)用區(qū)別,因?yàn)?grpc 接入了 dubbo 體系,所以使用 Dubbo 風(fēng)格,基于接口的編程來(lái)定義和使用遠(yuǎn)程服務(wù)。

到此這篇關(guān)于SpringBoot集成Dubbo啟用gRPC協(xié)議的文章就介紹到這了,更多相關(guān)SpringBoot集成Dubbo內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn)
本文主要介紹了java遞歸設(shè)置層級(jí)菜單的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
通過(guò)web控制當(dāng)前的SpringBoot程序重新啟動(dòng)
本文主要給大家介紹了如何通過(guò)web控制當(dāng)前的SpringBoot程序重新啟動(dòng),文章給出了詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11
springboot使用redisRepository和redistemplate操作redis的過(guò)程解析
本文給大家介紹springboot整合redis/分別用redisRepository和redistemplate操作redis,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-05-05
Java進(jìn)程CPU使用率過(guò)高排查步驟詳細(xì)講解
這篇文章主要介紹了Java進(jìn)程CPU使用率過(guò)高排查的相關(guān)資料,針對(duì)Java進(jìn)程CPU使用率高的問(wèn)題,我們可以遵循以下步驟進(jìn)行排查和優(yōu)化,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-06-06
java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)
在java項(xiàng)目的實(shí)際開(kāi)發(fā)和應(yīng)用中,常常需要用到將對(duì)象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下2021-08-08
Mybatis 插入一條或批量插入 返回帶有自增長(zhǎng)主鍵記錄的實(shí)例
下面小編就為大家分享一篇Mybatis 插入一條或批量插入 返回帶有自增長(zhǎng)主鍵記錄的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

