如果您使用相同的端点名称创建发布者和使用者,我们遇到了MassTransit丢失消息的情况。
请注意以下代码;如果我为消费者或发布者使用不同的端点名称(例如发布者的“rabbitmq:// localhost / mtlossPublised”),则该消息将计算已发布和已消费的匹配;如果我使用相同的端点名称(如示例中所示)那么我消耗的消息少于发布消息。
这是预期的行为吗?或者我做错了什么,在下面编写示例代码。
using MassTransit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MTMessageLoss
{
class Program
{
static void Main(string[] args)
{
var consumerBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
var publisherBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
consumerBus.SubscribeConsumer(() => new MessageConsumer());
for (int i = 0; i < 10; i++)
publisherBus.Publish(new SimpleMessage() { CorrelationId = Guid.NewGuid(), Message = string.Format("This is message {0}", i) });
Console.WriteLine("Press ENTER Key to see how many you consumed");
Console.ReadLine();
Console.WriteLine("We consumed {0} simple messages. Press Enter to terminate the applicaion.", MessageConsumer.Count);
Console.ReadLine();
consumerBus.Dispose();
publisherBus.Dispose();
}
}
public interface ISimpleMessage : CorrelatedBy<Guid>
{
string Message { get; }
}
public class SimpleMessage : ISimpleMessage
{
public Guid CorrelationId { get; set; }
public string Message { get; set; }
}
public class MessageConsumer : Consumes<ISimpleMessage>.All
{
public static int Count = 0;
public void Consume(ISimpleMessage message)
{
System.Threading.Interlocked.Increment(ref Count);
}
}
}
答案 0 :(得分:4)
底线,总线的每个实例都需要自己的队列来读取。即使总线只存在发布消息。这只是MassTransit工作方式的要求。
http://masstransit.readthedocs.org/en/master/configuration/config_api.html#basic-options - 请参阅警告。
当两个总线实例共享同一队列时,我们将行为保留为未定义。无论如何,这不是我们支持的条件。每个总线实例可以将元数据发送到其他总线实例,并且需要它自己的端点。这对于MSMQ来说是一个更大的交易,所以也许我们可以让这个案例在RabbitMQ上工作 - 但这不是我们在这一点上花了很多心思。
答案 1 :(得分:1)
正在发生的事情是,在给同一个Receiver Uri时,你告诉MT在两条总线上加载平衡消耗,但是你只有一条总线在监听这些消息。
如果你想跟踪收到哪些消息,你会看到它(几乎)每隔一秒消息。
调整了示例代码后,我得到了
We consumed 6 simple messages. Press Enter to terminate the applicaion.
Received 0
Received 3
Received 5
Received 6
Received 7
Received 8
在另一辆公共汽车上启动消费者,你将全部获得
We consumed 10 simple messages. Press Enter to terminate the applicaion.
Received 0
Received 1
Received 2
Received 3
Received 4
Received 5
Received 6
Received 7
Received 8
Received 9
所以是的,我会说这是预期的行为。
这是带有两个订阅者的调整示例代码
using MassTransit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MTMessageLoss
{
class Program
{
internal static bool[] msgReceived = new bool[10];
static void Main(string[] args)
{
var consumerBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
var publisherBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
publisherBus.SubscribeConsumer(() => new MessageConsumer());
consumerBus.SubscribeConsumer(() => new MessageConsumer());
for (int i = 0; i < 10; i++)
consumerBus.Publish(new SimpleMessage()
{CorrelationId = Guid.NewGuid(), MsgId = i});
Console.WriteLine("Press ENTER Key to see how many you consumed");
Console.ReadLine();
Console.WriteLine("We consumed {0} simple messages. Press Enter to terminate the applicaion.",
MessageConsumer.Count);
for (int i = 0; i < 10; i++)
if (msgReceived[i])
Console.WriteLine("Received {0}", i);
Console.ReadLine();
consumerBus.Dispose();
publisherBus.Dispose();
}
}
public interface ISimpleMessage : CorrelatedBy<Guid>
{
int MsgId { get; }
}
public class SimpleMessage : ISimpleMessage
{
public Guid CorrelationId { get; set; }
public int MsgId { get; set; }
}
public class MessageConsumer : Consumes<ISimpleMessage>.All
{
public static int Count = 0;
public void Consume(ISimpleMessage message)
{
Program.msgReceived[message.MsgId] = true;
System.Threading.Interlocked.Increment(ref Count);
}
}
}