如何使用带有Node.js的azure服务总线来侦听队列?

时间:2018-06-04 14:08:57

标签: node.js azure azureservicebus azure-servicebus-queues

背景

我有几个客户端将消息发送到azure服务总线队列。为了匹配它,我需要使用Node.js从该队列读取多个机器并在消息到达时使用消息。

研究

我已阅读azure service bus queues tutorial,我知道我可以使用receiveQueueMessage从队列中读取消息。

但是,本教程没有提到如何在收到消息后立即收听队列并阅读消息。

我知道我可以简单地在队列中查询消息,但这会使服务器无法获得真正的好处。

在SO中搜索后,我发现了一个有人遇到类似问题的讨论:

我知道他们最终使用了C#异步方法ReceiveAsync,但我不清楚是否:

  1. 该方法适用于Node.js
  2. 如果该方法一旦到达就会从队列中读取消息,就像我需要的那样。
  3. 问题

    Node.js的文档接近于不存在,其中一个教程是我找到的唯一主要文档。

    问题

    1. 如何通过天蓝色巴士服务队列通知我的工作人员收到的消息?

3 个答案:

答案 0 :(得分:2)

答案

根据Azure支持,当队列收到消息时,无法通知。这适用于所有语言。

解决方法

这个问题有两个主要的解决方法:

  1. 使用Azure主题和订阅。这样,您可以让所有客户端订阅事件 ko.bindingHandlers.PLPTileSizeOnHover = { init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { var data = ko.unwrap(valueAccessor()); // Intiallizing variables for product image animation var _index = 0; var imgArray = data.fullImageURLs(); var toBeScrolledBy = 0; var scroller = $(element).find('.customized-slider').eq(0); var StopAnimation; element.onmouseover = function () { //Start Animation StopAnimation = setInterval(function () { var totalSlides = $(element).find('.customized-slider').eq(0).children(); var slideWidth = totalSlides[0] && totalSlides[0].clientWidth; _index++; $($(element).find('.product-images')[_index]).attr('src', imgArray[_index]); if (_index >= imgArray.length) { _index = 0; } toBeScrolledBy = slideWidth * _index; $(scroller).css({ 'transform': 'translateX(-' + toBeScrolledBy + 'px)' }); }, 1500); } element.onmouseout = function () { //End of animation and reseting the index and div postion clearInterval(StopAnimation); _index = 0; $(scroller).css({ 'transform': 'translateX(0)' }); } } } ,并让他们在收到通知后检查队列。这有几个问题:首先你必须支付另一个Azure服务,其次你可以让多个客户尝试阅读相同的消息。

  2. 持续轮询。让客户每隔X秒检查一次队列。这个解决方案非常糟糕,因为您最终支付了所产生的网络流量,并且您通过无用的请求将服务垃圾邮件。为了帮助最大限度地减少这种情况,有一个名为长轮询的概念,记录很少,可能不存在。我确实找到了这个NPM模块:https://www.npmjs.com/package/azure-awesome-queue

  3. 替代

    老实说,此时,您可能想知道为什么要使用此服务。 我同意......

    作为替代方案,RabbitMQ是免费的,有社区,良好的文档和更多功能。

    这里的缺点是维护一个RabbitMQ容错集群并不是一件容易的事。

    另一种选择是Apache Kafka,它也非常可靠。

答案 1 :(得分:0)

我问myslef同样的问题,这里是我发现了什么。

使用谷歌PubSub的,它是你寻找什么。

如果你想留在Azure中,以下IST可能的:

  • 可以从SBS消息中触发云功能
  • 使用该云功能触发事件中心事件
  • 接收事件并从SBS获取消息

答案 2 :(得分:0)

您可以使用无服务器功能,即“ ServiceBusQueueTrigger”, 消息到达队列后立即调用它们,

在nodejs中进行操作非常简单,您需要在function.json中定义的绑定类型为

“类型”:“ serviceBusTrigger”,

本文(https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---javascript-example)可能会提供更多帮助。

相关问题