grpcurl使用示例

https://github.com/fullstorydev/grpcurl

grpcurl is a command-line interface (CLI) tool designed for interacting with gRPC (Remote Procedure Call) servers, serving as the equivalent of cURL for HTTP-based APIs. With grpcurl, users can easily send requests to gRPC servers, inspect responses, and debug gRPC-based services directly from the command line. This tool simplifies the process of working with gRPC APIs, allowing developers to test endpoints, diagnose issues, and explore service capabilities with ease.

#列出有哪些服务


[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 list
grpc.health.v1.Health
grpc.reflection.v1alpha.ServerReflection
pb.usermanagerService
pb.randmanagerService

#列出某个服务的方法


[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 list pb.randmanagerService
pb.randmanagerService.GenerateRandomKey
pb.randmanagerService.GetRandomNumber

#查看某个方法的描述


[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 describe pb.randmanagerService.GetRandomNumber
pb.randmanagerService.GetRandomNumber is a method:
rpc GetRandomNumber ( .pb.GetRandomNumberRequest ) returns ( .pb.GetRandomNumberResponse );

#查看某个方法的参数描述


[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 describe pb.GetRandomNumberRequest
pb.GetRandomNumberRequest is a message:
message GetRandomNumberRequest {
  int32 num = 1;
  int32 length = 2;
}

#查看某个方法的返回值描述


[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 describe pb.GetRandomNumberResponse
pb.GetRandomNumberResponse is a message:
message GetRandomNumberResponse {
  int32 code = 1;
  string msg = 2;
  .pb.GetRandomNumberData data = 3;
}
[work@localhost ~]$ grpcurl -plaintext 192.168.10.202:8090 describe pb.GetRandomNumberData
pb.GetRandomNumberData is a message:
message GetRandomNumberData {
  repeated string randoms = 1;
}

#请求某个方法


[work@localhost ~]$ grpcurl -d '{"num":2,"length":8}' -plaintext 192.168.10.202:8090 pb.randmanagerService.GetRandomNumber
{
  "msg": "ok",
  "data": {
    "randoms": [
      "23e9c800",
      "18db8480"
    ]
  }
}

#使用文件请求某个方法


[work@localhost ~]$ cat params.json | grpcurl -d @ -plaintext 192.168.10.202:8090 pb.randmanagerService.GetRandomNumber
{
  "msg": "ok",
  "data": {
    "randoms": [
      "23e9c800",
      "18db8480"
    ]
  }
}

#测试某个方法的耗时


[work@localhost ~]$ date; grpcurl -connect-timeout 3600 -keepalive-time 3600 -max-time 3600 -d '{"num":2,"length":8}' -plaintext 192.168.10.202:8090 pb.randmanagerService.GetRandomNumber; date
2025年 04月 27日 星期日 13:14:52 CST
ERROR:
  Code: DeadlineExceeded
  Message: context deadline exceeded
2025年 04月 27日 星期日 13:16:32 CST

--end--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值