gRPC测试
gRPC服务测试说明和命令参考,快速生成gRPC调用命令
🔧 命令生成
📝 Proto模板
📖 命令参考
Unary RPC
syntax = "proto3";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}Server Streaming RPC
service StreamService {
rpc ListItems (ListRequest) returns (stream Item) {}
}Client Streaming RPC
service UploadService {
rpc Upload (stream Chunk) returns (UploadStatus) {}
}Bidirectional Streaming RPC
service ChatService {
rpc Chat (stream ChatMessage) returns (stream ChatMessage) {}
}安装 grpcurl
# macOS brew install grpcurl # Linux go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest # Windows (Scoop) scoop install grpcurl
列出服务
# 列出所有服务 grpcurl -plaintext localhost:50051 list # 列出服务方法 grpcurl -plaintext localhost:50051 list helloworld.Greeter # 查看方法描述 grpcurl -plaintext localhost:50051 describe helloworld.Greeter.SayHello
调用方法
# Unary调用
grpcurl -plaintext -d '{"name":"World"}' localhost:50051 helloworld.Greeter/SayHello
# 使用proto文件
grpcurl -plaintext -proto api.proto -d '{"name":"World"}' localhost:50051 helloworld.Greeter/SayHello
# 带Metadata
grpcurl -plaintext -H "Authorization: Bearer token" -d '{}' localhost:50051 api.Service/Method使用 grpcui (Web UI)
# 安装 go install github.com/fullstorydev/grpcui/cmd/grpcui@latest # 启动 grpcui -plaintext localhost:50051
使用 Postman
Postman 原生支持 gRPC:新建请求 → 选择 gRPC → 输入服务地址 → 导入 .proto 文件或使用 Server Reflection