gRPC使用


gRPC的基本使用 (重点)

  • IDL定义接口
    • 使用编译器来生成grpc代码
    • 安装包 pip install grpcio-tools
    • 编译命令 python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. test.proto

test.proto

syntax = 'proto3';  // 设置协议版本

/*
需求: test1(age=30, scores=[60, 50, 40]) -> name="zs", info={max_score: 60}
*/

message RequestArgs{  // message用于定义参数类型
    int32 age=1;  // 同一个message中字段编号不能相同
    repeated int32 scores=2;  // 定义列表形式的参数
}

message Info {  // 自定义参数类型
    int32 max_score=1;
}

message ResponseArgs {
    string name=1;
    Info info=2;
}


service Test{  // 定义服务 对远程调用的函数进行分组, 如推荐系统组, AI聊天组
    rpc test1(RequestArgs) returns (ResponseArgs) {}
}

实现服务器

客户端代码


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM