mkdir myapp
touch server.js
-
const http = require("http");
-
http.createServer((req, res) => {
-
res.writeHead(200);
-
res.end("server endpoint hit");
-
console.log(`got new connection on port ${req} ${process.pid}`)
-
}).listen(13002);
启动9个实例
pm2 start server.js -i 9
-
ps -ef | grep node
-
root 3320 32331 0 Nov16 ? 00:00:03 node /root/study/nodejs/myapp/index.js
-
root 3510 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3517 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3524 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3531 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3542 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3553 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3564 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3575 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
-
root 3586 32331 0 00:08 ? 00:00:01 node /root/study/nodejs/server_app/server.js
pm2 ls查看 mode 为cluster
验证访问是否打到不同的进程
用pm2 log server 查看日志如下
2|server | got new connection on port [object Object] 5421
3|server | got new connection on port [object Object] 5422
4|server | got new connection on port [object Object] 5435
5|server | got new connection on port [object Object] 5441
6|server | got new connection on port [object Object] 5449
7|server | got new connection on port [object Object] 5455
curl多次访问如下
curl -is localhost:13002
增加多进程监听多端口模式,主要是该字段起作用 increment_var : 'PORT'
module.exports = { apps : [ { name: "myapp", script: "./app.js", instances: 2, exec_mode: "cluster", watch: true, increment_var : 'PORT', env: { "PORT": 3000, "NODE_ENV": "development" } } ] }
见:pm2.keymetrics.io/docs/usage/environment/
阅读(754) | 评论(0) | 转发(0) |