node.js中的集群不起作用.只有一个工人总是在响应
clustering in node.js is not working. Only one worker is always responding
我在node.js中练习集群,我有一个两核CPU。我创建了两个worker,每个worker运行一个简单的http服务器。服务器响应回调将阻塞5秒,以便将下一个请求发送给其他worker。为了验证worker是否并行工作,我在Firefox中打开了多个选项卡,并对每个选项卡进行了刷新。问题总是(99%)只有一个worker响应通过刷新选项卡发出的请求。一个worker只处理一个请求,所有其他请求都被阻塞,直到该worker完成。我的代码在这里代码:
var cluster = require('cluster');
var http = require('http');
if (cluster.isMaster) {
var cpus = require('os').cpus().length;
console.log('No of cpus:' + cpus);
console.log(require('os').cpus());
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('fork', function(worker) {
console.log('worker:' + worker.id + " is forked");
});
cluster.on('online', function(worker) {
console.log('worker:' + worker.id + " is online");
});
cluster.on('listening', function(worker) {
console.log('worker:' + worker.id + " is listening");
});
cluster.on('disconnect', function(worker) {
console.log('worker:' + worker.id + " is disconnected");
});
cluster.on('exit', function(worker) {
console.log('worker:' + worker.id + " is dead");
});
} else {
http.createServer(function(req, res) {
console.log('worker:' + cluster.worker.id + " going to send response ");
res.writeHead(200);
res.end("hello world. worker: " + cluster.worker.id);
var stop = new Date().getTime();
while (new Date().getTime() < stop + 5000) {;
}
}).listen(8000);
}
输出:20 Aug 00:36:11 - [nodemon] restarting due to changes...
20 Aug 00:36:12 - [nodemon] starting `node cluster.js`
No of cpus:2
[ { model: 'Intel(R) Core(TM)2 Duo CPU E4500 @ 2.20GHz',
speed: 2200,
times: { user: 2264671, nice: 0, sys: 698343, idle: 5965109, irq: 98812 } },
{ model: 'Intel(R) Core(TM)2 Duo CPU E4500 @ 2.20GHz',
speed: 2200,
times: { user: 2466000, nice: 0, sys: 502562, idle: 5959203, irq: 4609 } } ]
worker:1 is forked
worker:2 is forked
worker:2 is online
worker:1 is online
worker:2 is listening
worker:1 is listening
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
worker:1 going to send response
我注意到一件事。如果我强制重新加载(ctrl+f5)选项卡,那么两个工人一个接一个地响应。输出:worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
worker:1 going to send response
worker:2 going to send response
我很困惑,这里发生了什么正常刷新(f5)和强制重新加载(ctrl + f5)。帮我弄明白…!
表面节点集群上的TIL似乎并没有做到它所说的那样(至少对我来说是这样)。
我在相同的情况下,你是,观察相同的工作进程总是被给予一个请求,当我从浏览器同步生成它们。我的一个同事用fiddler一次重放了20个请求。当所有这些请求都以非常快的速度到达服务器时,比集群管理器将它们传递给工作器的速度还要快(我猜),那么您将看到额外的工作器被请求调用。
似乎一旦管理器把请求交给一个worker,它就不知道/不关心worker阻塞了。它只知道管道中只有一个请求,并且不需要将该请求提供给第一个worker以外的任何人,因为就管理器所知,他是可用的。
响应代码中的while
循环可能会导致一些严重的问题。如果您想模拟长时间运行的请求,则应该在那里使用setTimeout
。
为你的worker试试这个:
http.createServer(function(req, res) {
console.log('worker:' + cluster.worker.id + " going to send response ");
setTimeout(function() {
res.writeHead(200);
res.end("hello world. worker: " + cluster.worker.id);
}, 5000);
}).listen(8000);
话虽这么说,@dandavis在评论中说的是真的:cluster
不做循环负载平衡,所以,只要Worker 1
对请求可用,它就会处理它们。像我建议的那样使用setTimeout
实际上会使你的工人更多的可用来处理请求,所以很可能你只会看到Worker 1
处理请求,如果你只是从浏览器手动点击服务器。您可能需要某种负载测试脚本来查看两个worker处理请求。
至于你看到的F5和CTRL-F5之间的差异,我最好的猜测是,你的浏览器是保持连接到服务器活着,所以,当你只是使用F5,它使用相同的连接,这将始终去同一个工作者。当您使用CTRL-F5时,它实际上关闭了它之前拥有的连接,因此可以在下一个请求中连接到任何一个worker。
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在只能有一个asp.net表单的主页上从asp.net页面中的javascript中获取值
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- 有可能在来自链接的警报中有一个值吗
- 是否<asp:文本框>有一个onFocusLost事件
- 让谷歌数据图表有一个24小时x轴
- PayPal按钮是否有一个简单的选项,其中金额基于一些用户输入
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 当知道同一hiearch中至少有一个元素时,遍历到元素.结构使用jquery
- 使用来自不同异步函数的响应创建一个json对象
- PNG图像被覆盖,但每个图像都有一个链接可以更改,每次一个-Javascript/jQuery/CSS
- JQuery是否有一个“;移动“;作用或者有没有一种更紧凑的方法来做到这一点
- Regex或javascript每X个连续单词中有一个单词
- 有一个响应式按钮
- 我有一个API,我想从api响应中获取数据
- 我有一个这样的JSON响应,由于某种原因,我在访问该值时遇到了麻烦
- 如何在PHP中有一个动作响应javascript
- 我怎么能有一个响应高度与绝对位置子元素
- 当进行多个ajax请求时,是否有一个“良好的模式”?用于跟踪响应与哪些数据一致