问题
I have read over this article pretty thoroughly and as well have spent a few hours researching the subject of clustering (forking processes) in Node.js.
What I can't seem to understand from the article, is what determines which worker process gets request X, if they are all listening on the same port?
Is there a way for the master process to channel the requests, or is it just random?
回答1:
There a good explanation here. Long story short, there are 2 different behaviors depending on your node version:
node 0.8-0.10 (and 0.12+ on Windows): Each process listens on the port. The OS decides which one to wake up when a new connection comes in. In some applications under some OSs this doesn't work very well and leaves a few processes with a strong majority of the connections; in most it works just fine.
node 0.12+ (except on Windows): The master process listens on the port. As they come in, it hands them off to workers in a round-robin fashion.
In either of these cases, your application should treat it as random (although you can probably assume reasonable load-balancing characteristics). However, if you for some reason need finer control, one sentence in that article (note that it was written by a node.js core contributor, so there's some authority here):
Turning the selection algorithm into something that is configurable or pluggable by the developer is a change that is under consideration.
says that you might get what you're looking for. There appears to be an issue on Github pertaining to this option.
来源:https://stackoverflow.com/questions/20184890/node-js-clustering-what-determines-load-balancing