这是我非常简单的应用程序,当我运行它并要求始终有一个工作人员正在响应时,即使我同时加载了3000个以上的请求也是如此。
const http = require('http')
const options = [
"Go for it!",
"Maybe sleep on it",
"Do some more research",
"I don't know",
"I wouldn't"
]
const server = http.createServer((req, res) => {
const randomIndex = Math.floor(Math.random() * options.length)
const advice = options[randomIndex]
const payload = JSON.stringify({
processID: process.pid,
advice
})
console.log(`advice from ${process.pid}: ${advice}`)
res.writeHead(200, { 'Content-Type': 'application/json'})
res.end(payload)
})
server.listen(3000)
console.log(`advice service running`)
当我从pm2监视它时,我看到所有请求都是由最后一个工作人员处理的。