首页 文章

Nodejs Cluster:选择Worker

提问于
浏览
1

我使用Nodejs Cluster . 我有8名 Worker . 每当我去应用程序时,我都会连接到同一个worker(这是正常的,因为worker可以处理多个客户端 . )

出于测试目的,我想在不必围攻应用程序的情况下连接到不同的工作人员 . 有没有办法做到这一点?

例如:转到mywebsite.com/3将连接到第3名工作人员 .

1 回答

  • 1

    这是一个基于端口的解决方案:

    var cluster = require('cluster');
    var http = require('http');
    
    if (cluster.isMaster) {
    
      cluster.fork();
      cluster.fork();
      cluster.fork();
    
      return;
    
    }
    
    
    function app (req, res) {
      res.writeHead(200);
      res.end('hello from ' + cluster.worker.id);
    }
    
    http.createServer(app).listen(8000);
    http.createServer(app).listen(8000 + cluster.worker.id);
    

    例如,如果您希望连接到2工作人员,则使用端口8002 .

相关问题