[英]How to share a global variable in node js multi cluster mode when socket io is running on NGNIX load balancer but on fork using pm2?
[英]How to start next.js app with pm2 in cluster mode using Yarn?
我正在尝试在 pm2 中启动具有 2 个集群的 next.js 应用程序。 所以将命令yarn start
映射到pm2 start yarn -i 2 --name "frontend" -- start
应用程序启动,然后在一秒钟内崩溃。
[PM2] Starting /usr/bin/yarn in cluster_mode (2 instances)
[PM2] Done.
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ frontend │ default │ N/A │ cluster │ 14901 │ 0s │ 0 │ online │ 0% │ 33.8mb │ user │ disabled │
│ 1 │ frontend │ default │ N/A │ cluster │ 14908 │ 0s │ 0 │ online │ 0% │ 28.8mb │ user │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
.log 文件的输出
0|frontend | /usr/share/yarn/bin/yarn:2
0|frontend | argv0=$(echo "$0" | sed -e 's,\\,/,g')
0|frontend | ^^^^
0|frontend |
0|frontend | SyntaxError: missing ) after argument list
0|frontend | at Module._compile (internal/modules/cjs/loader.js:895:18)
0|frontend | at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
0|frontend | at Module.load (internal/modules/cjs/loader.js:815:32)
0|frontend | at Function.Module._load (internal/modules/cjs/loader.js:727:14)
0|frontend | at /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js:301:25
0|frontend | at wrapper (/usr/local/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16)
0|frontend | at next (/usr/local/lib/node_modules/pm2/node_modules/async/waterfall.js:96:20)
0|frontend | at /usr/local/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
0|frontend | at WriteStream.<anonymous> (/usr/local/lib/node_modules/pm2/lib/Utility.js:186:13)
0|frontend | at WriteStream.emit (events.js:210:5)
注意:作为 npm 应用程序启动应用程序可以工作,但在 fork 模式下。
npm/yarn 脚本启动 pm2 集群模式是不可行的,必须使用 javascript 文件路径作为集群脚本。 对于 next.js,您可以像这样编写配置:
module.exports = {
apps: [{
script: "node_modules/next/dist/bin/next",
args: "start",
exec_mode: "cluster",
}]
}
module.exports = {
apps : [
{
name: "project",
script: "./server.js",
watch: true,
interpreter: ".npm/yarn", //absolute path to yarn ; default is node
interpreter_args: "",
exec_mode: "cluster",
cwd: "", //the directory from which your app will be launched
args: "", //string containing all arguments passed via CLI to script
env_development: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 8000,
"NODE_ENV": "production",
}
}
]
}
任何其他参数都可以在这里看到:
https://pm2.keymetrics.io/docs/usage/application-declaration/
在项目级别创建文件ecosystem.config.js
并在下面设置配置:
module.exports = {
apps : [
{
name: "sa_page_local",
mode: "cluster",
script: "./server.js",
watch: true,
env_development: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 8000,
"NODE_ENV": "production",
}
}
]
}
pm2 start ecosystem.config.js --env production
pm2 start ecosystem.config.js --env development
我没有尝试过,只是根据文档,我写了这个场景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.