{"id":454,"date":"2021-05-04T15:48:59","date_gmt":"2021-05-04T15:48:59","guid":{"rendered":"http:\/\/robinluo.top\/?p=454"},"modified":"2021-05-04T15:48:59","modified_gmt":"2021-05-04T15:48:59","slug":"pm2-loadbalance","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=454","title":{"rendered":"pm2 loadbalance"},"content":{"rendered":"\n<p>pm2 \u8d1f\u8f7d\u5747\u8861<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm2 start app.js -i 4<\/code><\/pre>\n\n\n\n<p>\u4ee51\u4e2a\u4e3b\u8fdb\u7a0b\u76d1\u542c\u7aef\u53e3  4\u4e2a\u526f\u8fdb\u7a0b \u5904\u7406nodejs\u8bf7\u6c42\u8fdb\u7a0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm2 start app.js -i max<\/code><\/pre>\n\n\n\n<p>\u4ee5cpu\u6838\u5fc3\u7684\u6570\u91cf\u5904\u7406nodejs\u8bf7\u6c42\u8fdb\u7a0b<\/p>\n\n\n\n<p>pm2 \u5176\u5b9e\u662f\u5229\u7528nodejs cluster\u6a21\u5757\u8fd0\u884c<\/p>\n\n\n\n<p>\u67e5\u770b<a href=\"http:\/\/nodejs.cn\/api\/cluster.html\">http:\/\/nodejs.cn\/api\/cluster.html<\/a><\/p>\n\n\n\n<p>\u5355\u4e2a Node.js \u5b9e\u4f8b\u8fd0\u884c\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u3002 \u4e3a\u4e86\u5145\u5206\u5229\u7528\u591a\u6838\u7cfb\u7edf\uff0c\u6709\u65f6\u9700\u8981\u542f\u7528\u4e00\u7ec4 Node.js \u8fdb\u7a0b\u53bb\u5904\u7406\u8d1f\u8f7d\u4efb\u52a1\u3002<\/p>\n\n\n\n<p><code>cluster<\/code>\u00a0\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u5171\u4eab\u670d\u52a1\u5668\u7aef\u53e3\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const cluster = require('cluster');\r\nconst http = require('http');\r\nconst numCPUs = require('os').cpus().length;\r\n\r\nif (cluster.isMaster) {\r\n  console.log(`\u4e3b\u8fdb\u7a0b ${process.pid} \u6b63\u5728\u8fd0\u884c`);\r\n\r\n  \/\/ \u884d\u751f\u5de5\u4f5c\u8fdb\u7a0b\u3002\r\n  for (let i = 0; i &lt; numCPUs; i++) {\r\n    cluster.fork();\r\n  }\r\n\r\n  cluster.on('exit', (worker, code, signal) => {\r\n    console.log(`\u5de5\u4f5c\u8fdb\u7a0b ${worker.process.pid} \u5df2\u9000\u51fa`);\r\n  });\r\n} else {\r\n  \/\/ \u5de5\u4f5c\u8fdb\u7a0b\u53ef\u4ee5\u5171\u4eab\u4efb\u4f55 TCP \u8fde\u63a5\u3002\r\n  \/\/ \u5728\u672c\u4f8b\u5b50\u4e2d\uff0c\u5171\u4eab\u7684\u662f HTTP \u670d\u52a1\u5668\u3002\r\n  http.createServer((req, res) => {\r\n    res.writeHead(200);\r\n    res.end('\u4f60\u597d\u4e16\u754c\\n');\r\n  }).listen(8000);\r\n\r\n  console.log(`\u5de5\u4f5c\u8fdb\u7a0b ${process.pid} \u5df2\u542f\u52a8`);\r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ node server.js\n\u4e3b\u8fdb\u7a0b 3596 \u6b63\u5728\u8fd0\u884c\n\u5de5\u4f5c\u8fdb\u7a0b 4324 \u5df2\u542f\u52a8\n\u5de5\u4f5c\u8fdb\u7a0b 4520 \u5df2\u542f\u52a8\n\u5de5\u4f5c\u8fdb\u7a0b 6056 \u5df2\u542f\u52a8\n\u5de5\u4f5c\u8fdb\u7a0b 5644 \u5df2\u542f\u52a8<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>pm2 \u8d1f\u8f7d\u5747\u8861 \u4ee51\u4e2a\u4e3b\u8fdb\u7a0b\u76d1\u542c\u7aef\u53e3 4\u4e2a\u526f\u8fdb\u7a0b \u5904\u7406nodejs\u8bf7\u6c42\u8fdb\u7a0b \u4ee5cpu\u6838\u5fc3\u7684\u6570\u91cf\u5904\u7406nodejs\u8bf7\u6c42\u8fdb\u7a0b pm2 \u5176\u5b9e\u662f\u5229\u7528nodejs cluster\u6a21\u5757\u8fd0\u884c \u67e5\u770bhttp:\/\/nodejs.cn\/api\/cluster.html \u5355\u4e2a Node.js \u5b9e\u4f8b\u8fd0\u884c\u5728\u5355\u4e2a\u7ebf\u7a0b\u4e2d\u3002 \u4e3a\u4e86\u5145\u5206\u5229\u7528\u591a\u6838\u7cfb\u7edf\uff0c\u6709\u65f6\u9700\u8981\u542f\u7528\u4e00\u7ec4 Node.js \u8fdb\u7a0b\u53bb\u5904\u7406\u8d1f\u8f7d\u4efb\u52a1\u3002 cluster\u00a0\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u5171\u4eab\u670d\u52a1\u5668\u7aef\u53e3\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[74],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/454"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=454"}],"version-history":[{"count":2,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}