简体   繁体   English

Node.js集群+集体

[英]Node.js cluster + collective

I have submitted the issue to the github repo , so as to track it there! 我已将问题提交到github repo ,以便在那里进行跟踪!

I'm running a clustered app that could be on a machine with N cores. 我正在运行一个群集应用程序,该程序可能位于具有N个内核的计算机上。 Let's say I am running 2 of the app instances locally for testing, really emulating 2 different boxes. 假设我在本地运行2个应用实例进行测试,实际上是在模拟2个不同的盒子。 So N cores on N machines using the cluster module (in reality, the N machines is static, eg just 2 behind an AWS Load Balancer). 因此,使用cluster模块的N台计算机上的N个内核(实际上,N台计算机是静态的,例如,仅在AWS Load Balancer之后2个)。

  1. How do I properly configure the collective.js "all_hosts" option for this? 我该如何为此正确配置collection.js的 “ all_hosts”选项? Would I use process.id somehow along with IP? 我会以某种方式将process.id与IP一起使用吗?

Running the code snippets would be something along the lines of 2 bash terminals: 运行2个bash终端需要运行代码段:

terminal 1 : 1号航站楼

coffee cluster1

terminal 2 : 2号航站楼

coffee cluster2

Note: the code below works, but doesn't really work, as I can't quite figure out the configuration; 注:以下作品的代码,但没有真正的工作,因为我不能完全弄清楚的配置; each time I log data it's specific to the process. 每次记录数据时,它都是特定于该过程的。

cluster1.coffee : 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 : 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 : 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 : 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

In order to use collective.js with cluster and/or multiple servers you need to start it on every Node.js child process. 为了将Collective.jscluster和/或多个服务器一起使用,您需要在每个Node.js子进程上启动它。 Think of it as a http module, where you have to create the listener on every child/slave, not the master ( http://nodejs.org/api/cluster.html#cluster_cluster ). 可以将其视为一个http模块,在该模块中,您必须在每个子/从属服务器上而不是在主服务器上创建侦听器( http://nodejs.org/api/cluster.html#cluster_cluster )。 Following similar logic, for collective.js, you should do something like this (single server): 按照类似的逻辑,对于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...
    });
}

You should modify localhost to your ip addresses and make sure ports increment properly, if you want to use this on different servers. 如果要在其他服务器上使用localhost ,则应将localhost修改为您的IP地址,并确保端口正确增加。

For any additional information you can check crude tests at test/index.js 有关任何其他信息,您可以在test / index.js上检查原始测试。

Hope that helps! 希望有帮助! If you need any further assistance - please ask. 如果您需要任何进一步的帮助-请询问。

PS Admittedly, this way is to cumbersome and needs clearer explanation. PS诚然,这种方式比较麻烦并且需要更清晰的解释。 I hope to figure out a cleaner and easier initialization process in the near future. 我希望在不久的将来能找到一个更清洁,更轻松的初始化过程。 In addition to that, clarify the readme and provide some full examples. 除此之外,请澄清自述文件并提供一些完整的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM