grpcurl使用

更新时间:2023-04-09 14:13:50 阅读:120 所属栏目: 日志

grpcur是一个与grpc服务器交互的命令行工具

默认会使用服务器反射功能和使用https连接 如下所示

grpcurl localhost:3000 list

因为默认使用https连接,如果服务器不支持https就会报如下错误

Failed to dial target host "localhost:3000": tls: first record does not look like a TLS handshake

使用如下命令使用http执行

grpcurl -plaintext localhost:3000 list

默认需要服务器开启grpc反射,没开启就报如下错误

Failed to list services: server does not support the reflection API

这时候使用 -proto 选项指定protobuf文件即可

grpcurl -plaintext -proto ./proto/helloworld.proto localhost:3000  helloworld.Greeter/SayHello

如果要传递参数就使用 -d 参数

grpcurl -plaintext -d '{"name":"fuyoo"}' -proto ./proto/helloworld.proto localhost:3000  helloworld.Greeter/SayHello

使用 -d 参数的同时如果使用 @ 占位符就是从标准输入获取实际的请求体 如下所示

grpcurl -plaintext -d '{"name":"fuyoo"}' \
-proto ./proto/helloworld.proto \
localhost:3000  helloworld.Greeter/SayHello \
<<EOM
{
  "name":"fuyoo"
}
EOM

更多使用参数请查看 grpcurl -h

工具官方地址:grpcurl