syntax="proto3";packagegrpc.examples.echo;optiongo_package="google.golang.org/grpc/examples/features/proto/echo";// EchoRequest is the request for echo.
messageEchoRequest{stringmessage=1;// EchoResponse is the response for echo.
messageEchoResponse{stringmessage=1;// Echo is the echo service.
serviceEcho{// UnaryEcho is unary echo.
rpcUnaryEcho(EchoRequest)returns(EchoResponse){}// ServerStreamingEcho is server side streaming.
rpcServerStreamingEcho(EchoRequest)returns(streamEchoResponse){}// ClientStreamingEcho is client side streaming.
rpcClientStreamingEcho(streamEchoRequest)returns(EchoResponse){}// BidirectionalStreamingEcho is bidi streaming.
rpcBidirectionalStreamingEcho(streamEchoRequest)returns(streamEchoResponse){}
% grpcurl -plaintext 127.0.0.1:50051 list
grpc.examples.echo.Echo
grpc.reflection.v1alpha.ServerReflection
help 的说明如下:
If ‘list’ is indicated, the symbol (if present) should be a fully-qualified
service name. If present, all methods of that service are listed. If not
present, all exposed services are listed, or all services defined in protosets
-plaintext :
Use plain-text HTTP/2 when connecting to server (no TLS)
即上面的命令我们通过 HTTP/2 的方式与 gRPC 服务端进行了一次交互,查询了其支持的所有的 service,其中就包括前面定义的 proto 内容 grpc.examples.echo.Echo 。
grpc.reflection.v1alpha.ServerReflection service 是由服务端使用了反射服务所生成。
亦或者我们可以直接加载 proto 文件的方式查看所支持的服务:
% grpcurl -import-path echo -proto echo.proto list
grpc.examples.echo.Echo
help 的说明如下:
-import-path value :
The path to a directory from which proto sources can be imported, for
use with -proto flags. Multiple import paths can be configured by
specifying multiple -import-path flags. Paths will be searched in the
order given. If no import paths are given, all files (including all
imports) must be provided as -proto flags, and grpcurl will attempt to
resolve all import statements from the set of file names given.
-proto value :
The name of a proto source file. Source files given will be used to
determine the RPC schema instead of querying for it from the remote
server via the gRPC reflection API. When set: the ‘list’ action lists
the services found in the given files and their imports (vs. those
exposed by the remote server), and the ‘describe’ action describes
symbols found in the given files. May specify more than one via multiple
-proto flags. Imports will be resolved using the given -import-path
flags. Multiple proto files can be specified by specifying multiple
-proto flags. It is an error to use both -protoset and -proto flags.
由于上面的命令不是使用服务端的反射服务进行查询,而是使用的 proto 源文件进行加载查询得出,故只有一项
grpc.examples.echo.Echo。
案例2 查看指定服务支持的方法
在前面使用 list 的基础上,我们还可以进一步查询指定服务所支持的方法:
% grpcurl -plaintext 127.0.0.1:50051 list grpc.examples.echo.Echo
grpc.examples.echo.Echo.BidirectionalStreamingEcho
grpc.examples.echo.Echo.ClientStreamingEcho
grpc.examples.echo.Echo.ServerStreamingEcho
grpc.examples.echo.Echo.UnaryEcho