原文連結: 使用 grpcurl 通過指令行通路 gRPC 服務
一般情況下測試 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 時,需要通過
-cert
和
-key
參數設定公鑰和私鑰檔案,表示連結啟用了 TLS 協定的服務。
對于沒有啟用 TLS 協定的 gRPC 服務,通過
-plaintext
參數忽略 TLS 證書的驗證過程。
如果是 Unix Socket 協定,則需要指定
-unix
參數。
檢視服務清單:
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"
}
-d
參數後面也可以跟
@
,表示從标準輸入讀取 json 參數,一般用于輸入比較複雜的 json 資料,也可以用于測試流方法。
grpcurl -d @ -plaintext 127.0.0.1:50051 proto.Greeter.SayHello
可能遇到的錯誤
可能會遇到三個報錯:
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、服務沒有啟動 reflection 反射服務
Failed to list services: server does not support the reflection API
這行代碼是關鍵,一定要包含:
// 注冊 grpcurl 所需的 reflection 服務
reflection.Register(server)
3、參數格式錯誤:
Error invoking method "greet.Greeter/SayHello": error getting request data: invalid character 'n' looking for beginning of object key string
-d
後面參數為 json 格式,并且需要使用
''
包裹起來。
總結
用這個工具做一些簡單的測試還是相當友善的,上手也簡單。隻要掌握文中提到的幾條指令,基本可以涵蓋大部分的測試需求了。
擴充閱讀:
- https://appimage.github.io/BloomRPC/
- https://github.com/fullstorydev/grpcurl
文章中的腦圖和源碼都上傳到了 GitHub,有需要的同學可自行下載下傳。
位址: https://github.com/yongxinz/gopher/tree/main/blog
往期文章清單:
- 被 Docker 日志坑慘了
- 推薦三個實用的 Go 開發工具
- 這個 TCP 問題你得懂:Cannot assign requested address
Go 專欄文章清單:
- Go 專欄|開發環境搭建以及開發工具 VS Code 配置
- Go 專欄|變量和常量的聲明與指派
- Go 專欄|基礎資料類型:整數、浮點數、複數、布爾值和字元串
- Go 專欄|複合資料類型:數組和切片 slice
- Go 專欄|複合資料類型:字典 map 和 結構體 struct
- Go 專欄|流程控制,一網打盡
- Go 專欄|函數那些事
- Go 專欄|錯誤處理:defer,panic 和 recover
- Go 專欄|說說方法
- Go 專欄|接口 interface
- Go 專欄|并發程式設計:goroutine,channel 和 sync