我正在学习ZeroMQ,并从头开始制作指南中的所有示例(在NodeJS中)。但是,我正在创建一个本地集群,该集群处理作业,如果不能再处理,则将作业发送给通过其他套接字(“把一切都放在一起”的例子)连接的“云对等点”。
我请求您帮助调试代码,以查看为什么云对等端(并返回)不处理客户端的消息
如果您是克隆并执行我的回购 (cd to Chapter3,然后是两个带有node peering3.js me you和node peering3.js you me的终端),您可以跟踪谁发送和接收(Get)数据。
您可以使用NBR_CLIENTS和NBR_WORKERS (第12和13行),并看到作业没有正确地发送/返回…
如果你能看看我的代码,我将非常感激!
提前感谢…
发布于 2015-07-23 15:00:51
您在云代理之间的地址有问题。您正在使用本地workers ID在云代理之间寻址。注释代码以突出显示问题。
// - Route any request locally if we can, else to cloud.
localfe.on('message', function() {
var args = Array.apply(null, arguments);
console.log('LocalFE: Get ' + args.toString());
if (localCapacity > 0) {
console.log('LocalFE: Send '+ workers.shift() + ',\'\',' + args[0]+ ',\'\','+ args[2] + ' to LocalBE');
localCapacity--;
statebe.send(localCapacity);
localbe.send([workers.shift(), '', args[0], '', args[2]]);
} else {
// Route to random broker peer automatically
var randomPeer = randomBetween(3, argc);
var peer = process.argv[randomPeer];
/////////////////
// why are you referencing `workers` here, that is only for local workers
// You correctly route to `peer` here, though, so that should be fine
// however, you've removed a local worker from the queue erroneously
/////////////////
console.log('LocalFE: Send '+ workers.shift() + ',\'\',' + args[0]+ ',\'\','+ args[2] + ' to CloudBE at peer ' + peer);
cloudbe.send([peer, '', args[0], '', args[2]]);
}
});. cloudfe然后正确地将消息路由到本地工作人员,但在此之前它不会检查workers队列中是否有可用的工作人员,因此如果所有本地工作人员都被占用,那么您将被保存,本地workers队列将为空,并且消息不会发送到任何地方。您也失去了对云对等点的引用,因此当消息返回时,就无法知道它需要返回到云对等端:
cloudfe.on('message', function() {
var args = Array.apply(null, arguments);
console.log('CloudFE: Get ' + args.toString());
/////////////////
// if `workers` is already empty, `shift()`ing it will get you `undefined`
// also, you're removing the ID from the queue, which causes problems below
/////////////////
console.log('CloudFE: Send '+ workers.shift() + ',\'\',' + args[2]+ ',\'\','+ args[4] + ' to LocalBE');
localCapacity--;
statebe.send(localCapacity);
/////////////////
// you're now sending it to a *different* worker than you logged above
// and you've removed *two* workers from the queue instead of one
// as said above, if `workers` is already empty, you'll route it nowhere
// and lose the message
// further, nowhere in here are you logging the identity of the cloud peer, so you've
// lost the ability to route it back to the cloud peer that has the client
/////////////////
localbe.send([workers.shift(), '', args[2], '', args[4]]);
});..。工作人员应该成功地处理该消息并将其发送回其本地代理,至少对于前两条消息(而不是5,因为我们只将其发送给员工2和4)。但是,我们不仅失去了以前发送给我们消息的云代理的引用,而且在从工作人员那里收到消息时,我们甚至没有尝试将它发回:
// Reply from local worker.
localbe.on('message', function() {
var args = Array.apply(null, arguments);
//console.log('LocalBE: Get ' + args.toString());
workers.push(args[0]); // Add its identity to the array.
// We broadcast new capacity messages to other peers.
localCapacity++;
statebe.send(localCapacity);
// If it's not READY message, route the reply to client.
if (args[2].toString() != WORKER_READY) {
console.log('LocalBE: Send ' + args[2].toString() + ',\'\', ' + args[4].toString());
/////////////////
// you're attempting to send it directly back to the client, but the client
// you're addressing is not `connect()`ed to this broker, it's connected to
// the cloud broker, so it goes nowhere
/////////////////
localfe.send([args[2], '', args[4]]);
}
});所以:
shift()发送给云代理时,不要在localfe发送到云代理时排队--在有机会让他们工作之前,您很可能会失去所有本地工人cloudfe上接收消息时,将一个工作人员ID关闭一次到局部变量中,并在需要的地方使用该局部变量setTimeout()重试,或者将其发送给新的云对等程序。为了简单起见,我建议使用前者,否则您必须在消息中跟踪一组潜在的云对等I。https://stackoverflow.com/questions/31581908
复制相似问题