如何在express-node js中使用与cluster的粘性会话 [英] How to use sticky-session with cluster in express - node js

查看:185
本文介绍了如何在express-node js中使用与cluster的粘性会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题,我创建了一个集群的应用程序p>

但是我开始面对会话处理中的问题。如何在使用集群的快捷js中使用粘性会话。



我试图使用这个 npm模块。但这也导致了同样的局面。如何修复此会话问题。

  sticky(http.createServer(app).listen(app.get('port') ,function(){
console.log('Express server listen on port'+ app.get('port'));
}););


解决方案

最后找到的解决方案只是尝试这段代码。它保持粘性,并为其他客户使用所有cpus [process]。您可以使用以下代码使用快速集群粘性会话。您可以在这里获得粘性会话 https://github.com/indutny/sticky-session

  var http = require('http'); 
var cluster = require('cluster'); //只需要工作人员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);


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.

I was trying to use this npm module. But this resulted in the same situation. how to fix this session issue.

sticky(http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
}););

解决方案

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);

这篇关于如何在express-node js中使用与cluster的粘性会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆