Table of contents
- Configuration and introduction of protobuf for gRPC
- go language gRPC server and client
- python gRPC server and client
protobuf installation
Github: https://github.com/protocolbuffers/protobuf/releases
First download protoc, then protoc-3.19.4-win64.zip/bin/protoc.exe
unzip it GOPATH/bin
and GOPATH/bin
add it to the system Path
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
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 (参数); // 流
}