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进程之间事件通信没反应了