繁体   English   中英

pm2 集群模式下的 express 服务器端口配置问题

[英]express server port configuration issue with pm2 cluster mode

问题:我们在集群模式下启动 pm2,pm2 启动与 cpu 核数一样多的进程,pm2 也尝试启动与 cpu 核数一样多的节点服务器,但这里问题是它无法启动尽可能多的服务器,因为它们所有尝试并在 3000 的相同端口上启动,该端口已被第一个节点服务器占用

我们使用 nginx 并将其代理到 3000 端口。

我们在集群模式下使用 pm2,配置如下:

{
  "apps" : [{
    "script"    : "npm",
    "instances" : "max",
    "cwd":"/home/nginx/my-pwa" ,
    "args" : "run start:server:prod",
    "exec_mode" : "cluster",
    "wait_ready": true,
    "kill_timeout" : 4000,
    "watch" : true
  }]
}

运行 start:server:prod 是我们启动服务器的脚本

我们的快递服务器:

var app = require('../src/app');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

const http = require('http');
server = http.createServer(app);
server.listen(port));
server.on('error', onError);
server.on('listening', onListening);

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

process.on('message', function(msg) {
  if (msg == 'shutdown') {
    server.close();
    process.exit(0);  
  }
});

// Listening logic
function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
  console.log("Server started on ", bind);
  process.send('ready');
}

请帮忙,这是关键任务!

问题是pm2与npm不能很好地配合。 它无法使用npm脚本启动两个节点服务器。 正确的方法是使用节点

我以前的配置:

{
  "apps" : [{
    "script"    : "npm",
    "instances" : "max",
    "cwd":"/home/nginx/my-pwa" ,
    "args" : "run start:server:prod",
    "exec_mode" : "cluster",
    "wait_ready": true,
    "kill_timeout" : 4000,
    "watch" : true
  }]
}

我的新配置:

{
  "apps" : [{
    "script"    : "./server/bin/www",
    "instances" : "max",
    "exec_mode" : "cluster",
    "cwd":"/home/nginx/my-pwa" ,
    "env": {"NODE_ENV" : "production"},
    "name" : "my-pwa"
  }]
}

你可以看到我不再使用“脚本”:“npm”。 ./server/bin/www包含我的express服务器,pm2将使用node执行。 现在pm2能够自动处理群集。 那么输出应该如何看起来像现在一样? 它使一个上帝deamon管理根据你的cpu核心数量的工作服务器实例。 我们服务器上的输出:(它有2个内核)

nginx     1363     1  0 05:20 ?        00:00:03 PM2 v2.10.1: God Daemon (/home/nginx/.pm2)
nginx     1373  1363  0 05:20 ?        00:00:09 node /home/nginx/my-pwa/server/bin/www
nginx     1374  1363  0 05:20 ?        00:00:09 node /home/nginx/my-pwa/server/bin/www

此外,现在pm2重载工作正常。 当我看到重新加载日志时,pm2首先启动新工作人员,然后关闭旧工作人员。

对我来说,将exec_mode更改为cluster_mode工作。

我的ecosystem.config.js改为这样的东西

apps: [
  {
    script: 'server.js',
    instances: 'max',
    exec_mode: 'cluster_mode' // not 'fork' or 'cluster'
  }
]

我的问题是脚本需要命名为ecosystem.config.js .config.js,其他任何东西都不会在集群模式下启动。 它将以分叉模式启动,但任何其他配置都需要文件名。

我不认为你可以使用pm2集群模式和npm脚本。 首先使用pm2 list检查进程是否实际处于群集模式。 如果不是,请尝试修改配置以直接调用节点脚本而不是调用npm脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM