I wrote a GRPC server with basic features to support my aboratories, written articles, and videos it provides a sample of unary call, client streaming, server streaming and bidirectional streaming.
All the samples are "Hello world like" but once you have the project structure is possible to extend it to any purpose.
And I want to share it here. the image src code is available in my github
How to use the image:
docker run --rm --name grpc -p 50051:50051 getjv/go-grpc-server
Exploring server calls with grpcURL
gRPCurl is a super handy tool, and the main commands are:
-
grpcurl -plaintext [::]:50051 list
– lists the available services. -
grpcurl -plaintext [::]:50051 describe <ServiceName>
– provides more information about the available service. -
grpcurl -plaintext -d '<JSON-Payload>' [::]:50051 <ServiceName.MethodName>
– makes a direct call to a method with a payload.
Use samples:
Hello sample: it's a unary call with single request single response.
grpcurl -plaintext -d '{"name": "jhonatan"}' [::]:50051 helloworld.Greeter.SayHello
Server Streaming sample: it's a server streaming sample with 1 request and 10 messages from server
grpcurl -d '{"name": "Cliente"}' -plaintext localhost:50051 helloworld.Greeter/StreamGreetings
Client Streaming sample: it's a client streaming sample with 10 client requests and 1 message from server
Here the steps are a bit different:
- Open the channel with:
grpcurl -plaintext -d @ localhost:50051 helloworld.Greeter/EchoGreetings
- the terminal will wait the input give the objects bellow and press
enter
:
{"name": "Alice"}
{"name": "Bob"}
{"name": "Charlie"}
- Now press
CTRL+D
to notify all was sent. Server will send you the final confirmation.
Bi-directional streaming: this is the most advanced sample an open channel to both sides keeping interacting
to test execute:
- Open the channel with:
grpcurl -plaintext -d @ localhost:50051 helloworld.Greeter/OpenGreetings
- the terminal will wait the input give the objects bellow and press
enter
:
{"name": "Alice"}
- After each
enter
Server will send you a confirmation. - Now press
CTRL+D
to notify you are done.
Top comments (0)