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) {}
}