maven?grpc整合springboot?demo
1. 說明
GRPC基于protobuf來定義接口。分為server端和client端。其中server端提供接口實現(xiàn),client通過調(diào)用server端接口從而獲取期望數(shù)據(jù)。
2. 公共部分
2.1 添加依賴
<dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>2.12.0.RELEASE</version> </dependency> <dependency> <!-- Java 9+ compatibility --> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> </dependency>
添加插件(注意:如果wagon-provider-api無法自動引入,可以現(xiàn)在依賴中引入,以便于依賴的下載,然后在刪除依賴坐標即可)
<plugin> <!-- protobuf生成插件--> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.17.3:exe:${os.detected.classifier} </protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.39.0:exe:${os.detected.classifier} </pluginArtifact> <!--默認值--> <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin>
2.2 添加proto依賴文件
添加目錄src/main/proto
,并將目錄設(shè)置為Source Root
,然后在目錄src/main/proto
下添加文件hello.proto
,內(nèi)容如下
syntax = "proto3"; //指定proto版本 package com.server; // 生成的Java代碼的包名 option java_package = "com.grpc.server"; // 請求參數(shù) message HelloReq{ string name = 1; } // 返回參數(shù) message HelloResp{ string ret = 1; } // rpc service service HelloService{ // service中需要進行調(diào)用的具體方法 rpc hello(HelloReq) returns (HelloResp){} }
2.3 通過protobuf生成Java代碼
插件導入成功后,點擊下圖選中的protobuf:compile
和protbuf:compile-custom
依次生成對應的Java代碼(也就是接口依賴代碼)
3. server端接口具體實現(xiàn)
service代碼如下
import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; @GrpcService public class HelloService extends HelloServiceGrpc.HelloServiceImplBase { @Override public void hello(Hello.HelloReq request, StreamObserver<Hello.HelloResp> responseObserver) { Hello.HelloResp resp = Hello.HelloResp.newBuilder().setRet("你好-->"+request.getName()).build(); responseObserver.onNext(resp); responseObserver.onCompleted(); } }
4 client端接口具體實現(xiàn)
client端測試調(diào)用代碼如下
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class GrpcTest { @Autowired private HelloSerivce helloSerivce; @Test public void test1() throws Exception{ helloSerivce.haha("牛哈哈"); } }
以上就是maven grpc整合springboot demo的詳細內(nèi)容,更多關(guān)于maven grpc整合springboot 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis批量查詢、插入、更新、刪除的實現(xiàn)示例
由于需要處理短時間內(nèi)大量數(shù)據(jù)入庫的問題,想到了Mybatis的批量操作,本文主要介紹了MyBatis批量查詢、插入、更新、刪除的實現(xiàn)示例,感興趣的可以了解一下2023-05-05解決@Cacheable在同一個類中方法調(diào)用不起作用的問題
這篇文章主要介紹了解決@Cacheable在同一個類中方法調(diào)用不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Spring?@Conditional通過條件控制bean注冊過程
這篇文章主要為大家介紹了Spring?@Conditional通過條件控制bean注冊過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Java Socket編程簡介_動力節(jié)點Java學院整理
這篇文章主要介紹了Java Socket編程簡介的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05servlet監(jiān)聽實現(xiàn)統(tǒng)計在線人數(shù)功能 附源碼下載
這篇文章主要為大家詳細介紹了servlet監(jiān)聽統(tǒng)計在線人數(shù)的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04SpringCloud Alibaba 基本開發(fā)框架搭建過程
這篇文章主要介紹了SpringCloud Alibaba 基本開發(fā)框架搭建過程,開發(fā)工具選用的idea,本文通過圖文實例相結(jié)合給大家分享搭建全過程,需要的朋友可以參考下2021-06-06