GoLang Hands-on with gRPC-Unary
In case you are landing here directly, it’s strongly suggested that you go and read through this for fundamentals.
Part #1.) :- Steps to demonstrate the basic setup :-
Step #1.) Here is the pre-work i.e. installing “vscode-proto3” plugin to our IDE. We are using Visual-Studio IDE :-
Step #2.) Let’s proceed now to install “clang-Format” plugin to our IDE. We are using Visual-Studio IDE :-
Step #3.) Let’s install certain perquisite :-
brew install clang-format
Step #4.) Let’s install another important perquisite :-
brew install protobuf
Step #4.) Let’s install grpc specific dependency :-
go get -u google.golang.org/grpc
Step #5.) Let’s install some important plugins specific dependency :-
brew install protoc-gen-go
brew install protoc-gen-go-grpc
Part #2.) :- We are now setting up the gRPC based server :-
Step #1.) Let’s first setup an empty project , by doing init :-
go mod init github.com/adityagoel123/grpc-aditya-learning
Post the above step is being executed, we would be able to see a file called “go.mod” :-
Step #2.) Next, let’s create a new folder “greet” and inside the same, let’s create three separate folders namely :-
Step #3.) Next, let’s create a our proto file, which would contain our Request/Response structures and Service. If you are new to protos, please read & clap my this blog.
Step #4.) Now, as shown in terminal in aforementioned screenshot, let’s now utilise our proto-compiler and auto-generate the code now :-
protoc -Igreet/proto --go_opt=module=github.com/adityagoel123/grpc-aditya-learning --go_out=. --go-grpc_opt=module=github.com/adityagoel123/grpc-aditya-learning --go-grpc_out=. greet/proto/*.proto
Step #5.) Next step for us is to write the code for our Server :-
- At line #18, we are listening on the default TCP port i.e. 50051. This is also a default port at which gRPC server usually runs.
- At line #27, we are launching a new-server through gRPC.
- At line #28, we are registering our gRPC server with the Service, so that, it can start serving the traffic on the same.
Step #6.) Next we execute the below command, in order to streamline all of our dependencies :-
go mod tidy
Note that, aforementioned command also generates a new file “go.sum”, where all dependencies would be clearly stated :-
Step #7.) Next we perform the build of our server, so as to generate executables from it :-
go build -o bin/greet/server ./greet/server
Step #8.) Last, but not the least, we can start the server as follows :-
Part #3.) :- We are now setting up the gRPC based client :-
Step #1.) Now proceed for setting up the gRPC client :-
- At line #13, we are setting-up gRPC-connection in an un-secured manner.
- At line #19, we are indicating our intent that, gRPC-connection shall be closed, once the entire processing of main function is done with.
Step #2.) We have now started registering the client and client have started invoking the functions @ server-side :-
Step #3.) Next, as shown above screenshot, we shall proceed for building our client :-
go build -o bin/greet/client ./greet/client
As a result of the aforementioned command execution, executable file shall also be created :-
Step #3.) Next, we shall proceed ahead to execute our client, which would eventually make a call to the Server’s gRPC function.
That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.