grpc命令行工具grpcurl使用

安装

go get github.com/fullstorydev/grpcurl
go install github.com/fullstorydev/grpcurl/cmd/grpcurl

注册reflection服务

grpcurl对于其他grpc服务的感知皆来自reflection服务,所以在注册自己的服务之前需要先注册reflection服务,否则会提示

$ grpcurl -plaintext localhost:8888 list
Failed to list services: server does not support the reflection API
package main

import (
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	proto "let_me_go/protobuf"
	"log"
	"net"
)

func main() {
	grpcServer := grpc.NewServer()
	// 注册grpcurl的reflection服务
	reflection.Register(grpcServer)
	proto.RegisterHelloServiceServer(grpcServer, new(proto.HelloServiceImpl))

	listen, e := net.Listen("tcp", "localhost:8888")

	if e != nil {
		log.Fatal(e)
	}

	grpcServer.Serve(listen)
}

grpcurl使用

查询服务列表

➜  Desktop grpcurl -plaintext  localhost:8888 list
grpc.reflection.v1alpha.ServerReflection
proto.HelloService

查询服务提供的方法

➜  Desktop grpcurl -plaintext  localhost:8888 list proto.HelloService
proto.HelloService.Channel
proto.HelloService.Hello

查看更详细的描述

➜  Desktop grpcurl -plaintext  localhost:8888 describe proto.HelloService
proto.HelloService is a service:
service HelloService {
  rpc Channel ( stream .proto.String ) returns ( stream .proto.String );
  rpc Hello ( .proto.String ) returns ( .proto.String );
}

获取类型信息

➜  Desktop grpcurl -plaintext  localhost:8888 describe proto.String
proto.String is a message:
message String {
  string value = 1;
}

调用服务方法

➜  Desktop grpcurl -plaintext -d '{"value":"grpcurl"}' localhost:8888 proto.HelloService/Hello
{
  "value": "hello grpc"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值