0

そこで追跡できるように、問題をgithub repoに送信しました 。

N 個のコアを持つマシン上にある可能性のあるクラスター化されたアプリを実行しています。テスト用に 2 つのアプリ インスタンスをローカルで実行し、2 つの異なるボックスを実際にエミュレートしているとします。したがって、モジュールを使用する N 台のマシン上の N 個のコアcluster(実際には、N 台のマシンは静的です。たとえば、AWS ロード バランサーのわずか 2 台の背後にあります)。

  1. これに対して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
4

1 に答える 1

0

および/または複数のサーバーでcollective.jsを使用するclusterには、すべての 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で大まかなテストを確認できます。

それが役立つことを願っています! さらにサポートが必要な場合は、お問い合わせください。

PS 確かに、この方法は面倒であり、より明確な説明が必要です。近い将来、よりクリーンで簡単な初期化プロセスを見つけたいと思っています。それに加えて、readme を明確にし、いくつかの完全な例を提供してください。

于 2013-04-07T09:19:12.847 に答える