grpcurl通過命令行訪問gRPC服務(wù)
前言
一般情況下測試 gRPC 服務(wù),都是通過客戶端來直接請求服務(wù)端。如果客戶端還沒準(zhǔn)備好的話,也可以使用 BloomRPC 這樣的 GUI 客戶端。
如果環(huán)境不支持安裝這種 GUI 客戶端的話,那么有沒有一種工具,類似于 curl
這樣的,直接通過終端,在命令行發(fā)起請求呢?
答案肯定是有的,就是本文要介紹的 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?服務(wù) ????reflection.Register(server) ????//?注冊業(yè)務(wù)服務(wù) ????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 }
運(yùn)行服務(wù):
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 環(huán)境的話,可以通過 go tool 來安裝:
go?install?github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
grpcurl 使用
查看服務(wù)列表:
grpcurl?-plaintext?127.0.0.1:50051?list
輸出:
grpc.reflection.v1alpha.ServerReflection
proto.Greeter
查看某個服務(wù)的方法列表:
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 );
查看請求參數(shù):
grpcurl?-plaintext?127.0.0.1:50051?describe?proto.HelloRequest
輸出:
proto.HelloRequest is a message:
message HelloRequest {
string name = 1;
}
請求服務(wù):
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
解決:
請求時增加參數(shù):-plaintext
,參考上面的命令。
2、參數(shù)格式錯誤:
報錯信息:
Error?invoking?method?"greet.Greeter/SayHello":?error?getting?request?data:?invalid?character?'n'?looking?for?beginning?of?object?key?string
解決:
-d
后面參數(shù)為 json 格式,并且需要使用 ''
包裹起來。
總結(jié)
用這個工具做一些簡單的測試還是相當(dāng)方便的,上手也簡單。只要掌握文中提到的幾條命令,基本可以涵蓋大部分的測試需求了
擴(kuò)展閱讀:
https://appimage.github.io/BloomRPC/
https://github.com/fullstorydev/grpcurl
源碼下載地址:https://github.com/yongxinz/gopher/tree/main/blog
以上就是grpcurl通過命令行訪問gRPC服務(wù)的詳細(xì)內(nèi)容,更多關(guān)于grpcurl命令行訪問gRPC服務(wù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語言實現(xiàn)冒泡排序、選擇排序、快速排序及插入排序的方法
這篇文章主要介紹了Go語言實現(xiàn)冒泡排序、選擇排序、快速排序及插入排序的方法,以實例形式詳細(xì)分析了幾種常見的排序技巧與實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下2015-02-02Golang的Crypto/SHA256庫實戰(zhàn)指南
無論是在保護(hù)數(shù)據(jù)安全、驗證數(shù)據(jù)完整性,還是在構(gòu)建復(fù)雜的安全系統(tǒng)中,crypto/sha256都是Golang程序員不可或缺的工具,本文主要介紹了Golang的Crypto/SHA256庫實戰(zhàn)指南,感興趣的可以了解一下2024-02-02