Over time, each project grows and it becomes more difficult, longer and more expensive for the business to implement the new functionality into the existing monolith.
One of the solutions to this problem is the use of microservice architecture. For beginners or for those who first encounter this architecture, it can be difficult to understand where to start, what to do and what not to do.
In this article, the simplest microservice will be written on Nodejs & RabbitMQ, and the process of monolith migration to microservices is also shown.
Of course, you can not use RabbitMQ, there are other options for communication between microservices. The easiest one is HTTP, there is gRPC from Google.
I use RabbitMQ, because I consider it simple enough to start writing microservices, reliable and convenient in the sense that sending a message to a queue, you can be sure that the message will reach the microservice (even if it is turned off at the moment and then turned on ). Thanks to these advantages, you can write reliable microservices and use seamless deploy.
To begin with, we will implement a simple gateway that will accept HTTP requests while listening to a specific port.
We deploy RabbitMQ (through it our microservices and gateway will communicate):
$ docker run -d -p 5672:5672 rabbitmq
Initialize the project and install the micromq NPM package:
$ npm init -y $ npm i micromq -S
// Gateway micromq const Gateway = require('micromq/gateway'); // Gateway const app = new Gateway({ // , microservices: ['users'], // rabbitmq rabbit: { // rabbitmq (default: amqp://guest:guest@localhost:5672) url: process.env.RABBIT_URL, }, }); // /friends & /status GET app.get(['/friends', '/status'], async (req, res) => { // users await res.delegate('users'); }); // app.listen(process.env.PORT);
How it will work:
// MicroService micromq const MicroMQ = require('micromq'); // MicroService const app = new MicroMQ({ // ( , Gateway) name: 'users', // rabbitmq rabbit: { // rabbitmq (default: amqp://guest:guest@localhost:5672) url: process.env.RABBIT_URL, }, }); // /friends GET app.get('/friends', (req, res) => { // json res.json([ { id: 1, name: 'Mikhail Semin', }, { id: 2, name: 'Ivan Ivanov', }, ]); }); // /status GET app.get('/status', (req, res) => { // json res.json({ text: 'Thinking...', }); }); // app.start();
How it will work:
Suppose that we already have an application on express, and we want to start transferring it to microservices.
It looks like this:
const express = require('express'); const app = express(); app.get('/balance', (req, res) => { res.json({ amount: 500, }); }); app.get('/friends', (req, res) => { res.json([ { id: 1, name: 'Mikhail Semin', }, { id: 2, name: 'Ivan Ivanov', }, ]); }); app.get('/status', (req, res) => { res.json({ text: 'Thinking...', }); }); app.listen(process.env.PORT);
We want to take out from it 2 endpoints: / friends and / status. What do we need to do for this?
In the example above, when we created microservice users, we implemented two methods / friends and / status, which do the same thing that our monolith does.
In order to proxy requests to microservice from gateway, we will use the same package, connecting the middleware to our express application:
const express = require('express'); // Gateway micromq const Gateway = require('micromq/gateway'); const app = express(); // Gateway const gateway = new Gateway({ // ( , Gateway) name: 'users', // rabbitmq rabbit: { // rabbitmq (default: amqp://guest:guest@localhost:5672) url: process.env.RABBIT_URL, }, }); // middleware , app.use(gateway.middleware()); // , app.get('/balance', (req, res) => { res.json({ amount: 500, }); }); // /friends & /status GET app.get(['/friends', '/status'], async (req, res) => { // users // res.delegate middleware, await res.delegate('users'); }); // app.listen(process.env.PORT);
This works in the same way as in the example above, where we wrote the pure Gateway. In this example, the only difference is that the requests are received not by the Gateway, but by a monolith written in express.
I already told you this in the article “Learning to communicate between microservices on Node.js via RabbitMQ” .
Source: https://habr.com/ru/post/447074/
All Articles