当前位置: 首页 > article >正文

grpcurl使用

grpcurl是一个命令行工具,使用它可以在命令行中访问gRPC服务,就像使用curl访问http服务一样。

准备: 在gRPC服务中注册reflection服务 #

gRPC服务是使用Protobuf(PB)协议的,而PB提供了在运行时获取Proto定义信息的反射功能。grpc-go中的"google.golang.org/grpc/reflection"包就对这个反射功能提供了支持。

这里以grpc-go官方的helloword为例,代码结构如下:

1grpc-hello
2├── go.mod
3├── go.sum
4├── main.go
5└── proto
6    ├── doc.go
7    ├── helloworld.pb.go
8    └── helloworld.proto

helloworld.proto:

 1syntax = "proto3";
 2
 3package proto;
 4
 5// The greeting service definition.
 6service Greeter {
 7    // Sends a greeting
 8    rpc SayHello (HelloRequest) returns (HelloReply) {}
 9}
10
11// The request message containing the user's name.
12message HelloRequest {
13    string name = 1;
14}
15
16// The response message containing the greetings
17message HelloReply {
18    string message = 1;
19}

main.go:

 1package main
 2
 3import "fmt"
 4import "log"
 5import "net"
 6import "context"
 7import "grpc-hello/proto"
 8import "google.golang.org/grpc"
 9import "google.golang.org/grpc/reflection"
10
11func main() {
12	lis, err := net.Listen("tcp", ":8080")
13	if err != nil {
14		log.Fatalf("failed to listen: %v", err)
15	}
16
17	server := grpc.NewServer()
18	// 注册grpcurl所需的reflection服务
19	reflection.Register(server)
20	// 注册业务服务
21	proto.RegisterGreeterServer(server, &greeter{})
22	
23	if err := server.Serve(lis); err != nil {
24		log.Fatalf("failed to serve: %v", err)
25	}
26}
27
28type greeter struct {
29
30}
31
32func (*greeter) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloReply, error) {
33	fmt.Println(req)
34	reply := &proto.HelloReply{Message: "hello"}
35	return reply, nil
36}

main.go的第19行,使用reflection.Register(server)注册了reflection服务。

grpcurl的安装和使用 #

在Mac OS下安装grpcurl:

brew install grpcurl

grpcurl使用如下示例如下:

查看服务列表:

1grpcurl -plaintext 127.0.0.1:8080 list
2grpc.reflection.v1alpha.ServerReflection
3proto.Greeter

查看某个服务的方法列表:

1grpcurl -plaintext 127.0.0.1:8080 list proto.Greeter
2proto.Greeter.SayHello

查看方法定义:

1grpcurl -plaintext 127.0.0.1:8080 describe proto.Greeter.SayHello
2proto.Greeter.SayHello is a method:
3rpc SayHello ( .proto.HelloRequest ) returns ( .proto.HelloReply );

查看请求参数:

1grpcurl -plaintext 127.0.0.1:8080 describe proto.HelloRequest
2proto.HelloRequest is a message:
3message HelloRequest {
4  string name = 1;
5}

调用服务,参数传json即可:

1grpcurl -d '{"name": "abc"}' -plaintext 127.0.0.1:8080  proto.Greeter.SayHello
2{
3  "message": "hello"
4}

http://www.kler.cn/news/321385.html

相关文章:

  • gitlab集成CI/CD,shell方式部署
  • EMC术语简要介绍
  • SSM的学习(3)
  • 【论文_1992】 REINFORCE » P2 附录
  • 《程序猿之设计模式实战 · 模板方法》
  • JavaWeb美食推荐管理系统
  • 【Linux扩容根分区】LVM分区扩容过程踩坑记录
  • 计算机视觉硬件整理(四):相机与镜头参数介绍
  • EasyAR自定义相机RTSP视频流(CustomCamera)
  • uniapp自定义底部tabBar
  • Spring Boot入门到精通:网上购物商城系统
  • 实用的Git工作流程
  • docker pull镜像失败问题解决尝试
  • 【2】图像视频的加载和显示
  • 通信工程学习:什么是VIM虚拟化基础设施管理器
  • 操作配置笔记
  • 网络与信息安全工程师(工信部教育与考试中心)
  • 【网络安全】Drupal之缓存中毒+储存型XSS
  • 实时湖仓架构演变
  • 【最基础最直观的排序 —— 选择排序算法】
  • 进阶SpringBoot之 Dubbo-admin 安装测试
  • Node-GDAL:简洁强大的Node.js地理空间数据处理库
  • 什么是Node.js?
  • ElasticSearch的安装与使用
  • EasyCVR智慧公园视频智能管理方案:赋能公园安全管理新高度
  • Spring中一些常见注解的作用
  • 使用ucharts写的小程序页面柱状图上方没有数字
  • 7款国内AI搜索引擎大全网站
  • 说说海外云手机的自动化功能
  • [Redis][哨兵][上]详细讲解