Golang-RPC(七):如何调试gRPC服务

gRPC默认是 protobuf 编码的并不是明文的,其次,类似于RESTFUL API,我们也需要 curl 和 postman 这样的调试工具。gRPC也有类似的工具,对应的分别是grpcurlgrpcui

grpcurl-命令行工具

类似 curl,可以直接用来发送请求调用远程的 grpc 服务,官方地址请看 grpcurl

安装

也可以使用go get安装

go get -u github.com/fullstorydev/grpcurl
go install github.com/fullstorydev/grpcurl/cmd/grpcurl

使用

注意grpcurl是基于反射,需要在启动服务前添加这样一行代码

s := grpc.NewServer()
reflection.Register(s)

1.查询服务列表

grpcurl -plaintext 127.0.0.1:50051 list
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter

2.查询服务提供的方法

grpcurl -plaintext 127.0.0.1:50051 list helloworld.Greeter
helloworld.Greeter.SayHello

3.查看更详细的描述

grpcurl -plaintext 127.0.0.1:50051 describe helloworld.Greeter
helloworld.Greeter is a service:
service Greeter {
  rpc SayHello ( .helloworld.HelloRequest ) returns ( .helloworld.HelloReply );
}

4.获取类型信息

grpcurl -plaintext 127.0.0.1:50051 describe helloworld.HelloReply

输出

helloworld.HelloReply is a message:
message HelloReply {
  string message = 1;
}

5.调用服务方法

grpcurl -plaintext -d '{"name":"rao"}' 127.0.0.1:50051 helloworld.Greeter/SayHello

输出

{
  "message": "Hello rao"
}

grpcui-界面工具

简单的说,就是gRPC中的postman,能带你飞起来那种,官方地址 grpcui

安装

go get -u github.com/fullstorydev/grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui

出现报错
/root/gowork/pkg/mod/github.com/fullstorydev/grpcui@v1.3.0/cmd/grpcui/grpcui.go:31:2: missing go.sum entry for module providing package github.com/pkg/browser (imported by github.com/fullstorydev/grpcui/cmd/grpcui); to add:
        go get github.com/fullstorydev/grpcui/cmd/grpcui@v1.3.0
/root/gowork/pkg/mod/github.com/fullstorydev/grpcui@v1.3.0/cmd/grpcui/grpcui.go:32:2: missing go.sum entry for module providing package golang.org/x/crypto/ssh/terminal (imported by github.com/fullstorydev/grpcui/cmd/grpcui); to add:
        go get github.com/fullstorydev/grpcui/cmd/grpcui@v1.3.0
        
执行
go get github.com/fullstorydev/grpcui/cmd/grpcui@v1.3.0

使用

运行web界面,指定grpc的地址

grpcui -plaintext localhost:50051

gRPC Web UI available at http://127.0.0.1:1728/

提示Web UI的地址为 http://127.0.0.1:1728/
访问出来以下界面
在这里插入图片描述

输入参数,发起请求

在这里插入图片描述

在这里插入图片描述

postman发起gRPC请求

在新建请求时选择 gRPC
在这里插入图片描述
然后要导入你的.proto定义文件
在这里插入图片描述
选择服务发起调用
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值