maven?grpc整合springboot?demo
1. 說明
GRPC基于protobuf來定義接口。分為server端和client端。其中server端提供接口實(shí)現(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)在依賴中引入,以便于依賴的下載,然后在刪除依賴坐標(biāo)即可)
<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>
<!--默認(rèn)值-->
<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中需要進(jìn)行調(diào)用的具體方法
rpc hello(HelloReq) returns (HelloResp){}
}
2.3 通過protobuf生成Java代碼
插件導(dǎo)入成功后,點(diǎn)擊下圖選中的protobuf:compile和protbuf:compile-custom 依次生成對應(yīng)的Java代碼(也就是接口依賴代碼)

3. server端接口具體實(shí)現(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端接口具體實(shí)現(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的詳細(xì)內(nèi)容,更多關(guān)于maven grpc整合springboot 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Boot整合郵箱發(fā)送郵件實(shí)例
大家好,本篇文章主要講的是Spring?Boot整合郵箱發(fā)送郵件實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
Mybatis對MySQL if 函數(shù)的不支持問題解讀
接手項(xiàng)目后,為了實(shí)現(xiàn)多租戶功能,引入了Mybatis-plus,發(fā)現(xiàn)之前運(yùn)行正常的SQL語句報(bào)錯(cuò),原因是Mybatis不支持MySQL的if函數(shù),通過查詢資料,將SQL中的if函數(shù)替換為case語句,解決了問題2025-12-12
Java?IO?API實(shí)現(xiàn)獲取路徑信息并去除冗余信息
這篇文章主要為大家詳細(xì)介紹了Java?IO?API實(shí)現(xiàn)獲取路徑信息并去除冗余信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-03-03
Springcloud?feign傳日期類型參數(shù)報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Springcloud?feign傳日期類型參數(shù)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot集成Redis向量數(shù)據(jù)庫實(shí)現(xiàn)相似性搜索功能
Redis?是一個(gè)開源(BSD?許可)的內(nèi)存數(shù)據(jù)結(jié)構(gòu)存儲,用作數(shù)據(jù)庫、緩存、消息代理和流式處理引擎,向量檢索的核心原理是通過將文本或數(shù)據(jù)表示為高維向量,并在查詢時(shí)根據(jù)向量的相似度進(jìn)行搜索,本文給大家介紹了SpringBoot集成Redis向量數(shù)據(jù)庫實(shí)現(xiàn)相似性搜索功能2024-09-09

