Anyone know how to run Node Cluster on windows? I haven't been able to find any articles on the web and cannot seem to solve this problem:
有人知道如何在windows上运行节点集群吗?我在网上找不到任何文章,似乎也解决不了这个问题:
events.js:160
throw er; // Unhandled 'error' event
^
Error: write ENOTSUP
at exports._errnoException (util.js:1007:11)
at ChildProcess.target._send (internal/child_process.js:634:20)
at ChildProcess.target.send (internal/child_process.js:521:19)
at sendHelper (cluster.js:751:15)
at send (cluster.js:534:12)
at cluster.js:509:7
at SharedHandle.add (cluster.js:99:3)
at queryServer (cluster.js:501:12)
at Worker.onmessage (cluster.js:449:7)
at ChildProcess.<anonymous> (cluster.js:765:8)
And the code...
和代码…
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', (worker) => {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
});
} else {
console.log('else part ');
openPort();
}
function openPort() {
let server = dgram.createSocket('udp4');
server.bind(port, host);
server.on('message', processMessage);
}
3 个解决方案
#1
1
Support for UDP clustering was added in v0.11.14 (for Linux and OSX). Check file on node.js master, which says "dgram clustering is currently not supported on windows"
v0.11.14中添加了对UDP集群的支持(用于Linux和OSX)。检查文件节点。js master,它说“目前windows不支持dgram cluster”
#2
1
In the current node js version I am using below code to create cluster on windows.
在当前的node js版本中,我使用下面的代码在windows上创建集群。
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log("worker ${worker.process.pid} died");
cluster.fork();
});
} else {
var express = require('express');
var http = require('http');
// init app
var app = express();
function createServer(app) {
return http.createServer(app);
}
app.locals.server = createServer(app);
app.locals.server.listen(port, function() {
console.info("server online");
});
}
This will create clusters on same port.
这将在同一个端口上创建集群。
#3
0
So, in order to use UDP with Node cluster on Windows, you have to call server.bind like this:
因此,要使用UDP和Windows上的节点集群,您必须调用服务器。绑定是这样的:
server.bind({port: 1900, exclusive: true}, function () {
console.log('PORT BIND SUCCESS');
server.setBroadcast(true);
server.setMulticastTTL(128);
server.addMembership(multicastAddress, myIp);
});
The key part is to pass in the object {port: PORT, exclusive: true} to the bind function. I found the answer here: https://github.com/misterdjules/node/commit/1a87a95d3d7ccc67fd74145c6f6714186e56f571
关键部分是将对象{port: port, exclusive: true}传递给bind函数。我在这里找到了答案:https://github.com/misterdjules/node/commit/1a87a95d3d7ccc67f74145c6f67146f67146e56f571
#1
1
Support for UDP clustering was added in v0.11.14 (for Linux and OSX). Check file on node.js master, which says "dgram clustering is currently not supported on windows"
v0.11.14中添加了对UDP集群的支持(用于Linux和OSX)。检查文件节点。js master,它说“目前windows不支持dgram cluster”
#2
1
In the current node js version I am using below code to create cluster on windows.
在当前的node js版本中,我使用下面的代码在windows上创建集群。
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log("worker ${worker.process.pid} died");
cluster.fork();
});
} else {
var express = require('express');
var http = require('http');
// init app
var app = express();
function createServer(app) {
return http.createServer(app);
}
app.locals.server = createServer(app);
app.locals.server.listen(port, function() {
console.info("server online");
});
}
This will create clusters on same port.
这将在同一个端口上创建集群。
#3
0
So, in order to use UDP with Node cluster on Windows, you have to call server.bind like this:
因此,要使用UDP和Windows上的节点集群,您必须调用服务器。绑定是这样的:
server.bind({port: 1900, exclusive: true}, function () {
console.log('PORT BIND SUCCESS');
server.setBroadcast(true);
server.setMulticastTTL(128);
server.addMembership(multicastAddress, myIp);
});
The key part is to pass in the object {port: PORT, exclusive: true} to the bind function. I found the answer here: https://github.com/misterdjules/node/commit/1a87a95d3d7ccc67fd74145c6f6714186e56f571
关键部分是将对象{port: port, exclusive: true}传递给bind函数。我在这里找到了答案:https://github.com/misterdjules/node/commit/1a87a95d3d7ccc67f74145c6f67146f67146e56f571