# cURL

curl 是一个常见的命令行工具,用来请求 web 服务器接口,c 就是 client 的意思。
curl 支持的协议很多

  • http
  • https
  • FTP
  • SCP
  • SMTP
  • Websockect

其使用方法可以参考这里: https://www.ruanyifeng.com/blog/2019/09/curl-reference.html

但是 grpc 是不支持的.

# grpcurl

grpcurl 就是用来支持 grpc 的一个工具。
支持功能包括:

  • 调用 RPC 接口,支持携带参数
  • 可以根据 proto 列出来所有的服务
  • 可以通过反射生成服务的描述

# 试用

# 环境搭建

  • 安装 go
  • 安装 grpcurl
  • proto 生成相关(非必须)
    这里就不赘述了。

# 启动 grpc 服务

首先本地起一个 grpc 服务,这里可以简单使用官方提供的模板。

git clone -b v1.57.0 --depth 1 https://github.com/grpc/grpc-go
cd grpc-go/examples/helloworld

修改服务,因为 grpcurl 需要服务器支持反射

// 修改 grpc-go/examples/helloworld/greeter_server/main.go
s := grpc.NewServer()  
  
reflection.Register(s) // 添加这一行

然后启动服务

go run main.go

# 使用 grpcurl

# 列举服务

因为本地不是 tls 服务,需要加 - plaintext

(base) PS C:\Users\fanlu> grpcurl -plaintext localhost:50051 list
grpc.reflection.v1.ServerReflection
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter
// 指定service
(base) PS C:\Users\fanlu> grpcurl -plaintext localhost:50051 list helloworld.Greeter
helloworld.Greeter.SayHello

# 描述协议

(base) PS C:\Users\fanlu> grpcurl -plaintext localhost:50051 describe helloworld.Greeter.SayHello
helloworld.Greeter.SayHello is a method:
rpc SayHello ( .helloworld.HelloRequest ) returns ( .helloworld.HelloReply );

# 调用协议

(base) PS C:\Users\fanlu> grpcurl -plaintext localhost:50051 helloworld.Greeter.SayHello
{
  "message": "Hello "
}

# 注意:要区分大小写!

更复杂的用法还没用过。

更新于 阅读次数