12

有人知道如何在 Windows 上运行 Node Cluster 吗?我在网上找不到任何文章,似乎无法解决这个问题:

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)

而且代码...

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

3 回答 3

2

v0.11.14(适用于 Linux 和 OSX)中添加了对 UDP 集群的支持。检查node.js master上的文件,上面写着“windows目前不支持dgram集群”

于 2016-09-04T16:23:13.360 回答
1

在当前节点 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");
  });
}

这将在同一端口上创建集群。

于 2016-09-07T12:26:58.703 回答
1

因此,为了在 Windows 上将 UDP 与 Node 集群一起使用,您必须像这样调用 server.bind:

server.bind({port: 1900, exclusive: true}, function () {
        console.log('PORT BIND SUCCESS');
        server.setBroadcast(true);
        server.setMulticastTTL(128);
        server.addMembership(multicastAddress, myIp);
    });

关键部分是将对象 {port: PORT, exclusive: true} 传递给绑定函数。我在这里找到了答案:https ://github.com/misterdjules/node/commit/1a87a95d3d7ccc67fd74145c6f6714186e56f571

于 2017-05-25T16:10:56.287 回答