Table of contents generated with markdown-toc
It's google RPC. :sweat_smile:
Hey, I know about google. But what is RPC?
- It stands for Remote Procedural Call.
- It uses a form of function call instead of the popular HTTP calls.
- It uses IDL(Interface Definition Lang.) as a form of contract.
- It's a google RPC.
- It uses HTTP/2 protocol.
- It uses Protocol Buffer (ProtoBuf) format for sending/receiving messages.
- Easy - It's a simple function call.
- Support - It is supported in a lot of languages.
- Fast - It uses HTTP/2 protocol.
- Well it's HTTP version 2. 🤦
- It's faster than HTTP 1 as it comes with the following features:
- Enable request and response multiplexing - Servers can send multiple messages in a single request.
- Header compression
- binary protocol - Send message in 0/1 format instead of text-based format
- It uses Protocol Buffer (ProtoBuf) as the IDL.
- Well REST is an architectural style.
- It defines protocols to talk between client and server.
- It uses HTTP/1 protocol.
- It uses JSON format for sending/receiving messages.
- Well it's simple, easy to use and there are many tools to test it easily.
- Issues:
- Sometimes the payload becomes huge and it decreases the performance.
- It's unary, that is we can send one request at a time.
- REST uses HTTP/1 protocol which does three-way handshaking for the first message, making it slower.
Note: It's best to use REST when communicating between browser and back-end. gRPC is best for inter-microservice communication.
-
Create go.mod file go mod init github.com/ughosh/grcp-learn
-
Create a file
hello.proto
-
In add
hello.proto
- Add the version of proto file
syntax = "proto3";
- Add folder name where we want to save generated files
option go_package = "github.com/ughosh/grcp-learn/invoicer";
- Define service
service Invoicer { rpc Hello(HelloRequest) returns (HelloResponse); }
- Define request and response as message
message HelloRequest{ string name = 1; } message HelloResponse { string msg = 1; }
- Add the version of proto file
-
Create a folder name
hello
-
Run the command to generate proto files based on the description given in our
hello.proto
fileprotoc \ --go_out=hello \ --go_opt=paths=source_relative \ --go-grpc_out=hello \ --go-grpc_opt=paths=source_relative \ hello.proto
-
If error occur, try running
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@lates
-
The generate file will be showing some error, in case the lib is not present. Do
go mod tidy
after this to clean-upgo.mod
filego get -u google.golang.org/grpc
-
If we make any changes in our
hello.proto
file, we have to run command #5. To save our time, we can save the entire command in Makefile. Create aMakefile
and add the command
generate_grpc_code_hello:
protoc \
--go_out=hello \
--go_opt=paths=source_relative \
--go-grpc_out=hello \
--go-grpc_opt=paths=source_relative \
hello.proto
Now, if we want to do any updates, run make generate_grpc_code_hello
Add main.go file and run that file using go run main.go
This will start our server which is listening to port 8080
To test it, we will be using grpcurl (https://github.com/fullstorydev/grpcurl) Run the command in another terminal
grpcurl -plaintext -d '{"name": "Test"}' localhost:8080 Hello.Hello
You will output like
{
"msg": "Hello Test"
}
Congratulations! You created your first gRPC call in Go lang 🎉