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-formatStep #4.) Let’s install another important perquisite :-
brew install protobufStep #4.) Let’s install grpc specific dependency :-
go get -u google.golang.org/grpcStep #5.) Let’s install some important plugins specific dependency :-
brew install protoc-gen-go
brew install protoc-gen-go-grpcPart #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-learningPost 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 :-
- client
- server
- proto
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/*.protoStep #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 tidyNote 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/serverStep #8.) Last, but not the least, we can start the server as follows :-
./bin/greet/serverPart #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/clientAs 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.
./bin/greet/clientThat’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.
References :-
- https://adityagoel123.medium.com/in-conversation-with-googles-grpc-part1-f33521f313f
- https://adityagoel123.medium.com/in-conversation-with-protocolbuffers-5881ebd2b885
- https://adityagoel123.medium.com/in-conversation-with-http-2-c27c5c26abc0
- https://adityagoel123.medium.com/developing-microservices-apis-using-grpc-6ee97805861
- https://stackoverflow.com/questions/60578892/protoc-gen-go-grpc-program-not-found-or-is-not-executable
- https://stackoverflow.com/questions/57700860/protoc-gen-go-program-not-found-or-is-not-executable
- https://github.com/tut195/grpc-java-course-Stephane-Maarek
- https://github.com/grpc/grpc-go/issues/3794
- https://stackoverflow.com/questions/70482508/grpc-withinsecure-is-deprecated-use-insecure-newcredentials-instead
- https://github.com/grpc/grpc-go/blob/master/examples/features/xds/server/main.go#L34