我已将问题提交到 github repo,以便在那里进行跟踪!
我正在运行一个集群应用程序,它可以在具有 N 个内核的机器上。假设我在本地运行 2 个应用程序实例进行测试,实际上模拟了 2 个不同的盒子。因此,使用该模块的 N 台机器上的 N 个内核cluster
(实际上,N 台机器是静态的,例如,仅在 AWS 负载均衡器后面 2 个)。
- 如何为此正确配置collective.js “all_hosts”选项?我会
process.id
以某种方式与 IP 一起使用吗?
运行代码片段将类似于 2 个 bash 终端:
1号航站楼:
coffee cluster1
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