[英]Azure service bus dropping messages?
我试图在Azure中构建一个简单的Web API REST服务,后端有一个服务总线队列工作器。 我可以从Web API向工作人员发送一条消息就好了。 但是,我试图发送更多消息只是为了看看一切是如何工作的。 所以,我创建了一个看起来像这样的简单控制器:
for (int i = 0; i < 100; i++)
{
var msg = new BrokeredMessage("Ping");
BioConnector.QueueConnector.OrdersQueueClient.Send(msg);
}
当我打电话给控制器时,我只得到工人收到的消息的大约1/2左右。 其余的似乎都被丢弃了。
使用此处发布的示例代码,我只收到大约一半的消息,所以我编写了自己的测试代码。 我用> 100个队列消息尝试了它,并且总是有100%发送/记录奇偶校验。 也许你的代码有类似的问题。
在app.config中,使用您提供的值将其更改为:
<appSettings> <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://blahblah.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=pDk0b....=" /> </appSettings>
使用指令添加这些:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using Microsoft.ServiceBus; using Microsoft.ServiceBus.Messaging; using System.Configuration; using System.Threading;
将代码方法更改为以下内容:
class Program { static void Main(string[] args) { string connectionString = ConfigurationSettings.AppSettings["Microsoft.ServiceBus.ConnectionString"]; var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); QueueDescription queueDesc = new QueueDescription("TestQueue"); if (!namespaceManager.QueueExists(queueDesc.Path)) { namespaceManager.CreateQueue(queueDesc); } QueueClient topicClient = QueueClient.CreateFromConnectionString(connectionString, queueDesc.Path); int sentMsgCount = 0; int recdMsgCount = 0; for (int i = 0; i < 100; i++) { BrokeredMessage msg = new BrokeredMessage("Test message " + i); topicClient.Send(msg); sentMsgCount++; Console.WriteLine("Sent Message: " + msg); } QueueClient subClient = QueueClient.CreateFromConnectionString(connectionString, queueDesc.Path); bool moreMessages = true; while (moreMessages) { BrokeredMessage recdMsg = subClient.Receive(TimeSpan.FromSeconds(3)); if (recdMsg != null) { Console.WriteLine("Received Message: " + recdMsg); recdMsgCount++; recdMsg.Complete(); } else { moreMessages = false; } } Console.WriteLine("# of sent msgs: " + sentMsgCount + ", # of rec'd msgs: " + recdMsgCount); Console.Read(); } }
这是一个奇怪的问题。 通过随机浏览“尝试的东西”,我最终改变了队列的字符串名称,然后一切都开始工作了。 我没有更改除队列名称之外的任何内容 - 根本不更改任何配置参数。
它似乎与Azure上的特定队列有关。
Azure Service Bus提供持久的消息传递,因此您不会丢失任何消息。 一些要进一步调查的项目:1)是否有另一个worker角色实例从该队列中取消消息2)您是否使用peek-lock作为接收模式,因为这将是保证至少一次传递的唯一方法。 接收和删除模式没有保证3)由于消息过期或超过最大传送计数而进入死信队列的消息,即它们被接收但未完成多次4)如果以上都不适用然后提高支持服务单和Azure产品团队可以调查症状,因为正如我所提到的,这是一个持久的消息传递系统,所以没有消息会“丢失”。
我的项目中有WindowsAzure.ServiceBus NuGet包 ,并使用QueueClient.Send()
发送消息并遇到相同的消息丢失问题。
我的解决方案完全解决了这个问题:
在发送方面,我不得不使用REST API来发送消息 。
在接收方,这是我提取邮件正文的方式:
using (var stream = brokeredMessage.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var msg = streamReader.ReadToEnd();
// Convert the JSON message to an object
// var obj = JsonConvert.DeserializeObject<ObjectType>(msg);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.