使用 Grpcurl 通过命令行访问 gRPC 服务

开发 架构
一般情况下测试 gRPC 服务,都是通过客户端来直接请求服务端。如果客户端还没准备好的话,也可以使用 BloomRPC 这样的 GUI 客户端。

[[426754]]

一般情况下测试 gRPC 服务,都是通过客户端来直接请求服务端。如果客户端还没准备好的话,也可以使用 BloomRPC 这样的 GUI 客户端。

如果环境不支持安装这种 GUI 客户端的话,那么有没有一种工具,类似于 curl 这样的,直接通过终端,在命令行发起请求呢?

答案肯定是有的,就是本文要介绍的 grpcurl。

gRPC Server

首先来写一个简单的 gRPC Server:

syntax = "proto3"
 
package proto; 
 
// The greeting service definition. 
service Greeter { 
    // Sends a greeting 
    rpc SayHello (HelloRequest) returns (HelloReply) {} 

 
// The request message containing the user's name
message HelloRequest { 
    string name = 1; 

 
// The response message containing the greetings 
message HelloReply { 
    string message = 1; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

main.go

package main 
 
import ( 
    "context" 
    "fmt" 
    "grpc-hello/proto" 
    "log" 
    "net" 
 
    "google.golang.org/grpc" 
    "google.golang.org/grpc/reflection" 

 
func main() { 
    lis, err := net.Listen("tcp"":50051"
    if err != nil { 
        log.Fatalf("failed to listen: %v", err) 
    } 
 
    server := grpc.NewServer() 
    // 注册 grpcurl 所需的 reflection 服务 
    reflection.Register(server) 
    // 注册业务服务 
    proto.RegisterGreeterServer(server, &greeter{}) 
 
    fmt.Println("grpc server start ..."
    if err := server.Serve(lis); err != nil { 
        log.Fatalf("failed to serve: %v", err) 
    } 

 
type greeter struct { 

 
func (*greeter) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloReply, error) { 
    fmt.Println(req) 
    reply := &proto.HelloReply{Message: "hello"
    return reply, nil 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

运行服务:

go run main.go 
 
server start ... 
  • 1.
  • 2.
  • 3.

grpcurl 安装

这里我介绍三种方式:

Mac

brew install grpcurl 
  • 1.

Docker

# Download image 
docker pull fullstorydev/grpcurl:latest 
# Run the tool 
docker run fullstorydev/grpcurl api.grpc.me:443 list 
  • 1.
  • 2.
  • 3.
  • 4.

go tool

如果有 Go 环境的话,可以通过 go tool 来安装:

go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest 
  • 1.

grpcurl 使用

查看服务列表:

grpcurl -plaintext 127.0.0.1:50051 list 
  • 1.

输出:

grpc.reflection.v1alpha.ServerReflection 
proto.Greeter 
  • 1.
  • 2.

查看某个服务的方法列表:

grpcurl -plaintext 127.0.0.1:50051 list proto.Greeter 
  • 1.

输出:

proto.Greeter.SayHello 
  • 1.

查看方法定义:

grpcurl -plaintext 127.0.0.1:50051 describe proto.Greeter.SayHello 
  • 1.

输出:

proto.Greeter.SayHello is a method: 
rpc SayHello ( .proto.HelloRequest ) returns ( .proto.HelloReply ); 
  • 1.
  • 2.

查看请求参数:

grpcurl -plaintext 127.0.0.1:50051 describe proto.HelloRequest 
  • 1.

输出:

proto.HelloRequest is a message: 
message HelloRequest { 
  string name = 1; 

  • 1.
  • 2.
  • 3.
  • 4.

请求服务:

grpcurl -d '{"name": "zhangsan"}' -plaintext 127.0.0.1:50051 proto.Greeter.SayHello 
  • 1.

输出:


  "message""hello" 

  • 1.
  • 2.
  • 3.

可能遇到的错误

可能会遇到两个报错:

1、gRPC Server 未启用 TLS:

报错信息:

Failed to dial target host "127.0.0.1:50051": tls: first record does not look like a TLS handshake 
  • 1.

解决:

请求时增加参数:-plaintext,参考上面的命令。

2、参数格式错误:

报错信息:

Error invoking method "greet.Greeter/SayHello": error getting request data: invalid character 'n' looking for beginning of object key string 
  • 1.

解决:

-d 后面参数为 json 格式,并且需要使用 '' 包裹起来。

总结:

用这个工具做一些简单的测试还是相当方便的,上手也简单。只要掌握文中提到的几条命令,基本可以涵盖大部分的测试需求了。

扩展阅读:

https://appimage.github.io/BloomRPC/

https://github.com/fullstorydev/grpcurl

文章中的脑图和源码都上传到了 GitHub,有需要的同学可自行下载。

地址: https://github.com/yongxinz/gopher/tree/main/blog

本文转载自微信公众号「AlwaysBeta」,可以通过以下二维码关注。转载本文请联系AlwaysBeta公众号。

 

责任编辑:武晓燕 来源: AlwaysBeta
相关推荐

2015-12-30 14:47:01

LinuxDropbox访问

2012-02-08 16:37:36

ibmdw

2023-08-01 13:31:18

模型Alpacaicuna

2020-10-31 08:20:39

curl命令命令行互联网

2010-11-16 11:55:31

Oracle命令行

2018-01-24 16:30:43

Linux命令Wifi

2010-10-12 17:01:21

MySQL命令行

2010-06-01 19:14:53

SVN命令行

2014-01-24 09:09:32

Linux命令行网速

2014-04-01 10:22:47

Linux命令行Amazon S3云存储

2010-10-12 17:08:16

MySQL命令行

2023-04-18 17:11:43

命令Linux

2010-03-01 13:40:12

Linux引导安装

2010-10-19 09:45:23

SQL Server命

2010-05-18 15:48:03

2010-05-18 15:06:46

Subversion命

2010-05-18 16:05:24

2020-12-10 16:16:08

工具代码开发

2020-12-11 06:44:16

命令行工具开发

2009-08-16 20:24:59

linux命令行登陆linux命令行linux命令
点赞
收藏

51CTO技术栈公众号