1.参考文档
GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format
2.python的依赖
python -m pip install grpcio #安装grpc
python -m pip install grpcio-tools #安装grpc tools
3.生成py文件的命令
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto
# .代表当前文件下
syntax = "proto3";
import "google/protobuf/empty.proto";
option go_package = "./;proto";
service UserService {
rpc GetUserList(PageInfo) returns (UserInfoList);
rpc GetUserByField(FieldInfo) returns (UserInfo);
rpc CreateUser(CreateUserInfo) returns (UserInfo); //添加用户
rpc UpdateUser(UpdateUserInfo) returns (google.protobuf.Empty); // 更新用户
rpc CheckPassWord (PasswordCheckInfo) returns (Status); //检查密码
}
message UserInfo {
int32 id = 1;
string userName = 2;
string passWord = 3;
string mobile = 4;
string avatar = 5;
string email = 6;
bool isDelete = 7;
bool isActive = 8;
bool isSuperuser = 9;
string createTime = 10;
string updateTime = 11;
string birthDay = 12;
}
5.测试生成的代码
from grpc_test import helloworld_pb2
request = helloworld_pb2.HelloRequest()
request.name = "xxx"
req_str = request.SerializeToString()
print(req_str)
request2 = helloworld_pb2.HelloRequest()
request2.ParseFromString(req_str)
print(request2.name)
6.go的protoc的安装
https://github.com/protocolbuffers/protobuf/releases
2.将上面解压的到bin的路径,将此路径添加到系统变量的PATH中
3.下载依赖
go get github.com/golang/protobuf/protoc-gen-go
4.生成go的protobuf文件
protoc -I . goods.proto --go_out=plugins=grpc:.