This example shows how you can use gRPCurl to interact with gRPC servers.
For full details on gRPCurl please see the offical website https://github.com/fullstorydev/grpcurl
To build the gRPC client and server interfaces, first install protoc:
sudo apt install protobuf-compiler
brew install protoc
Then install the Go gRPC plugin:
go get google.golang.org/grpc
Then run the build command:
protoc -I protos/ protos/currency.proto --go_out=plugins=grpc:protos/currency
The example service can be run using the following command
โ go run main.go
2020-05-22T16:34:40.139+0100 [INFO] Starting service on 0.0.0.0:9092
gRPC Curl can be installed from the following URL:
https://github.com/fullstorydev/grpcurl
or by running the following go install
command
go install github.com/fullstorydev/grpcurl/cmd/grpcurl
By default gRPCurl will attempt to use server reflection to determine the methods and types. To enable this reflection must be explictly implemented in the API. If reflection is not availabe, gRPCurl can also use the protos for the service.
grpcurl -import-path ./protos -proto service.proto list
To list services defined by the API.
grpcurl --plaintext localhost:9092 list
Currency
grpc.reflection.v1alpha.ServerReflection
Parameter --plaintext
tells gRPCurl to not use Insecure mode for the connection
-plaintext
Use plain-text HTTP/2 when connecting to server (no TLS).
grpcurl --plaintext localhost:9092 list Currency
Currency.GetRate
Currency.SubscribeRates
grpcurl --plaintext localhost:9092 describe Currency.GetRate
Currency.GetRate is a method:
rpc GetRate ( .RateRequest ) returns ( .RateResponse );
grpcurl --plaintext localhost:9092 describe Currency.SubscribeRates
Currency.GetRate is a method:
rpc GetRate ( .RateRequest ) returns ( .RateResponse );
Parameter --msg-template
displays a
-msg-template
When describing messages, show a template of input data.
grpcurl --plaintext --msg-template localhost:9092 describe .RateRequest
RateRequest is a message:
message RateRequest {
.Currencies Base = 1;
.Currencies Destination = 2;
}
Message template:
{
"Base": "EUR",
"Destination": "EUR"
}
RateRequest is a message:
grpcurl --plaintext -d '{"Base": "GBP", "Destination": "USD"}' localhost:9092 Currency/GetRate
{
"rate": 1.2229967868538965
}
It is also possible to read the data from stdin.
grpcurl --plaintext -d @ localhost:9092 Currency/GetRate <<EOM
{
"Base": "GBP",
"Destination": "USD"
}
EOM
The SubscribeRates service is a bi-directional streaming API, the following example will send a single message then block while receiving messages from the server.
grpcurl --plaintext -d @ localhost:9092 Currency/GetRate <<EOM
{
"Base": "GBP",
"Destination": "USD"
}
EOM
Client logs:
{
"rate": 12.12
}
{
"rate": 12.12
}
{
"rate": 12.12
}
Server Logs:
2020-05-22T21:20:07.572+0100 [INFO] Starting service on 0.0.0.0:9092
2020-05-22T21:20:12.053+0100 [INFO] SubscribeRates called
2020-05-22T21:20:12.053+0100 [INFO] Send message to client
2020-05-22T21:20:12.053+0100 [INFO] New message from client: base=EUR dest=EUR
2020-05-22T21:20:12.053+0100 [ERROR] Client write closed
2020-05-22T21:20:13.053+0100 [INFO] Send message to client
2020-05-22T21:20:14.054+0100 [INFO] Send message to client
To keep the client stream open you can use the same call as previous but this time ommit the message.
grpcurl --plaintext -d @ localhost:9092 Currency/GetRate
You can then paste the message payload to stdin and gRPCurl will send it to the server.