Example implementation of gRPC bwtween the internal microservices communications. The original request from the front-end is still using a Restful.
Note: This is just an example project, for those who wants to learn about gRPC on NodeJs.
The illustration below is about how this project behave, so you may get the idea about how it's work under the hood.
+----------------------+
|DB| |
+ +--+ +--------------+ +------+
| | | Products | | |
| | +--------------+ +<---+ |
| | | | |
| +----------------------+ | | Queries
| | |
| | |
[Restful] | |
+-----------------+ HTTP Req +----------------+ RPC Calls +------------+-v+
| +------+----->+ +------------> |
| External Client | | | Client Service | | Server App |
| <------+------+ <------------+ |
+-----------------+ HTTP Resp +----------------+ +---------------+
|
|
|
|
|
+
-
HTTP Request coming from the front-end or client via CURL or another RESTful client (e.g. Insomnia, Postman, etc)
-
Client Service requesting data through Rpc to the Server App
-
Server App then doing a DB query and returning the result back
-
Client Service got the result and return back to the original front-end or client.
-
Clone this project
-
There are 3 subdirectories;
server
is a server of gRPC;client
is a server of client which provide a restful service to the user, andprotos
holds the.proto
file for product. -
Go to
server
directory and install dependencies via
$ npm i
-
Do the same step on the
client
directory -
Create a new Postgres database called
grpc_products
(you may configure the username / password of the DB later onserver/knexfile.js
) -
Running the migration from the server directory
$ npm run migrate
- Also the run the seeds
$ npm run seed
Go to server
directory and run npm start
. If everything Ok you will see a message like
> gRPC server running at http://127.0.0.1:1831
go to client
directory and run npm start
. You should see a message like this
> Server run at 3000
$ curl http://127.0.0.1:3000/api/products
{
"products": [
{
"id": 1,
"name": "pencil",
"price": "1.99"
},
{
"id": 2,
"name": "pen",
"price": "2.99"
}
]
}
$ curl http://127.0.0.1:3000/api/products/1
{
"id": 1,
"name": "pencil",
"price": "1.99"
}
$ -X POST -d '{"name": "lamp", "price": "18.8"}' -H "Content-Type: application/json" http://127.0.0.1:3000/api/products
{"status":"Created"}
$ curl -X PUT -d '{"name": "Spoon", "price": "34.53"}' \
-H 'Content-Type: application/json' http://127.0.0.1:3000/api/products/3
{"status":"Updated"}
$ curl -X DELETE http://127.0.0.1:3000/api/products/3
{"status":"Deleted"}
MIT (more)