什麼是gRPC
gRPC是谷歌開源的基於go語言的一個現代的開源高性能RPC框架,可以在任何環境中運行。它可以有效地連接數據中心內和跨數據中心的服務,並提供可插拔的支持,以實現負載平衡,跟蹤,健康檢查和身份驗證。它還適用於分佈式計算的最後一英里,用於將設備,移動應用程序和瀏覽器連接到後端服務。
簡單的服務定義:使用Protocol Buffers定義您的服務,這是一個功能強大的二進制序列化工具集和語言
一、搞定protobuf
1)maven,在plugins目錄下加入下面的plugin
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.6.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
2)注意:os.detected.classifier,如何得到:
3)編譯之後,在右邊maven的目錄下,會出現protobuf的插件
4)寫.proto文件,定義服務。注意.proto的位置,和java在同一目錄
modelTraining.proto的內容:
syntax = "proto3";
//定義輸出的目錄,生成的目錄就是“net/devh/examples/grpc/lib”下面
option java_package = "com.product.selftraining.proto";
//定義輸出的文件名稱,生成在lib下的就是HelloWorldProto.class
option java_outer_classname = "ModelTrainingProto";
// The greeting service definition.
//定義的接口的類,這裏會生成一個SimpleGrpc.class,服務端需要來實現的
service ModelTrainingService {
//定義接口方法
//定義訓練接口,調用python的服務
rpc modelTrainning (ModelTrainingRequest) returns (TrainingResponse) {
}
}
//請求參數
message ModelTrainingRequest {
string name = 1;
}
//返回結果
message TrainingResponse {
string message = 1;
}
點擊protobuf的compile和compile-custome
會在target文件夾下生成相應的文件
至此,protobuf搞定
二、grpc-client搭建
新增加依賴:grpc-client起步依賴
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>2.6.1.RELEASE</version>
</dependency>
修改屬性文件,增加如下內容:
#grpc設置
grpc.client.hello-grpc-server.address=static://localhost:6000
grpc.client.hello-grpc-server.enableKeepAlive=true
grpc.client.hello-grpc-server.keepAliveWithoutCalls=true
grpc.client.hello-grpc-server.negotiationType=plaintext
新建調用grpc-server服務的類:
package com.product.selftraining.proto.Impl;
import com.product.selftraining.commons.responses.BaseResponse;
import com.product.selftraining.commons.responses.GrpcModelTrainingResponse;
import com.product.selftraining.proto.ModelTrainingGrpcService;
import com.product.selftraining.proto.ModelTrainingProto;
import com.product.selftraining.proto.ModelTrainingServiceGrpc;
import io.grpc.Channel;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
/**
* @author wangwenxuan
* @date 2020/2/29
* @description grpc調用python端服務的實現類
*/
@Service
public class ModelTrainingGrpcServiceImpl implements ModelTrainingGrpcService {
@GrpcClient("hello-grpc-server")
private Channel serverChannel;
public BaseResponse<GrpcModelTrainingResponse> trainningModel(GrpcModelTrainingResponse grpcModelTrainingResponse){
ModelTrainingServiceGrpc.ModelTrainingServiceBlockingStub stub = ModelTrainingServiceGrpc.newBlockingStub(serverChannel);
//TODO:!!!request和response的搭建
ModelTrainingProto.ModelTrainingRequest.Builder builder= ModelTrainingProto.ModelTrainingRequest.newBuilder();
ModelTrainingProto.TrainingResponse response = stub.modelTrainning(builder.build());
return BaseResponse.success();
}
}
三、grpc-server搭建
目前我不用,因爲server搭在別的語言端