Node.js 使用NodeJS发送到ZeroMQ云后端
我正在学习ZeroMQ并从头开始制作指南中的所有示例(在NodeJS中)。但我与一个创建本地集群来处理作业的集群不谋而合,如果它不能再处理作业,则将作业发送给通过其他套接字连接的“云对等方”() 我请求您帮助调试代码,以了解为什么客户端的消息没有被云对等方处理(并返回):Node.js 使用NodeJS发送到ZeroMQ云后端,node.js,sockets,zeromq,Node.js,Sockets,Zeromq,我正在学习ZeroMQ并从头开始制作指南中的所有示例(在NodeJS中)。但我与一个创建本地集群来处理作业的集群不谋而合,如果它不能再处理作业,则将作业发送给通过其他套接字连接的“云对等方”() 我请求您帮助调试代码,以了解为什么客户端的消息没有被云对等方处理(并返回): 当工作人员连接时,发送一条就绪消息,让local知道有多少人。此消息将发布到所有云对等方(附加对等方名称) 当客户端发送请求时,localFE将接收该请求 如果有本地工人可用,LocalFE将路由到localBE。否则,到随机
cd
到第3章,然后是两个带有节点peering3.js me you
和节点peering3.js you me
的终端),您可以跟踪谁发送和接收(获取
)数据
您可以使用NBR\U客户端
和NBR\U工作人员
(第12行和第13行),查看作业未正确发送/返回
如果你能看看我的代码,我将非常感激
提前感谢…您在云代理之间的寻址有问题。您正在使用本地
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]]);
}
});
因此:
localfe
发送到云代理时,不要让您的工作人员排队-您可能会在没有机会发送工作之前失去所有本地工作人员cloudfe
的消息时,shift()
将工作人员ID转换为局部变量,并在需要时使用该局部变量setTimeout()
重试,或者将其发送给新的云对等方。为了简单起见,我建议使用前者,否则您必须在消息中跟踪整个云对等ID序列在云代理之间进行寻址有问题。您正在使用本地
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]]);
}
});
因此:
localfe
发送到云代理时,不要让您的工作人员排队-您可能会在没有机会发送工作之前失去所有本地工作人员cloudfe
的消息时,shift()
将工作人员ID转换为局部变量,并在需要时使用该局部变量setTimeout()
重试,或者将其发送给新的云对等方。为了简单起见,我建议使用前者,否则您必须在消息中跟踪整个云对等ID序列在深入了解我是否能提供帮助之前,感谢您(a)通读了指南,(b)实际使用了示例。我强烈建议您提交任何缺少节点版本的示例的代码。谢谢!你会看到这里仍然缺少一些东西,但我以前专注于本地云通信…我是第一个在工作中学习ZeroMQ的人,我想在介绍任何内容之前先阅读指南。在深入到