标签:ref name min 验证 启动 news target 复杂 url
reflection
包中只有一个Register函数,用于将grpc.Server
注册到反射服务中。reflection
包文档给出了简单的使用方法:import (
"google.golang.org/grpc/reflection"
)
func main() {
s := grpc.NewServer()
pb.RegisterYourOwnServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
s.Serve(lis)
}
$ go get github.com/fullstorydev/grpcurl
$ go install github.com/fullstorydev/grpcurl/cmd/grpcurl
list
命令,用于获取服务或服务方法的列表。比如grpcurl localhost:1234 list
命令将获取本地1234端口上的grpc服务的列表。在使用grpcurl
时,需要通过-cert和-key参数设置公钥和私钥文件
,链接启用了tls协议的服务。对于没有没用tls协议的grpc服务,通过-plaintext
参数忽略tls证书的验证过程。如果是Unix Socket协议,则需要指定-unix
参数。$ grpcurl localhost:1234 list
>> Failed to dial target host "localhost:1234": tls: first record does not look like a TLS handshake
$ grpcurl -plaintext localhost:1234 list
>> Failed to list services: server does not support the reflection API
syntax = "proto3";
package HelloService;
message String {
string value = 1;
}
service HelloService {
rpc Hello (String) returns (String);
rpc Channel (stream String) returns (stream String);
}
$ grpcurl -plaintext localhost:1234 list
>> HelloService.HelloService
>> grpc.reflection.v1alpha.ServerReflection
HelloService.HelloService是在protobuf文件定义的服务
。而ServerReflection服务则是reflection包注册的反射服务
。通过ServerReflection服务可以查询包括本身在内的全部gRPC服务信息。list
子命令还可以查看HelloService服务的方法列表:$ grpcurl -plaintext localhost:1234 list HelloService.HelloService
>> Channel
>> Hello
describe
子命令查看更详细的描述信息:$ grpcurl -plaintext localhost:1234 describe HelloService.HelloService
HelloService.HelloService is a service:
{
"name": "HelloService",
"method": [
{
"name": "Hello",
"inputType": ".HelloService.String",
"outputType": ".HelloService.String",
"options": {
}
},
{
"name": "Channel",
"inputType": ".HelloService.String",
"outputType": ".HelloService.String",
"options": {
},
"clientStreaming": true,
"serverStreaming": true
}
],
"options": {
}
}
$ grpcurl -plaintext localhost:1234 describe HelloService.String
HelloService.String is a message:
{
"name": "String",
"field": [
{
"name": "value",
"number": 1,
"label": "LABEL_OPTIONAL",
"type": "TYPE_STRING",
"options": {
},
"jsonName": "value"
}
],
"options": {
}
}
message String {
string value = 1;
}
-d
参数传入一个json字符串作为输入参数,调用的是HelloService服务的Hello方法:$ grpcurl -plaintext -d ‘{"value": "gopher"}‘ localhost:1234 HelloService.HelloService/Hello
{
"value": "hello:gopher"
}
-d
参数是@
则表示从标准输入读取json输入参数,这一般用于比较输入复杂的json数据,也可以用于测试流方法。$ grpcurl -plaintext -d @ localhost:1234 HelloService.HelloService/Channel
{"value": "gopher"}
{
"value": "hello:gopher"
}
{"value": "wasm"}
{
"value": "hello:wasm"
}
标签:ref name min 验证 启动 news target 复杂 url
原文地址:https://www.cnblogs.com/binHome/p/13068129.html