热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GolangRPC(七):如何调试gRPC服务

gRPC默认是protobuf编码的并不是明文的,其次,类似于RESTFULAPI,我们也需要curl和postman这样的调试工具。gR

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:50051gRPC Web UI available at http://127.0.0.1:1728/

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

输入参数,发起请求

在这里插入图片描述

在这里插入图片描述


推荐阅读
author-avatar
mobiledu2502855037
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有