grpcurl 是一個多平臺可以用的,可以發(fā)送grpc請求到服務器的客戶端工具,可以用于grpc服務器的各種測試和debug
列出一個grpc servert提供的所有服務(如果服務器沒有添加TLS證書,需要使用-plaintext參數(shù))
grpcurl -plaintext localhost:6565 list
帶參數(shù)請求,參數(shù)必須放在host地址前
grpcurl -d '{"first_name":"Tom","last_name":"Willson"}' -plaintext localhost:6565 list
請求時參數(shù)的順序并不一定要跟proto文件中設定的一致,參數(shù)名一致了就行
此處-d后面跟的必須是單個positional argument,如果使用windows的cmd和powershell,可能會產(chǎn)生各種解析問題,以下為powershell的正確范例
> grpcurl -d '{\"first_name\":\"Tom\",\"last_name\":\"Willson\"}' -plaintext localhost:6565 UserVerifyService/sayHello
{
"message": "Hello Tom Willson"
}
使用參數(shù)-authority可以假裝請求server為另一個,從而通過證書校驗,在測試環(huán)境很管用
> grpcurl -d '{\"first_name\":\"Tom\",\"last_name\":\"Willson\"}' localhost:6565 UserVerifyService/sayHello
Failed to dial target host "localhost:6565": x509: certificate is valid for mila.lab, not localhost
> grpcurl -d '{\"first_name\":\"Tom\",\"last_name\":\"Willson\"}' -authority='mila.lab' localhost:6565 UserVerifyService/sayHello
{
"message": "Hello Tom Willson"
}