Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding microservices using Express.js and docker

I am new to node.js and docker as well as the microservices architecture. I am trying to understand what microservices architecture actually is and theoretically I do understand what microservices arch is.Please see the following implementation This is the index.js file:

var express = require("express"); 
var app = express();
var service1 = require("./service1");
var service2 = require("./service2");
app.use("/serviceonerequest",service1);
app.use("/servicetwo",service2);
app.listen(3000,function(){
    console.log("listening on port 3000");
});

The file service1:

var express = require("express");
var router = express.Router();
router.use(express.json());
router.get("/",(req,res)=>{
    //perform some service here
    res.send("in the get method of service 1");
    res.end();
});        
router.post("/letsPost",(req,res)=>{
    res.send(req.body);
    res.end("in post method here");
})
module.exports = router;

The file service2:

var express = require("express");
var router = express.Router();       
router.use(express.json());
router.get("/",(req,res)=>{
    //perform some service here
    res.end("in the GET method for service 2");
});    
router.post("/postservice2",(req,res)=>{
    res.send(req.body);
});    
module.exports = router;
  1. Does the above qualifies as 'micro service architecture'?Since there are two services and they can be accessed through the 'api-gateway' index.js?
  2. I have read the basic tutorial of Docker.Is it possible to have the above three "modules" in separate containers?
  3. If the above does not qualify as a microservice what should be done to convert the above sample into microservices?
like image 980
user1261913 Avatar asked Apr 02 '18 14:04

user1261913


1 Answers

This does not really qualify as a microservice architecture.

The whole code you provided is small enough to be considered one single microservice (containing two routes), but this is not an example of a microservice architecture.

According to this definition;

"Microservices are small, autonomous services that work together"
Building Microservices <-- tip: you should read this book

Both service1 and service2 to be considered a microservice should be autonomous, what is not happening when you place them together in the same express app. For example; you cant restart one without not-affecting the other. You cant upgrade version of service1 without also having to deploy service2. They are not distributed in the sense that they can leave in separate machines.

like image 191
Renato Gama Avatar answered Sep 22 '22 11:09

Renato Gama