grpcurl通过命令行访问gRPC服务

作者:yongxinz 时间:2022-05-19 13:18:11 

前言

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

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

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

gRPC Server

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

helloworld.proto

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;
}

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
}

运行服务:

go run main.go
server start ...

grpcurl 安装

这里我介绍三种方式:

Mac

brew install grpcurl

Docker

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

go tool

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

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

grpcurl 使用

查看服务列表:

grpcurl -plaintext 127.0.0.1:50051 list

输出:

grpc.reflection.v1alpha.ServerReflection
proto.Greeter

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

grpcurl -plaintext 127.0.0.1:50051 list proto.Greeter

输出:

proto.Greeter.SayHello

查看方法定义:

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

输出:

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

查看请求参数:

grpcurl -plaintext 127.0.0.1:50051 describe proto.HelloRequest

输出:

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

请求服务:

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

输出:

{
  "message": "hello"
}

可能遇到的错误

可能会遇到两个报错:

1、gRPC Server 未启用 TLS:

报错信息:

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

解决:

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

2、参数格式错误:

报错信息:

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

解决:

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

来源:https://mp.weixin.qq.com/s/GShwcGCopXVmxCKnYf5FhA

标签:grpcurl,命令行,服务访问,gRPC
0
投稿

猜你喜欢

  • Vue3状态管理之Pinia的入门使用教程

    2024-05-09 15:12:03
  • JS高阶函数原理与用法实例分析

    2024-05-09 10:36:21
  • PyQt 线程类 QThread使用详解

    2021-09-18 05:51:37
  • Python使用pylab库实现绘制直方图功能示例

    2022-12-07 04:48:56
  • pygame游戏之旅 游戏中添加显示文字

    2023-03-26 02:56:23
  • CSS兼容性(IE和Firefox)技巧大全

    2010-07-29 12:29:00
  • php实现二叉树中和为某一值的路径方法

    2023-07-04 20:29:08
  • django foreignkey外键使用的例子 相当于left join

    2021-04-17 15:52:33
  • flask框架视图函数用法示例

    2022-07-18 09:36:18
  • python 判断三个数字中的最大值实例代码

    2021-03-23 00:01:04
  • 用ASP对网页进行限制性的访问

    2008-07-03 13:02:00
  • go自动下载所有的依赖包go module使用详解

    2024-04-28 10:49:48
  • Python中lru_cache的使用和实现详解

    2022-04-22 10:48:28
  • python3 最常用的三种装饰器语法汇总

    2021-10-10 15:08:12
  • Golang将Map的键值对调的实现示例

    2024-04-27 15:32:40
  • python科学计算之scipy——optimize用法

    2022-03-14 03:59:44
  • 关于python线程池的四种实现方式

    2023-08-23 06:52:30
  • Pycharm学习教程(2) 代码风格

    2022-03-21 08:38:31
  • PHP入门基础之注释的写法

    2023-10-20 07:26:17
  • javascript实现倒计时提示框

    2024-04-22 22:30:35
  • asp之家 网络编程 m.aspxhome.com