SpringBoot3結(jié)合gRpc實現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟
一、gRPC概念介紹
gRPC(Google Remote Procedure Call,Google遠(yuǎn)程過程調(diào)用)是一個現(xiàn)代開源高性能遠(yuǎn)程過程調(diào)用(RPC)框架,可以在任何環(huán)境中運(yùn)行。它由Google開發(fā),旨在幫助開發(fā)人員更輕松地構(gòu)建分布式應(yīng)用,特別是當(dāng)代碼可能在不同地方運(yùn)行的時候。
gRPC是一個高性能、開源和通用的RPC框架,它基于HTTP/2設(shè)計,并支持多種編程語言和平臺。
隨著其開源和廣泛應(yīng)用,gRPC已成為云原生計算基金會(CNCF)的一個孵化項目,被大量組織和企業(yè)采用。
核心特點
- 高性能:gRPC使用HTTP/2作為傳輸協(xié)議,支持二進(jìn)制組幀、多路復(fù)用、雙向全雙工通信和流式處理等功能,從而顯著提高性能。與JSON相比,gRPC的消息序列化速度更快,消息體積更小。
- 跨平臺與跨語言:gRPC支持多種編程語言和平臺,如C++、Java、Python、Go等,使得開發(fā)人員可以在不同的環(huán)境中使用統(tǒng)一的RPC框架。
- 靈活性與可擴(kuò)展性:gRPC提供了豐富的功能,如負(fù)載平衡、跟蹤、健康檢查和身份驗證等,這些功能都是可插拔的,可以根據(jù)需要進(jìn)行配置和擴(kuò)展。
- 安全性:gRPC支持TLS加密,確保數(shù)據(jù)在傳輸過程中的安全性。同時,它還支持多種認(rèn)證機(jī)制,如JWT(JSON Web Tokens)等,以確保服務(wù)的訪問安全。
工作原理
- 服務(wù)定義:在gRPC中,服務(wù)通過.proto文件進(jìn)行定義。這些文件包含了服務(wù)的接口描述、消息類型等信息。開發(fā)人員可以使用Protocol Buffers(簡稱Protobuf)來定義這些結(jié)構(gòu)化的消息。
- 代碼生成:基于.proto文件,gRPC提供了protoc編譯器來生成支持多種編程語言的客戶端和服務(wù)端代碼。這使得開發(fā)人員可以輕松地實現(xiàn)跨語言的RPC調(diào)用。
- 通信過程:在客戶端和服務(wù)端之間,gRPC通過HTTP/2協(xié)議進(jìn)行通信??蛻舳税l(fā)送請求到服務(wù)端,服務(wù)端處理請求并返回響應(yīng)。整個通信過程都是基于二進(jìn)制格式的,從而提高了性能和效率。
應(yīng)用場景
- 微服務(wù)架構(gòu):在微服務(wù)架構(gòu)中,gRPC可以有效地連接多語言服務(wù),實現(xiàn)服務(wù)間的快速通信。
- 分布式計算:gRPC適用于分布式計算的最后一英里,將設(shè)備、移動應(yīng)用程序和瀏覽器連接到后端服務(wù)。
- API設(shè)計:與REST API相比,gRPC提供了一種更加高效和靈活的API設(shè)計風(fēng)格,適用于需要高性能和低延遲的應(yīng)用場景。
二、簡單使用步驟
首先,你需要定義gRPC服務(wù)。這里我們使用一個簡單的helloworld.proto文件。
1. helloworld.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "HelloWorldProto";
package helloworld;
service HelloWorldService {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
2. 生成Java代碼
使用protoc編譯器和gRPC插件生成Java代碼。
protoc --java_out=./src/main/java --grpc-java_out=./src/main/java --plugin=protoc-gen-grpc-java=/path/to/protoc-gen-grpc-java-1.x.x-linux-x86_64.exe helloworld.proto
確保將/path/to/protoc-gen-grpc-java-1.x.x-linux-x86_64.exe替換為你的protoc-gen-grpc-java插件的實際路徑。
3. 服務(wù)端實現(xiàn)
在Spring Boot應(yīng)用中,你可以創(chuàng)建一個組件來實現(xiàn)gRPC服務(wù)。
package com.example.grpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
public class HelloWorldServiceImpl extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
String message = "Hello, " + req.getName() + "!";
HelloReply reply = HelloReply.newBuilder().setMessage(message).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
4. 客戶端調(diào)用
在Spring Boot應(yīng)用中,你可以創(chuàng)建一個服務(wù)來調(diào)用gRPC服務(wù)。這里使用JUnit單元測試
import com.example.grpc.HelloReply;
import com.example.grpc.HelloRequest;
import com.example.grpc.HelloWorldServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.junit.jupiter.api.Test;
public class HelloWorldClientServiceTest {
@Test
public void sayHello() {
ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9091").usePlaintext().build();
HelloWorldServiceGrpc.HelloWorldServiceBlockingStub stub = HelloWorldServiceGrpc.newBlockingStub(channel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName("Lisa").build());
System.out.println("response = " + response);
channel.shutdown();
}
}
5. 構(gòu)建和運(yùn)行
確保你的pom.xml中包含了必要的gRPC和Spring Boot依賴。
然后執(zhí)行maven compile 指令生成java代碼
mvn compile
啟動SpringBoot服務(wù)端,然后運(yùn)行測試用例,得到如下結(jié)果:
response = message: "Hello, Lisa!"
三、添加gRPC相關(guān)依賴包
參考 grpc-spring
在Spring Boot項目中使用gRPC,你需要在項目的pom.xml 中添加相關(guān)的gRPC依賴。
以下是在Maven項目中添加gRPC依賴的示例。
<dependencies>
<dependency>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>jprotoc</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>${grpc-spring-boot-starter.version}</version>
</dependency>
</dependencies>
<properties>
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
<protoc.version>3.25.1</protoc.version>
<grpc-java.version>1.64.0</grpc-java.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
</properties>
請注意,你需要將${grpc-java.version}、${grpc-spring-boot-starter.version}替換為你想要使用的具體版本號。
這些依賴項包括了gRPC的基礎(chǔ)庫、與Protobuf的集成、以及Spring Boot對gRPC的支持。確保你使用的版本是兼容的,并根據(jù)你的項目需求進(jìn)行調(diào)整。如果你還需要使用到其他的gRPC功能(如安全認(rèn)證、健康檢查等),你可能需要添加更多的依賴項。
四、使用Maven插件自動生成proto對應(yīng)代碼
參考 os-maven-plugin 和 protobuf-maven-plugin
如果你希望通過maven編譯時自動生成proto對應(yīng)的java代碼,則需要添加Maven插件和相關(guān)依賴
<properties>
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
<protoc.version>3.25.1</protoc.version>
<grpc-java.version>1.64.0</grpc-java.version>
<os-maven-plugin.version>1.7.1</os-maven-plugin.version>
<grpc-spring-boot-starter.version>3.1.0.RELEASE</grpc-spring-boot-starter.version>
</properties>
//...//
<dependencies>
<dependency>
<groupId>com.salesforce.servicelibs</groupId>
<artifactId>jprotoc</artifactId>
<version>1.2.2</version>
</dependency>
<!-- gRPC Server + Client -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>${grpc-spring-boot-starter.version}</version>
</dependency>
</dependencies>
// ... //
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<!-- protobuf-maven-plugin -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>protoc-compile</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
<execution>
<id>protoc-test-compile</id>
<phase>generate-test-sources</phase>
<goals>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
</protocArtifact>
<attachProtoSources>true</attachProtoSources>
<useArgumentFile>true</useArgumentFile>
<writeDescriptorSet>false</writeDescriptorSet>
<attachDescriptorSet>false</attachDescriptorSet>
<includeDependenciesInDescriptorSet>false</includeDependenciesInDescriptorSet>
<checkStaleness>true</checkStaleness>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
<protocPlugins>
</protocPlugins>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
通過上面配置后,你就可以在項目中通過指令mvn compile生成proto對應(yīng)的java代碼,不需要通過外包指令了。
到此這篇關(guān)于SpringBoot3結(jié)合gRpc實現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟的文章就介紹到這了,更多相關(guān)SpringBoot3 gRpc遠(yuǎn)程服務(wù)調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
util.Date與sql.Date的相互轉(zhuǎn)換過程
本文介紹了Java中兩個常用日期類的區(qū)別和相互轉(zhuǎn)換方法,包括java.util.Date和java.sql.Date,并展示了如何使用SimpleDateFormat進(jìn)行日期格式化2026-01-01
Java中向文件寫入數(shù)據(jù)的幾種常見方式分享
在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡單羅列一下平時比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-10-10
springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn)
本文主要介紹了springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

