寶藏好物gRPCurl


寶物簡介

grpcur是一個與grpc服務器交互的命令行工具,可認為是gRPC的curl工具。

grpcurl用於從命令行調用gRPC服務器支持的RPC方法,gRPC使用二進制編碼(protobuf), 不能利用常規的curl工具(早期的curl版本還不支持HTTP/2)。

  1. grpcurl工具接受json編碼的消息(對人類和腳本更友好), 工具底層會轉化為protobuf與服務器交互。

  2. grpcurl 必須了解服務的Protobuf協議(服務的schema),才能調用它們, grpcurl通過三種方式之一實現此目的。

  • gRPC服務器添加gRPC反射
  • 直接讀取proto源文件
  • 加載編譯后的protoset文件(包含已被編碼的proto描述文件)

grpcurl命令結構


grpc  --flag  address   command(list/describe) symbol
  -- flag:  tls/plaintext   請求參數
  --  address : host:port
  --  command  list/describe
  --  symbol: 具體服務名/請求參數

grpc  --plaintext -d  @"{message:"hello"}"  localhost:5000   greet.Greeter

特性

gRPCurl是有gRPC上去創建的命令行工具,功能包括:

  • 調用grpc服務,包括流式服務
  • 使用[grpc反射]進行服務發現
  • 列出並描述 grpc服務
  • 支持調用安全(TLS)或者不安全(plain-text)的gRPC服務, 針對TLS有大量配置

ASP.NET Core設置grpc反射

  • 添加Grpc.AspNetCore.Server.Reflection包引用
  • Startup.cs注冊反射
public void ConfigureServices(IServiceCollection services)
{
    services.AddGrpc();
    services.AddGrpcReflection();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGrpcService<GreeterService>();

        if (env.IsDevelopment())
        {
            endpoints.MapGrpcReflectionService();
        }
    });
}

golang 添加grpc反射

  • 添加 google.golang.org/grpc/reflection pkg
  • 添加注冊反射的代碼
 s := grpc.NewServer()
        pb.RegisterGreeterService(s, &pb.GreeterService{SayHello: sayHello})
+       // Register reflection service on gRPC server.
+       reflection.Register(s)
        if err := s.Serve(lis); err != nil {
                log.Fatalf("failed to serve: %v", err)
        }

服務發現、服務調用

grpcurl list/describe 可以列出gRPC服務端反射的protobuf

  1. grpcurl localhost:5001 list

如果grpc服務不帶 TLS, 則grpcurl --plaintext localhost:5000 list

greet.Greeter
grpc.reflection.v1alpha.ServerReflection
  1. grpcurl localhost:5001 describe
greet.Greeter is a service:
service Greeter {
  rpc SayHello ( .greet.HelloRequest ) returns ( .greet.HelloReply );
}
grpc.reflection.v1alpha.ServerReflection is a service:
service ServerReflection {
  rpc ServerReflectionInfo ( stream .grpc.reflection.v1alpha.ServerReflectionRequest ) returns ( stream .grpc.reflection.v1alpha.ServerReflectionResponse );
}

再着重看下請求參數:
grpc localhost:5001 describe greet.HelloRequest, 會給出hellorequest的pb結構。

grpc -d ( Data for request contents) 傳參調用gRPC方法

  1. grpcurl -d {"name":"World"} localhost:5001 greet.Greeter/SayHello
{
  "message": "Hello World"
}

gRPCui

gRPCui 是 gRPC 的交互式 Web UI,基於gRPCurl,並提供一個GUI來發現和測試 gRPC 服務,類似於 Postman 或 Swagger UI 等 HTTP 工具。

  • 安裝: go install github.com/fullstorydev/grpcui/cmd/grpcui@latest
  • 使用: grpcui localhost:5001 , 同樣的,服務不帶TLS, 加上 --plaintext

會立刻打開類Swagger窗口:

輸入Request Header、Request Data,自行倒騰。


就是這么神奇!

以上是利用gRPC服務反射,獲取protobuf的方式,grcpurl還支持直接讀取protobuf文件。

`grpcurl  -import-path ../protos   -proto  greet.proto -d {\"name\":\"World\"} localhost:5001 greet.Greeter/SayHello`


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM