Node.js集群+集合

时间:2013-04-07 00:56:21

标签: node.js tcp

我已将问题提交给github repo,以便在那里进行跟踪!

我正在运行可以在具有N个核心的计算机上的群集应用程序。假设我在本地运行2个应用程序实例进行测试,实际上模拟了2个不同的框。因此,使用cluster模块的N台机器上有N个核心(实际上,N台机器是静态的,例如只有2台AWS负载均衡器)。

  1. 如何为此正确配置collective.js“all_hosts”选项?我会以某种方式与IP一起使用process.id吗?
  2. 运行代码片段将与2个bash终端类似:

    terminal 1

    coffee cluster1

    terminal 2

    coffee cluster2

    注意:下面的代码有效,但确实不起作用,因为我无法弄清楚配置;每次我记录数据时都会特定于该过程。

    cluster1.coffee

    cluster = require 'cluster'
    numCPUs = require('os').cpus().length
    
    if cluster.isMaster
    
      i = 0 
      cluster.setupMaster 
        exec: './server1'
    
      console.log "App 1 clustering with: #{numCPUs} clusters"
    
      while i < numCPUs
        cluster.fork()
        i++
    
      cluster.on 'fork', (worker) ->
        console.log 'Forked App 1 server worker ' + worker.process.pid
    

    server1.coffee

    Collective = require 'collective'
    
    all_hosts = [
        host: 'localhost', port: 8124 # Wrong
    ]
    
    collective = new Collective(
      host: 'localhost'
      port: 8124
    , all_hosts, (collective) ->
    
    )
    
    collectiveUpsert = () ->
    
      num = Math.floor((Math.random()*10000)+1)
    
      data = 
        num: num
    
      console.log process.pid + ' sees current num as: ' + JSON.stringify(collective.get('foo.bar'))
      console.log process.pid + ' setting num to: ' + JSON.stringify(data)
    
      collective.set 'foo.bar', data
    
    setInterval (->
      collectiveUpsert()
    ), 5 * 1000
    

    cluster2.coffee

    cluster = require 'cluster'
    numCPUs = require('os').cpus().length
    
    if cluster.isMaster
    
      i = 0 
      cluster.setupMaster 
        exec: './server2'
    
      console.log "App 2 clustering with: #{numCPUs} clusters"
    
      while i < numCPUs
        cluster.fork()
        i++
    
      cluster.on 'fork', (worker) ->
        console.log 'Forked App 2 server worker ' + worker.process.pid
    

    server2.coffee

    Collective = require 'collective'
    
    all_hosts = [
        host: 'localhost', port: 8124 # Wrong
    ]
    
    collective = new Collective(
      host: 'localhost'
      port: 8124
    , all_hosts, (collective) ->
    
    )
    
    collectiveUpsert = () ->
    
      num = Math.floor((Math.random()*10000)+1)
    
      data = 
        num: num
    
      console.log process.pid + ' sees current num as: ' + JSON.stringify(collective.get('foo.bar'))
      console.log process.pid + ' setting num to: ' + JSON.stringify(data)
    
      collective.set 'foo.bar', data
    
    setInterval (->
      collectiveUpsert()
    ), 5 * 1000
    

1 个答案:

答案 0 :(得分:0)

为了将collective.jscluster和/或多个服务器一起使用,您需要在每个Node.js子进程上启动它。可以将其视为http模块,您必须在每个子/从设备上创建监听器,而不是主设备(http://nodejs.org/api/cluster.html#cluster_cluster)。遵循类似的逻辑,对于collective.js,你应该做这样的事情(单一服务器):

if (cluster.isMaster) {
    // fork n children
} else {
    var current_host = {host: "localhost", port: 10000};
    current_host.port += cluster.worker.id; // this is incremented for every new process.

    var all_hosts = [
        {"host": "localhost", "port": 10001},
        {"host": "localhost", "port": 10002},
        {"host": "localhost", "port": 10003},
        {"host": "localhost", "port": 10004},
        {"host": "localhost", "port": 10005},
        {"host": "localhost", "port": 10006}
        // must be the same amount as is the child process count.
    ];

    var collective = new modules.collective(current_host, all_hosts, function (collective) {
        // Do your usual stuff. Start http listener, etc...
    });
}

如果要在不同的服务器上使用此功能,则应将 localhost 修改为您的IP地址,并确保端口正确递增。

有关任何其他信息,您可以在test/index.js

查看原始测试

希望有所帮助!如果您需要任何进一步的帮助 - 请询问。

P.S。不可否认,这种方式很麻烦,需要更清楚的解释。我希望在不久的将来能够找到更简洁的初始化过程。除此之外,澄清自述文件并提供一些完整的例子。

相关问题