Dieses Blog durchsuchen

Sonntag, 25. September 2016

node.js: create and scale a simple rest microservice cluster with cluster.js and express to your servers cpu


Everyone speaks about microservices. Today we want to create our first node.js microservice based on an article in the current phpmagazin.

On top we want to add this microservice to a servicecluster, which can scale perfect to your hardware and doubles your number of request per second to 10000 hits / sec

Prerequisits

We need some things installed on the local machine.
  • node.js and npm installed

If you have installed the node and npm we can proceed

 

Create a npm project

-npm init 
This will open a promt to fill your projectdata

 

Install needed node libraries

We need express.js as a webserver framework, SQLite.js as the database-layer and body-parser to process responses

-npm install --save express body-parser cluster


Create index file

Our index.js references the libraries, implements the router and starts the app on port 8085

Here is the index.js

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({extened:false}));

require('./lib/router')(app);

app.listen(8085);



Create your routings

Our service implements 6 routes:

URL HTTP Method Description
/timetrack/ GET READ all entries
/timetrack/user/:id GET READ a user entry
/timetrack/id/:id GET READ an entry
/timetrack/ POST add an entry
/timetrack/:id PUT update an entry
/timetrack/:id DELETE delete an entry

Lets create this routes in the folder "lib" (create this folder) by adding a file "routes.js"

This simple router just takes the requests and returns a static string, so that we can see the router working

lib/router.js
module.exports = function (app)
{
    app.get('/timetrack/id/:id', (req, res)=>
    {
        res.send('Returning a specific item');
    });

    app.get('/timetrack', (req, res)=>
    {
        res.send('Returning all items');
    });

    app.post('/timetrack', (req, res)=>
    {
        res.send('add an item');
    });

    app.put('/timetrack/:id', (req, res)=>
    {
        res.send('updating an item');
    });

    app.delete('/timetrack/:id', (req, res)=>
    {
        res.send('delete a specific item');
    });
};



Create your cluster

Cluster.js gives us the possibility to create 1 nodeprocess per cpu. So you can scale your node app with factor 8 on a octacore i7

This will double the number of request per second.

cluster.js
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  console.log('#######################################');
  console.log('found:' + numCPUs + ' cpus on this server:');
  console.log('#######################################');

  for (var i = 0; i < numCPUs; i++) {
    
    console.log('starting cluster instance on cpu:' + i);
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {

    //change this line to Your Node.js app entry point.
    require("./index.js");
}

Test your service

node index.js

Open a second terminal and run

ab -n 10000 -c 1000 http://localhost:8085 


Now we want to scale our service:
Stop your app and restart it with

node cluster.js
Restest the service and compare the results
ab -n 10000 -c 1000 http://localhost:8085 

As you can see, in the first test we got 5448 Request per second. On the second test with cluster.js we have 10756 Requste per second. This is more then double the perfomance.

Realy awsome

Keine Kommentare:

Kommentar veröffentlichen