springboot 整个grpc

什么是gRPC

gRPC是谷歌开源的基于go语言的一个现代的开源高性能RPC框架,可以在任何环境中运行。它可以有效地连接数据中心内和跨数据中心的服务,并提供可插拔的支持,以实现负载平衡,跟踪,健康检查和身份验证。它还适用于分布式计算的最后一英里,用于将设备,移动应用程序和浏览器连接到后端服务。

简单的服务定义:使用Protocol Buffers定义您的服务,这是一个功能强大的二进制序列化工具集和语言

一、搞定protobuf

 1)maven,在plugins目录下加入下面的plugin

            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.5.0
                
                    com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:1.6.1:exe:${os.detected.classifier}
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            

2)注意:os.detected.classifier,如何得到:

如何得到os.detected.classifier

3)编译之后,在右边maven的目录下,会出现protobuf的插件

springboot 整个grpc_第1张图片

4)写.proto文件,定义服务。注意.proto的位置,和java在同一目录

springboot 整个grpc_第2张图片

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

springboot 整个grpc_第3张图片

会在target文件夹下生成相应的文件

springboot 整个grpc_第4张图片

至此,protobuf搞定

二、grpc-client搭建

新增加依赖:grpc-client起步依赖

        
            net.devh
            grpc-client-spring-boot-starter
            2.6.1.RELEASE
        

修改属性文件,增加如下内容:

#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 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搭在别的语言端

 

 

你可能感兴趣的:(grpc)