6
votes

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?
2

2 Answers

8
votes

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.

4
votes

Actually I think you are missing the concept of microservice architecture. Your services must be independent and if they need to communicate with each other they must use a service discovery mechanism that will return a healthy instance of that service. Another pattern of microservices architecture is that every single service must have an endpoint (/health) that returns the health status of the service, having this your service discovery can check if that instance is healthy and return it as a healthy instance..

Microservices is not about technology it's about the concept and implementing the right patterns. Otherwise you will have a chaos architecture :D

If you want to understande the concepts I really recommend this book: http://shop.oreilly.com/product/0636920033158.do