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