相信大家對(duì)RPC協(xié)議都有一定的了解,并且或多或少都會(huì)在項(xiàng)目中涉及,但可能都和小編類(lèi)似,都是直接使用平臺(tái)封裝的插件,對(duì)于其中的原理不是很了解,今天借此機(jī)會(huì)和大家分享下最近接觸的RPC框架-grpc,一同聊聊那些知其然卻不知其所以然的內(nèi)容。
?大家好,我是指北君。又是一周工作日的最后一天,但也不要忘記努力。
吃得苦中苦,你長(zhǎng)大開(kāi)路虎,少壯不努力,你長(zhǎng)大開(kāi)夏利。
接下來(lái)開(kāi)始進(jìn)入正題~
?前言
相信大家對(duì)RPC協(xié)議都有一定的了解,并且或多或少都會(huì)在項(xiàng)目中涉及,但可能都和小編類(lèi)似,都是直接使用平臺(tái)封裝的插件,對(duì)于其中的原理不是很了解,今天借此機(jī)會(huì)和大家分享下最近接觸的RPC框架-grpc,一同聊聊那些知其然卻不知其所以然的內(nèi)容。
?概述
RPC(Remote Procedure Call)遠(yuǎn)程過(guò)程調(diào)用協(xié)議,是一種本地可以通過(guò)網(wǎng)絡(luò)請(qǐng)求遠(yuǎn)程計(jì)算機(jī),完成計(jì)算機(jī)間的數(shù)據(jù)內(nèi)容的交互的協(xié)議,不需要了解網(wǎng)絡(luò)底層技術(shù)就可以快速上手,使得開(kāi)發(fā)更加容易,同時(shí)提升了交互體驗(yàn)效率。
為了方便開(kāi)發(fā),有很多基于RPC協(xié)議實(shí)現(xiàn)的RPC框架,比如Thrift、Dubbo,和本文即將要介紹的gRPC。
?什么是gRPC
- gRPC是由google開(kāi)發(fā)的一種支跨平臺(tái)(語(yǔ)言)、高性能、開(kāi)源通用的RPC框架。
- 它是基于HTTP2.0協(xié)議的,可以保持客戶(hù)端與服務(wù)端長(zhǎng)連接,基于二進(jìn)制流(字節(jié)流)傳輸數(shù)據(jù)。
- 客戶(hù)端與服務(wù)端交互過(guò)程
客戶(hù)端(gRPC Sub)調(diào)用A方法,發(fā)起RPC請(qǐng)求
請(qǐng)求內(nèi)容使用Protobf進(jìn)行對(duì)象序列化壓縮
服務(wù)端(gRPC Server)接收請(qǐng)求,解析請(qǐng)求內(nèi)容,業(yè)務(wù)處理后返回
響應(yīng)結(jié)果通過(guò)Protobuf進(jìn)行對(duì)象序列化壓縮
客戶(hù)端接收響應(yīng),解析響應(yīng)內(nèi)容,最終完成交互
?實(shí)踐案例
小編以java版進(jìn)行案例展示,其它語(yǔ)言類(lèi)似,可自行測(cè)試
POM依賴(lài)
- gRPC官方提供完成的依賴(lài)配置,按照說(shuō)明直接引用即可(依賴(lài)包含插件),版本僅供參考,也可選擇其它版本。
<!-- gRPC配置 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.29.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.29.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
<version>1.29.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.29.0</version>
</dependency>
<!-- proto插件 -->
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.11.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.29.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
編寫(xiě)protobuf文件
- 小編使用的是proto3版本,需要注意固定的目錄結(jié)構(gòu)(src/proto/*.proto),否則會(huì)編譯失敗。
- proto文件有固定的編寫(xiě)格式,可以自行上網(wǎng)搜索即可?
syntax = "proto3";
//包所在路徑
option java_package = "com.greatom.dockerdemo.rule";
option java_multiple_files = true;
package rule;
//聲明服務(wù)和方法
service RuleService {
//查詢(xún)并更新規(guī)則
rpc getArchivesDic (RuleRequest) returns (RuleResponse);
//獲取當(dāng)前規(guī)則字典
rpc getRule (Request) returns (Response);
}
//定義請(qǐng)求對(duì)象
message RuleRequest {
// message RuleRPCDTO {
// int32 ruleCode = 1;
// string administrativeCost = 2;
// }
Response ruleRPCDTO = 1;
int32 basicId = 2;
}
//定義響應(yīng)對(duì)象
message RuleResponse {
int32 id = 1;
}
message Request {
}
//定義響應(yīng)消息
message Response {
int32 ruleCode = 1;
string administrativeCost = 2;
}
- 使用maven插件編譯,雙擊執(zhí)行(生成Bean,maven->Plugins->protobuf->protobuf:compile;生成具體接口,maven->Plugins->protobuf->protobuf:compile-custom)。
- 小編只執(zhí)行protobuf:compile命令,然后在target目錄(\target\generated-sources\protobuf)下就找到了生成的java文件,復(fù)制出來(lái)粘貼到項(xiàng)目執(zhí)行目錄下即可。
編寫(xiě)接口實(shí)現(xiàn)類(lèi)
- 編譯完后會(huì)生成RuleServiceGrpc接口,接下來(lái)就是按照自己的業(yè)務(wù)需求編寫(xiě)邏輯即可。小編定義的兩個(gè)接口分別是 getArchivesDic(更新規(guī)則)、getRule(查詢(xún)規(guī)則)。具體實(shí)現(xiàn)如下
// 繼承生成的RuleServiceGrpc.RuleServiceImplBase
// 實(shí)現(xiàn)接口具體邏輯
@Component
public class RuleGRPCServer extends RuleServiceGrpc.RuleServiceImplBase {
// 更新規(guī)則字典
@Override
public void getArchivesDic(RuleRequest request, StreamObserver<RuleResponse> responseObserver) {
Response ruleRPCDTO = request.getRuleRPCDTO();
RuleDTO ruleDTO = new RuleDTO();
BeanUtils.copyProperties(ruleRPCDTO, ruleDTO);
RuleResponse ruleResponse = RuleResponse.newBuilder().setId(1).build();
responseObserver.onNext(ruleResponse);
responseObserver.onCompleted();
}
// 查詢(xún)規(guī)則字典
@Override
public void getRule(Request request, StreamObserver<Response> responseObserver) {
Response response = Response.newBuilder().setRuleCode(1)
.setAdministrativeCost("2222").build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
服務(wù)端與客戶(hù)端
public static void main(String[] args) throws Exception {
// 設(shè)置service接口.
Server server = ServerBuilder.forPort(9999).addService(new RuleGRPCServiceImpl()).build().start();
System.out.println(String.format("GRpc服務(wù)端啟動(dòng)成功, 端口號(hào): %d.", port));
server.awaitTermination();
}
日志 --- GRpc服務(wù)端啟動(dòng)成功, 端口號(hào): 9999. public static void main(String[] args) throws Exception {
// 1. 拿到一個(gè)通信的channel
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9999).usePlaintext().build();
try {
// 2.拿到道理對(duì)象
RuleServiceGrpc.RuleServiceBlockingStub rpcDateService = RuleServiceGrpc.newBlockingStub(managedChannel);
Request rpcDateRequest = Request
.newBuilder()
.build();
// 3. 請(qǐng)求
Response rpcDateResponse = rpcDateService.getRule(rpcDateRequest);
// 4. 輸出結(jié)果
System.out.println(rpcDateResponse.getRuleCode());
} finally {
// 5.關(guān)閉channel, 釋放資源.
managedChannel.shutdown();
}
}
日志:
- 16:05:44.628 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] INBOUND DATA: streamId=3 padding=0 endStream=false length=12 bytes=0000000007086f1203323232
- 16:05:44.648 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] INBOUND HEADERS: streamId=3 headers=GrpcHttp2ResponseHeaders[grpc-status: 0] padding=0 endStream=true
- 輸出結(jié)果-----111
- 16:05:44.664 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler - [id: 0x8447cc92, L:/127.0.0.1:60973 - R:localhost/127.0.0.1:9999] OUTBOUND GO_AWAY: lastStreamId=0 errorCode=0 length=0 bytes=客戶(hù)端日志輸出結(jié)果即表示客戶(hù)端通過(guò)gRPC調(diào)用服務(wù)端成功,并返回結(jié)果。
?總結(jié)
gRPC本質(zhì)上就是傳統(tǒng)的C|S模型,這樣看角色分的清楚,也很容易理解。
還有就是它很聰明的點(diǎn)是基于HTTP2.0協(xié)議的,而不是自己制定,這就對(duì)未來(lái)的網(wǎng)絡(luò)開(kāi)發(fā)很友好,降低了門(mén)檻。
比較難上手的點(diǎn)在于proto文件的編寫(xiě)和使用,這部分需要插件等依賴(lài),過(guò)程相對(duì)復(fù)雜,但也可能會(huì)出現(xiàn)工具或腳本,可以簡(jiǎn)化下這部分。但生成代碼確實(shí)是真香~ 減少了一部分工作量。?