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
猜你喜欢
- 网上http接口自动化测试Python实现有很多,我也是在慕课网上学习了相关课程,并实际操作了一遍,于是进行一些总结,便于以后回顾温习,有许
- 这是一个access较为豪华的包装范例,他调用了flash的流媒体控件,利用flash的交互与通信功能,借以达到了我们装扮软件的目的。fla
- 参考的一些文章以及论文我都会给大家分享出来 —— 链接就贴在原文,论文我上传到资源中去,大家可以免费下载学习,如果当天资源区找不到论文,那就
- 一、property() 函数讲解了解 @property 装饰器之前,我们首先要了解内置函数的 property()。class prop
- 场景游戏里有很多关卡(可能有几百个了),理论上每次发布到外网前都要遍历各关卡看看会不会有异常,上次就有玩家在打某个关卡时卡住不动了,如果每个
- 下面先看下python 使用值排序字典的方法In [8]: a={'x':11,'y':22,'c&
- 函数:startswith()作用:判断字符串是否以指定字符或子字符串开头一、函数说明语法:string.startswith(str, b
- 如下所示:import matplotlib.pyplot as pltimport numpy as npfrom scipy impor
- 这篇文章主要是想说,“引用只能指向具体对象而不能指向引用”//创建变量testArray 并引用数组 ["1&
- 简介字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能改变通常由单引号(' ),双引号(" ),
- 自己写的小工具,可以直接获取csdn文章并转换为markdown格式效果图核心代码from PySide2.QtWidgets import
- Python对象动态的增加属性和方法前面我们了解到数据封装、继承和多态只是面向对象程序设计中最基础的3个概念。在Python中,面向对象还有
- 一、使用 PyCharm 软件安装 pandas 包1、打开 PyCharm2、点击右上角 “Files” →“Settings...”3、
- Python 安装Graphviz 详细教程Python安装Graphviz画图器首先,要明确他是一个独立的软件,如果大家用pip的方法装了
- 让你的读者能够方便地收藏你的文章到社会化书签(网摘)网站,如 新浪,google,yahoo,Del.icio.us, 365key等添加到
- 本文实例为大家分享了Python画圣诞树的具体代码,供大家参考,具体内容如下源代码from turtle import *import ra
- django上线后,需要把setting.py文件的debug=True改为False,以防暴露代码报错问题。因为我项目用到css的地方只有
- 一、什么是shutilshutil可以简单地理解为sh + util ,shell工具的意思。shutil模块是对os模块的补充,主要针对文
- 安装数据可视化模块matplotlib:pip install matplotlib导入matplotlib模块下的pyplot1 折线图f
- Microsoft office Access Database Engine 错误 '80040e14' Insert I