首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ZeroMQ向NodeJS云后端发送

问用ZeroMQ向NodeJS云后端发送
EN

Stack Overflow用户
提问于 2015-07-23 08:23:28
回答 1查看 105关注 0票数 1

我正在学习ZeroMQ,并从头开始制作指南中的所有示例(在NodeJS中)。但是,我正在创建一个本地集群,该集群处理作业,如果不能再处理,则将作业发送给通过其他套接字(“把一切都放在一起”的例子)连接的“云对等点”。

我请求您帮助调试代码,以查看为什么云对等端(并返回)不处理客户端的消息

  1. 当员工连接时,发送一条现成的信息,让localBE知道有多少人。此消息是对所有云对等点(附加对等名)的PUBlished。
  2. 当客户端发送REQuest时,localFE会接收它。
  3. 如果有本地工作人员可用,则LocalFE路由到localBE。否则,路由到随机的cloudBE对等点。
  4. 因此,cloudFE接收到该消息,并在可用的情况下将其路由到本地工作人员。然后,它应该返回到原来的对等程序的客户端(!)

如果您是克隆并执行我的回购 (cd to Chapter3,然后是两个带有node peering3.js me you和node peering3.js you me的终端),您可以跟踪谁发送和接收(Get)数据。

您可以使用NBR_CLIENTS和NBR_WORKERS (第12和13行),并看到作业没有正确地发送/返回…

如果你能看看我的代码,我将非常感激!

提前感谢…

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-23 15:00:51

您在云代理之间的地址有问题。您正在使用本地workers ID在云代理之间寻址。注释代码以突出显示问题。

代码语言:javascript
复制
//  - 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队列将为空,并且消息不会发送到任何地方。您也失去了对云对等点的引用,因此当消息返回时,就无法知道它需要返回到云对等端:

代码语言:javascript
复制
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)。但是,我们不仅失去了以前发送给我们消息的云代理的引用,而且在从工作人员那里收到消息时,我们甚至没有尝试将它发回:

代码语言:javascript
复制
//  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]]);
    }
});

所以:

  1. 当shift()发送给云代理时,不要在localfe发送到云代理时排队--在有机会让他们工作之前,您很可能会失去所有本地工人
  2. 在cloudfe上接收消息时,将一个工作人员ID关闭一次到局部变量中,并在需要的地方使用该局部变量
  3. 获取云对等ID并将其添加到消息中,以便您知道是哪个对等点发起了云请求。
  4. 如果队列中没有可用的工作人员,可以将其挂起并执行setTimeout()重试,或者将其发送给新的云对等程序。为了简单起见,我建议使用前者,否则您必须在消息中跟踪一组潜在的云对等I。
  5. 当从工作人员接收回消息时,请检查云对等ID,如果找到,则将其适当地路由回去,而不是盲目地将其路由回可能通过不同云代理连接的客户端。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31581908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档