gRPC
A traditional way in microservices, the way they communicate to each other is using HTTP / REST API. A service consumes an HTTP endpoint. It puts the payload as JSON and gets the response as JSON. It is slow and a little bit complicated. We need to transform the data to JSON and vice versa on every single request. The JSON is also heavy and sometimes not efficient.
With gRPC, you can send a request with an object then the response is an object too. The object is just like a variable of your programming language that you can set or get the value by a function. There's no need to decode or encode JSON. It is simple.
gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework. It was initially created by Google in 2015 to connect a large number of microservices. You can visit this article for more stories.
gRPC communicates using binary data over HTTP/2. It is more compact and efficient than REST API. Many benchmarks said that it is way faster than REST API such as this one.
It supports 11 programming languages and Android/Web platforms. Let me show you here a simple example of gRPC in GoLang and NodeJS.
Protocol Buffers
gRPC is using Protocol Buffers. We need to install the compiler first from https://grpc.io/docs/protoc-installation/. After that, we need to write the Protobuf. More references about the syntax can be found https://developers.google.com/protocol-buffers/docs/proto3.
|
|
We create the Protobuf for articles. Then, we compile it to the preferred programming language. In this example, I compile it to GoLang.
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
Install the protocol compiler plugins for Go using the commands above.
export PATH="$PATH:$(go env GOPATH)/bin"
Update the PATH
so that the protoc compiler can find the plugins.
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative article/article.proto
The command above will create an article.pb.go
and article_grpc.pb.go
.
Server
|
|
In this example, the article data is hardcoded. In a real project, you should get them from the database. PORT=50051
. Execute the server.
Client
|
|
SERVER="localhost:50051"
. Execute the client then we will get the result.
Check the terminal or Stdout for the request and response.
NodeJS
We can let GoLang as a server and NodeJS as a client.
npm install -g grpc-tools grpc_tools_node_protoc --js_out=import_style=commonjs,binary:node --grpc_out=grpc_js:node article/article.proto
Execute those commands above the compile the Protobuff for NodeJS.
|
|
The package.json
file. Make sure to install the node modules before coding the client.
|
|
That is the NodeJS / JavaScript version of the client. The intuition is the same as the GoLang one.
Closing
I tried to create another client in PHP but I failed to install some prerequisites. It also happened to the Web. I have no clue. Maybe gRPC is not ready yet, or maybe it is my mistake.
This is just an introduction to gRPC. I hope this simple explanation and tutorial can help you. I think it is maybe enough to inspire you to migrate from REST API to gRPC. There are some features of gRPC I didn't cover here. Maybe in the next articles.
References
- https://github.com/aristorinjuang/grpc
- https://grpc.io/docs/languages/go/quickstart/
- https://grpc.io/docs/languages/node/quickstart/