简体   繁体   English

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

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

Problem: We start pm2 in cluster mode, and pm2 starts as many processes as there are cpu cores, pm2 also tries to start as many node servers as there are cpu cores but the problem here is that it fails to start as many servers because they all try and start on the same port that is 3000, which already gets occupied by the first node server问题:我们在集群模式下启动 pm2,pm2 启动与 cpu 核数一样多的进程,pm2 也尝试启动与 cpu 核数一样多的节点服务器,但这里问题是它无法启动尽可能多的服务器,因为它们所有尝试并在 3000 的相同端口上启动,该端口已被第一个节点服务器占用

We using nginx and proxy it to 3000 port.我们使用 nginx 并将其代理到 3000 端口。

we are using pm2 in cluster mode with the following configuration:我们在集群模式下使用 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
  }]
}

run start:server:prod is our script to start the server运行 start:server:prod 是我们启动服务器的脚本

Our express server:我们的快递服务器:

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

Please help, it's mission critical!请帮忙,这是关键任务!

The problem is that pm2 doesn't play well with npm. 问题是pm2与npm不能很好地配合。 It is not able to start two node servers using npm script. 它无法使用npm脚本启动两个节点服务器。 The right way is to use node 正确的方法是使用节点

My previous config: 我以前的配置:

{
  "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
  }]
}

My new config: 我的新配置:

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

As you can see that I am no longer using "script": "npm". 你可以看到我不再使用“脚本”:“npm”。 ./server/bin/www contains my express server which pm2 will execute using node. ./server/bin/www包含我的express服务器,pm2将使用node执行。 Now pm2 is able to automatically handle clustering. 现在pm2能够自动处理群集。 So how's the output is supposed to look like now? 那么输出应该如何看起来像现在一样? It makes one god deamon which manages the worker server instances which are according to the number of your cpu cores. 它使一个上帝deamon管理根据你的cpu核心数量的工作服务器实例。 Output on our server: (It has 2 cores) 我们服务器上的输出:(它有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

Also, now pm2 reload works fine. 此外,现在pm2重载工作正常。 When I see logs on reload, pm2 starts new workers first and then shuts down the old workers. 当我看到重新加载日志时,pm2首先启动新工作人员,然后关闭旧工作人员。

For me changing the exec_mode to cluster_mode worked. 对我来说,将exec_mode更改为cluster_mode工作。

My ecosystem.config.js was changed to something like this 我的ecosystem.config.js改为这样的东西

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

My issue was that the script needed to be named ecosystem.config.js anything else wouldn't launch in cluster mode.我的问题是脚本需要命名为ecosystem.config.js .config.js,其他任何东西都不会在集群模式下启动。 it will launch in forked mode but the file name is necessary for any other config.它将以分叉模式启动,但任何其他配置都需要文件名。

I don't think you can use pm2 cluster mode with npm scripts. 我不认为你可以使用pm2集群模式和npm脚本。 First check if the process is actually in cluster mode using pm2 list . 首先使用pm2 list检查进程是否实际处于群集模式。 If it isn't, try modifying your configuration to call the node script directly instead of the calling npm script. 如果不是,请尝试修改配置以直接调用节点脚本而不是调用npm脚本。

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

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