How to use sticky-session with cluster in express - node js

前端 未结 2 1407
不知归路
不知归路 2021-02-20 05:30

I created a cluster depending app with reference to this question

But I started facing issues in session handling. how to use sticky-session in express js with cluster.<

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 05:38

    Finally found solution just try this code. Its maintain sticky as well as it uses all the cpus [ process ] for other clients. You can use express cluster sticky session using following code. You can get sticky-session here https://github.com/indutny/sticky-session

    var http = require('http');
    var cluster = require('cluster'); // Only required if you want the worker id
    var sticky = require('sticky-session');
    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
        console.log('worker: ' + cluster.worker.id);
        res.send('Hello World!');
    });
    
    
    var server = http.createServer(app);
        sticky.listen(server,3000);
    

提交回复
热议问题