SpringBoot3結(jié)合gRpc實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟
一、gRPC概念介紹
gRPC(Google Remote Procedure Call,Google遠(yuǎn)程過(guò)程調(diào)用)是一個(gè)現(xiàn)代開(kāi)源高性能遠(yuǎn)程過(guò)程調(diào)用(RPC)框架,可以在任何環(huán)境中運(yùn)行。它由Google開(kāi)發(fā),旨在幫助開(kāi)發(fā)人員更輕松地構(gòu)建分布式應(yīng)用,特別是當(dāng)代碼可能在不同地方運(yùn)行的時(shí)候。
gRPC是一個(gè)高性能、開(kāi)源和通用的RPC框架,它基于HTTP/2設(shè)計(jì),并支持多種編程語(yǔ)言和平臺(tái)。
隨著其開(kāi)源和廣泛應(yīng)用,gRPC已成為云原生計(jì)算基金會(huì)(CNCF)的一個(gè)孵化項(xiàng)目,被大量組織和企業(yè)采用。
核心特點(diǎn)
- 高性能:gRPC使用HTTP/2作為傳輸協(xié)議,支持二進(jìn)制組幀、多路復(fù)用、雙向全雙工通信和流式處理等功能,從而顯著提高性能。與JSON相比,gRPC的消息序列化速度更快,消息體積更小。
- 跨平臺(tái)與跨語(yǔ)言:gRPC支持多種編程語(yǔ)言和平臺(tái),如C++、Java、Python、Go等,使得開(kāi)發(fā)人員可以在不同的環(huán)境中使用統(tǒng)一的RPC框架。
- 靈活性與可擴(kuò)展性:gRPC提供了豐富的功能,如負(fù)載平衡、跟蹤、健康檢查和身份驗(yàn)證等,這些功能都是可插拔的,可以根據(jù)需要進(jìn)行配置和擴(kuò)展。
- 安全性:gRPC支持TLS加密,確保數(shù)據(jù)在傳輸過(guò)程中的安全性。同時(shí),它還支持多種認(rèn)證機(jī)制,如JWT(JSON Web Tokens)等,以確保服務(wù)的訪問(wèn)安全。
工作原理
- 服務(wù)定義:在gRPC中,服務(wù)通過(guò).proto文件進(jìn)行定義。這些文件包含了服務(wù)的接口描述、消息類(lèi)型等信息。開(kāi)發(fā)人員可以使用Protocol Buffers(簡(jiǎn)稱(chēng)Protobuf)來(lái)定義這些結(jié)構(gòu)化的消息。
- 代碼生成:基于.proto文件,gRPC提供了protoc編譯器來(lái)生成支持多種編程語(yǔ)言的客戶(hù)端和服務(wù)端代碼。這使得開(kāi)發(fā)人員可以輕松地實(shí)現(xiàn)跨語(yǔ)言的RPC調(diào)用。
- 通信過(guò)程:在客戶(hù)端和服務(wù)端之間,gRPC通過(guò)HTTP/2協(xié)議進(jìn)行通信。客戶(hù)端發(fā)送請(qǐng)求到服務(wù)端,服務(wù)端處理請(qǐng)求并返回響應(yīng)。整個(gè)通信過(guò)程都是基于二進(jìn)制格式的,從而提高了性能和效率。
應(yīng)用場(chǎng)景
- 微服務(wù)架構(gòu):在微服務(wù)架構(gòu)中,gRPC可以有效地連接多語(yǔ)言服務(wù),實(shí)現(xiàn)服務(wù)間的快速通信。
- 分布式計(jì)算:gRPC適用于分布式計(jì)算的最后一英里,將設(shè)備、移動(dòng)應(yīng)用程序和瀏覽器連接到后端服務(wù)。
- API設(shè)計(jì):與REST API相比,gRPC提供了一種更加高效和靈活的API設(shè)計(jì)風(fēng)格,適用于需要高性能和低延遲的應(yīng)用場(chǎng)景。
二、簡(jiǎn)單使用步驟
首先,你需要定義gRPC服務(wù)。這里我們使用一個(gè)簡(jiǎn)單的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插件的實(shí)際路徑。
3. 服務(wù)端實(shí)現(xiàn)
在Spring Boot應(yīng)用中,你可以創(chuàng)建一個(gè)組件來(lái)實(shí)現(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. 客戶(hù)端調(diào)用
在Spring Boot應(yīng)用中,你可以創(chuàng)建一個(gè)服務(wù)來(lái)調(diào)用gRPC服務(wù)。這里使用JUnit單元測(cè)試
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依賴(lài)。
然后執(zhí)行maven compile 指令生成java代碼
mvn compile
啟動(dòng)SpringBoot服務(wù)端,然后運(yùn)行測(cè)試用例,得到如下結(jié)果:
response = message: "Hello, Lisa!"
三、添加gRPC相關(guān)依賴(lài)包
參考 grpc-spring
在Spring Boot項(xiàng)目中使用gRPC,你需要在項(xiàng)目的pom.xml 中添加相關(guān)的gRPC依賴(lài)。
以下是在Maven項(xiàng)目中添加gRPC依賴(lài)的示例。
<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>
請(qǐng)注意,你需要將${grpc-java.version}、${grpc-spring-boot-starter.version}替換為你想要使用的具體版本號(hào)。
這些依賴(lài)項(xiàng)包括了gRPC的基礎(chǔ)庫(kù)、與Protobuf的集成、以及Spring Boot對(duì)gRPC的支持。確保你使用的版本是兼容的,并根據(jù)你的項(xiàng)目需求進(jìn)行調(diào)整。如果你還需要使用到其他的gRPC功能(如安全認(rèn)證、健康檢查等),你可能需要添加更多的依賴(lài)項(xiàng)。
四、使用Maven插件自動(dòng)生成proto對(duì)應(yīng)代碼
參考 os-maven-plugin 和 protobuf-maven-plugin
如果你希望通過(guò)maven編譯時(shí)自動(dòng)生成proto對(duì)應(yīng)的java代碼,則需要添加Maven插件和相關(guān)依賴(lài)
<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>
通過(guò)上面配置后,你就可以在項(xiàng)目中通過(guò)指令mvn compile生成proto對(duì)應(yīng)的java代碼,不需要通過(guò)外包指令了。
到此這篇關(guān)于SpringBoot3結(jié)合gRpc實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟的文章就介紹到這了,更多相關(guān)SpringBoot3 gRpc遠(yuǎn)程服務(wù)調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合SpringDataRedis的示例代碼
這篇文章主要介紹了SpringBoot整合SpringDataRedis的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Java并發(fā)系列之CyclicBarrier源碼分析
這篇文章主要為大家詳細(xì)分析了Java并發(fā)系列之CyclicBarrier源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Tomcat?8.5?+mysql?5.7+jdk1.8開(kāi)發(fā)JavaSE的金牌榜小項(xiàng)目
這篇文章主要介紹了Tomcat?8.5?+mysql?5.7+jdk1.8開(kāi)發(fā)JavaSE的金牌榜小項(xiàng)目,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
IDEA啟動(dòng)Tomcat時(shí)控制臺(tái)出現(xiàn)亂碼問(wèn)題及解決
這篇文章主要介紹了IDEA啟動(dòng)Tomcat時(shí)控制臺(tái)出現(xiàn)亂碼問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
DUCC配置平臺(tái)實(shí)現(xiàn)一個(gè)動(dòng)態(tài)化線程池示例代碼
這篇文章主要為大家介紹了DUCC配置平臺(tái)實(shí)現(xiàn)一個(gè)動(dòng)態(tài)化線程池示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
springboot啟動(dòng)時(shí)如何指定spring.profiles.active
這篇文章主要介紹了springboot啟動(dòng)時(shí)如何指定spring.profiles.active問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
springboot項(xiàng)目中使用docker進(jìn)行遠(yuǎn)程部署的實(shí)現(xiàn)
本文主要介紹了在Spring Boot項(xiàng)目中使用Docker進(jìn)行遠(yuǎn)程部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
淺談Spring中@Transactional事務(wù)回滾及示例(附源碼)
本篇文章主要介紹了淺談Spring中@Transactional事務(wù)回滾及示例(附源碼),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

