当前位置:网站首页>Golang-RPC(七):如何调试gRPC服务

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

2022-06-09 11:45:00 raoxiaoya

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/[email protected]/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/[email protected]
/root/gowork/pkg/mod/github.com/fullstorydev/[email protected]/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/[email protected]
        
执行
go get github.com/fullstorydev/grpcui/cmd/[email protected]

使用

运行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/
访问出来以下界面
在这里插入图片描述

输入参数,发起请求

在这里插入图片描述

在这里插入图片描述

原网站

版权声明
本文为[raoxiaoya]所创,转载请带上原文链接,感谢
https://blog.csdn.net/raoxiaoya/article/details/125178375