GRPC (1) protobuf configuration

Table of contents

  1. Configuration and introduction of protobuf for gRPC
  2. go language gRPC server and client
  3. python gRPC server and client

protobuf installation

Github: https://github.com/protocolbuffers/protobuf/releases
download link

First download protoc, then protoc-3.19.4-win64.zip/bin/protoc.exeunzip it GOPATH/bin
and GOPATH/binadd it to the system Path
environment variable

Install the protogo compiler plugin

go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]

Create a new grpc.proto file

syntax = "proto3";

package pb; 
option go_package="./;pb";   // pb是生成.go文件的package名

service GRPC{
    
    
  rpc Ping(ping) returns(pong);
}

message ping {
    
    
  string msg = 1;
}

message pong{
    
    
  string  msg = 1;
}

4. Compile the .proto file

cmd run:

protoc --go_out=. --go_opt=paths=source_relative  --go-grpc_out=. --go-grpc_opt=paths=source_relative  grpc.proto

Compile the .proto file

The first step to generate the protobuf file is complete

protobuf usage

syntax = "proto3";

package xxxx;

message Xxx{
    
    
  string name = 1;
  int32 age = 2;
  bool sex = 3;
  repeated string ss = 4;
  map<string,string> testmap = 5;
}

service XXXService {
    
    
  rpc XXXX(参数) returns (参数);
  rpc XXXX(stream 参数) returns (参数); // 流
}

Guess you like

Origin blog.csdn.net/xuehu96/article/details/122812830