protobuf的使用

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

1.下载protoc-21.1-win64.zip

2.将上面解压的到bin的路径,将此路径添加到系统变量的PATH中

3.下载依赖

go get github.com/golang/protobuf/protoc-gen-go

4.生成go的protobuf文件

protoc -I . goods.proto --go_out=plugins=grpc:.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值