开始学习节点集群出现错误

started learning node cluster getting error

我尝试将此作为我的第一个代码,但出现错误

   const cluster = require('cluster');
    const http = require('http');
    const numCPUs = 4;

    if (cluster.isMaster) {
      // Fork workers.
      for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
      }
       cluster.on('online', function(worker) {
            console.log('Worker ' + worker.process.pid + ' is online');
        });

      cluster.on('exit', (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
      });

    } else {
      // Workers can share any TCP connection
      // In this case it is an HTTP server
      http.createServer((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
      }).listen(8000);
    }

这是我得到的错误:-

工人11056在线 工人11057在线 工人11058在线 工人11059在线 events.js:141 扔呃; // 未处理的 'error' 事件 ^

错误:绑定 EADDRINUSE null:8000 在 Object.exports._errnoException (util.js:870:11) 在 exports._exceptionWithHostPort (util.js:893:20) 在 cb (net.js:1302:16) 在 rr (cluster.js:594:14) 在工人。 (cluster.js:564:9) 在过程中。 (cluster.js:714:8) 在 emitTwo (events.js:92:20) 在 process.emit (events.js:172:7) 在 handleMessage (internal/child_process.js:689:10) 在 Pipe.channel.onread (internal/child_process.js:440:11) events.js:141 扔呃; // 未处理的 'error' 事件 ^

错误:绑定 EADDRINUSE null:8000 在 Object.exports._errnoException (util.js:870:11) 在 exports._exceptionWithHostPort (util.js:893:20) 在 cb (net.js:1302:16) 在 rr (cluster.js:594:14) 在工人。 (cluster.js:564:9) 在过程中。 (cluster.js:714:8) 在 emitTwo (events.js:92:20) 在 process.emit (events.js:172:7) 在 handleMessage (internal/child_process.js:689:10) 在 Pipe.channel.onread (internal/child_process.js:440:11) events.js:141 扔呃; // 未处理的 'error' 事件 ^

错误:绑定 EADDRINUSE null:8000 在 Object.exports._errnoException (util.js:870:11) 在 exports._exceptionWithHostPort (util.js:893:20) 在 cb (net.js:1302:16) 在 rr (cluster.js:594:14) 在工人。 (cluster.js:564:9) 在过程中。 (cluster.js:714:8) 在 emitTwo (events.js:92:20) 在 process.emit (events.js:172:7) 在 handleMessage (internal/child_process.js:689:10) 在 Pipe.channel.onread (internal/child_process.js:440:11) 工人 11056 死亡 工人 11057 死亡 工人 11058 死亡 events.js:141 扔呃; // 未处理的 'error' 事件 ^

错误:绑定 EADDRINUSE null:8000 在 Object.exports._errnoException (util.js:870:11) 在 exports._exceptionWithHostPort (util.js:893:20) 在 cb (net.js:1302:16) 在 rr (cluster.js:594:14) 在工人。 (cluster.js:564:9) 在过程中。 (cluster.js:714:8) 在 emitTwo (events.js:92:20) 在 process.emit (events.js:172:7) 在 handleMessage (internal/child_process.js:689:10) 在 Pipe.channel.onread (internal/child_process.js:440:11) 工人 11059 死亡

EADDRINUSE 表示其他内容已绑定到您尝试使用的 port/IP 地址组合。

要检查的事项:

  • 您是否已经有一些其他进程绑定到端口 8000?如果是这样,也许可以在此处更改代码中的端口号。

  • 您是否有此代码的另一个实例 运行? (在类 UNIX 操作系统上,可能使用 ps 和适当的选项来找到它。)如果是这样,终止另一个实例。 (在类似 UNIX 的操作系统上,您可以使用 killpkill 以及适当的选项。)

您是在项目的其他地方启动服务器吗?

尝试将您创建的新工作程序拆分到一个单独的文件中。

worker.js

import express from 'express';
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);

并将服务器设置指向此文件

setup.js

const cluster = require('cluster');
const http = require('http');
const numCPUs = 4;

cluster.setupMaster({
  exec: __dirname + '/worker.js'
});

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
     cluster.fork();
  }
  cluster.on('online', function(worker) {
     console.log('Worker ' + worker.process.pid + ' is online');
   });

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
   });
   return 1;
}