pm2集群模式下的快速服务器端口配置问题 [英] express server port configuration issue with pm2 cluster mode

查看:626
本文介绍了pm2集群模式下的快速服务器端口配置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

We using nginx and proxy it to 3000 port.

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

we are using pm2 in cluster mode with the following configuration:

{
  "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是我们启动服务器的脚本

run start:server:prod is our script to start the 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!

推荐答案

问题是pm2在npm中不能很好地发挥作用.它无法使用npm脚本启动两个节点服务器.正确的方法是使用节点

The problem is that pm2 doesn't play well with npm. It is not able to start two node servers using npm script. The right way is to use node

我以前的配置:

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

如您所见,我不再使用"script":"npm". ./server/bin/www包含我的快速服务器,pm2将使用节点执行该服务器.现在,pm2能够自动处理集群. 那么输出应该看起来像现在吗? 它使一个上帝守护进程,根据您的cpu核心数来管理工作服务器实例. 我们服务器上的输出:(它有2个核心)

As you can see that I am no longer using "script": "npm". ./server/bin/www contains my express server which pm2 will execute using node. Now pm2 is able to automatically handle clustering. 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. Output on our server: (It has 2 cores)

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首先启动新的工作程序,然后关闭旧的工作程序.

Also, now pm2 reload works fine. When I see logs on reload, pm2 starts new workers first and then shuts down the old workers.

这篇关于pm2集群模式下的快速服务器端口配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆