grpcur是一个与grpc服务器交互的命令行工具,可认为是gRPC的curl
工具。
grpcurl用于从命令行调用gRPC服务器支持的RPC方法,gRPC使用二进制编码(protobuf), 不能利用常规的curl工具(早期的curl版本还不支持HTTP/2)。
grpcurl工具接受json编码的消息(对人类和脚本更友好), 工具底层会转化为protobuf与服务器交互。
grpcurl 必须了解服务的Protobuf协议(服务的schema),才能调用它们, grpcurl通过三种方式之一实现此目的。
gRPCurl是有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();
}
});
}
有各种操作系统的适配版本。
grpcurl list/describe 可以列出gRPC服务端反射的protobuf
grpcurl localhost:5001 list
greet.Greeter
grpc.reflection.v1alpha.ServerReflection
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 -d ( Data for request contents) 传参调用gRPC方法
grpcurl -d {"name":"World"} localhost:5001 greet.Greeter/SayHello
{
"message": "Hello World"
}
gRPCui 是 gRPC 的交互式 Web UI,基于gRPCurl,并提供一个GUI来发现和测试 gRPC 服务,类似于 Postman 或 Swagger UI 等 HTTP 工具。
会立刻打开类Swagger窗口:
输入Request Header、Request Data,自行倒腾。
就是这么神奇!
以上是利用gRPC服务反射,获取protobuf的方式,grcpurl还支持直接读取protobuf文件。
`grpcurl -import-path ../protos -proto greet.proto -d {\"name\":\"World\"} localhost:5001 greet.Greeter/SayHello`
手机扫一扫
移动阅读更方便
你可能感兴趣的文章