SpringBoot集成Dubbo啟用gRPC協(xié)議
前言
Dubbo 在 2.7.5 版本開始支持原生 gRPC 協(xié)議,對于計(jì)劃使用 HTTP/2 通信或者期望 gRPC 協(xié)議支持服務(wù)治理能力的,都可以考慮接入 Dubbo 體系啟用 gRPC 協(xié)議。
由于官網(wǎng)給的代碼示例是基于spring現(xiàn)在基本上都是基于SpringBoot開發(fā),所以本文提供一SpringBoot 的代碼示例。
此外還會簡單說明 Dubbo 支持的原生 gRPC 協(xié)議與原生 gRPC 協(xié)議在代碼開發(fā)時的區(qū)別。
如果對gRPC協(xié)議不了解的,后續(xù)文章會有更新,請持續(xù)關(guān)注。
項(xiàng)目結(jié)構(gòu)
根據(jù)現(xiàn)在微服務(wù)開發(fā)的常見方式,先搭建一個項(xiàng)目,結(jié)構(gòu)如下
這樣的項(xiàng)目結(jié)構(gòu)可以將服務(wù)的聲明和實(shí)現(xiàn)隔離開,如果有 client 調(diào)用,直接添加api module 的依賴即可。
代碼示例
項(xiàng)目結(jié)構(gòu)確定好后需要做三件事
- 在項(xiàng)目中需要用到 grpc 和 dubbo 相關(guān)依賴,所以在父工程中的 pom.xml 文件添加兩者的 BOM。
- gRPC 支持的序列化協(xié)議為 protobuf,我們在 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 模塊,對外提供的 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注冊中心說明 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 啟動類添加 @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)過以上的步驟,一個簡單的 SpringBoot 集成 Dubbo 啟用 gRPC 協(xié)議的示例就完成了。這個時候直接啟動項(xiàng)目是會報(bào)錯的,因?yàn)閜rotobuf相關(guān)的代碼還沒生成,我們需要對項(xiàng)目進(jìn)行 maven install 以及 maven reload 操作。
maven install 的目的是為了生成protobuf相關(guān)代碼,這個時候我們可以在 target 中看到
maven reload 的目的是為了更新加載 pom.xml 文件,從而將 api module 中生成的代碼加載到 service module。
操作后就可以成功啟動項(xiàng)目了。
區(qū)別
在項(xiàng)目啟動成功后可以回頭看下 Dubbo 支持的原生 gRPC 與原生 gRPC 在代碼編寫過程中的區(qū)別
- maven plugin 的區(qū)別,dubbo 在原先的基礎(chǔ)上添加了 dubbo-grpc 的 plugin,目的是生成擴(kuò)展的代碼做到對 grpc 的支持。
對應(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)格,基于接口的編程來定義和使用遠(yuǎn)程服務(wù)。
到此這篇關(guān)于SpringBoot集成Dubbo啟用gRPC協(xié)議的文章就介紹到這了,更多相關(guān)SpringBoot集成Dubbo內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java經(jīng)典算法匯總之選擇排序(SelectionSort)
選擇排序也是比較簡單的一種排序方法,原理也比較容易理解,選擇排序在每次遍歷過程中只記錄下來最小的一個元素的下標(biāo),待全部比較結(jié)束之后,將最小的元素與未排序的那部分序列的最前面一個元素交換,這樣就降低了交換的次數(shù),提高了排序效率。2016-04-04詳解hibernate雙向多對多關(guān)聯(lián)映射XML與注解版
本篇文章主要介紹了詳解hibernate雙向多對多關(guān)聯(lián)映射XML與注解版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Spring Security如何使用URL地址進(jìn)行權(quán)限控制
這篇文章主要介紹了Spring Security如何使用URL地址進(jìn)行權(quán)限控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java 為什么要避免使用finalizer和Cleaner
這篇文章主要介紹了Java 為什么要避免使用finalizer和Cleaner,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03JavaWeb實(shí)現(xiàn)文件上傳與下載的方法
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)文件上傳與下載的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01Elasticsearch查詢之Term?Query示例解析
這篇文章主要為大家介紹了Elasticsearch查詢之Term?Query示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04JPA之EntityManager踩坑及解決:更改PersistenceContext
這篇文章主要介紹了JPA之EntityManager踩坑及解決:更改PersistenceContext方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java實(shí)現(xiàn)可視化走迷宮小游戲的示例代碼
這篇文章主要介紹了Java如何實(shí)現(xiàn)可視化走迷宮小游戲。本程序適用于java程序員鞏固類與對象、文件讀取、事件響應(yīng)、awt包中各種工具的相關(guān)概念以及對邏輯能力的鍛煉,需要的可以參考一下2022-11-11