cluster用了 Sticky session以后进程之间事件通信失效了
照着 https://github.com/indutny/sticky-session 这上面的例子写个测试代码
var cluster = require('cluster');
var sticky = require('sticky-session');
const PORT=5000;
var totalReqs = 0;
var localReq = 0;
var server = require('http').createServer(function(req, res) {
res.end(process.pid+ ' worker: ' + cluster.worker.id);
process.send(" worker " + process.pid + " local is " + localReq + " sum is " + totalReqs);
});
if (!sticky.listen(server, PORT)) {
// Master code
if (cluster.isMaster) {
Object.keys(cluster.workers).forEach(function(i){
cluster.workers[i].on('message', function(msg) {
totalReqs++;
console.log(totalReqs);
});
});
}
server.once('listening', function() {
console.log('server started on port',PORT);
});
} else {
// Worker code
console.log(process.pid);
}
sticky的效果实现了,但是master/woker进程之间事件通信没反应了
4 回复
这个session是用来干嘛的?如果是配置相关的,也不必如此
@yakczh sid用redis去存,而非在cluster上做手脚,曾经有兄弟给我打过电话问这个
我记得express的connect-session中间件支持RedisStorage存储的,默认是MemoryStorage。我同意狼叔的建议。使用redis实现会话数据持久化与多进程间的会话数据共享 。