安装
mac:brew install grpcurl
使用
-
查看服务列表
->grpcurl -plaintext ip:port list - proto.Greeter //输出的服务名
-
查看某个服务的方法
->grpcurl -plaintext ip:port list 服务名 - grpcurl -plaintext ip:port list proto.Greeter - proto.Greeter.abc - proto.Greeter.123 //方法名
-
查看某个方法的定义
->grpcurl -plaintext ip:port describe proto.Greeter.abc - proto.Greeter.abc is a method: rpc abc ( .proto.HelloRequest ) returns ( .proto.HelloReply );
-
查看请求参数
->grpcurl -plaintext ip:port describe proto.HelloRequest - proto.HelloRequest is a message: message HelloRequest { string name = 1; }
-
请求服务
->grpcurl -d '{"name": "zhangsan"}' -plaintext ip:port proto.Greeter.SayHello - { "message": "hello" }
错误解决
-
TLS证书错误:
Failed to dial target host "127.0.0.1:50001" : tls: first record does not look like a TLS handshake
解决:没有启用 TLS 协议的 gRPC 服务,通过
-plaintext
参数忽略 TLS 证书的验证过程。 -
服务没有方法:
Error invoking method "proto.Greeter.SayHello": service "proto.Greeter" does not include a method named "SayHello"
解决思路:
1.判断是否有多个环境使用不同版本的服务
2.如果没有多环境的话,检查ip和port
-
参数格式错误
Error invoking method "greet.Greeter/SayHello": error getting request data: invalid character 'u' looking for beginning of object key string
解决:grpcurl中的JSON请求参数体需要使用’ ‘进行包裹,并且注意请求体重参数的格式。