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


猜你喜欢
- 本文实例为大家分享了python实现图像识别的具体代码,供大家参考,具体内容如下#! /usr/bin/env python from PI
- Python脚本常见参数获取和处理平常写 python 脚本时会有一些从命令行获取参数的需求,这篇文章记录下常见的参数获取和处理方式。1.
- 引言在经历过一些尝试之后,觉得在当下的项目中运用链路压测的能力,不等着其他人了。链路这个词其实不如路径通俗易懂,跟产品沟通这个比较有效率。具
- 刚我在做Tree view 绑定时自己摸索了一下,网上有人说TreeView绑定数据源,用什么递归绑定啥的,我不想看了,就自己试着写了一个我
- 120726 11:57:22 [Warning] 'user' entry 'root@localhost.loc
- 项目环境:python3.6一、项目结构二、数据集准备数据集准备分为两步:获取图片.提取人脸.1、获取图片首先可以利用爬虫,从百度图片上批量
- 目录一、常见orm数据库框架1、peewee 简单demo二、Model 和 Field 关系三、Model 模型四、Filed 字段1、字
- 准备本篇文章译自英文文档 Compile PyTorch Models。作者是 Alex Wong。更多 TVM 中文文档可访问 &
- 一般TensorFlow中扩展维度可以使用tf.expand_dims()。近来发现另一种可以直接运用取数据操作符[]就能扩展维度的方法。用
- 使用Vue实现简单的用户登录界面,登录成功以后查询账号用户类型进行相应的页面路由跳转,效果如下图所示:HTML部分:<div clas
- 很久没有发表文章了,最近一直在研究产品设计标准的问题,之前有发过一篇关于 Axure的教程 ,相信很多人已经学会如何使用,这次我给大家介绍一
- 随着CSS3越来越热,CSS3动画也逐渐受到大家的关注。这次有幸修改淘宝网全站页头,小小地应用了下(详见http://www.taobao.
- 本文实例讲述了python通过pil模块将raw图片转换成png图片的方法。分享给大家供大家参考。具体分析如下:python通过pil模块将
- 点击按钮,出现半透明遮罩层弹框,说说自己之前发过的愁吧1、遮罩层半透明了 弹框也跟着半透明了 就像这样 绝望吧 是哪里错了呢?你的
- 文章主要讲术了一些SQL Server新的Bug,帮您认识这些被忽略的SQL Server注入技巧。1.关于Openrowset和Opend
- Python中有以下几个基本的数据类型:整数 int字符串 str浮点数 float集合 set列表 list元组 tuple字典 dict
- 平时自己写了很多代码,但从没好好计算总共写了多少行,面试时被问起来,就傻了。。。闲来无事,写个python程序来统计下import os##
- 一.__eq__方法在我们定义一个类的时候,常常想对一个类所实例化出来的两个对象进行判断这两个对象是否是完全相同的。一般情况下,我们认为如果
- 本文实例讲述了javascript设计模式 – 简单工厂模式。分享给大家供大家参考,具体如下:介绍:简单工厂模式是最常用的一类创建型设计模式
- 在这篇文章(不敢妄称教程,最多称之为学习笔记)里,我会从头开始实现客户端模板的效果。不过你不要期望能够在这里找到可以直接拿去使用直接复用灵活