【问题标题】:MassTransit RabbitMq Sending MessagesMassTransit RabbitMq 发送消息
【发布时间】:2020-02-17 16:56:03
【问题描述】:

我无法弄清楚在发送/发布消息时如何在我的GetSendEndpoint()) 任务中指定ExchangeQueue

根据 MassTransit 文档https://masstransit-project.com/usage/producers.html#send,您可以像这样指定交换和队列

GetSendEndpoint(new Uri("queue:input-queue"))

但是,我只能做其中一个吗?

是否有指定交换和队列的替代发送方式?

我在 Asp.Net Core 中执行此操作,所以这是我的配置:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   services.AddMassTransit();

   services.AddSingleton(p => Bus.Factory.CreateUsingRabbitMq(cfg =>
   {
         cfg.Host("rabbitmq://localhost", h =>
         {
             h.Username("admin");
             h.Password("admin");
         });
   }));

   services.AddSingleton<IBus>(p => p.GetRequiredService<IBusControl>());
   services.AddSingleton<IHostedService, BusService>();
}

这就是发送消息的方式

var endpoint = await _bus.GetSendEndpoint(new Uri(queue:Test.Queue));
await endpoint.Send(new Message()
{
     Text = "This is a test message"
});

如您所见,我只能指定队列名称。

【问题讨论】:

    标签: c# rabbitmq masstransit


    【解决方案1】:

    如果您指定交易所,则仅在代理上声明交易所。消息将直接发送到交易所。

    "exchange:your-exchange-name"
    

    如果你指定了一个队列,这个队列会连同一个同名的交换器一起被声明并且交换器将被绑定到队列中。消息将被传递到同名的交换器,该交换器将它们传递到队列。

    "queue:your-queue-name"
    

    如果您想要不同的交换和队列名称,您可以使用以下命令同时指定:

    "exchange:your-exchange-name?bind=true&queue=your-queue-name"
    

    或者你可以简化,但是两个队列有点混乱:

    "queue:your-exchange-name&queue=your-queue-name"
    

    【讨论】:

    • 谢谢克里斯,我知道这一点。但是有没有办法像使用 RabbitMq 客户端一样通过指定 Exchange 然后 Queue 来发送它?
    • 老实说,我不明白你的意思。在 RabbitMQ 中直接发送到队列的唯一方法是指定一个空交换 ("") 并将队列名称放在 RoutingKey 中。 MassTransit 并没有公开该功能。
    • exchange:your-exchange-name?bind=true&amp;queue=your-queue-name 这符合我的目的 :) 谢谢
    • Chris 还确认是否可以在发布者将消息发送到 Exchange 时执行Acknowledgement。主要是确认消息是否被传递到队列中。
    【解决方案2】:

    在此处阅读 Chris 的回复 Mass Transit : No consumer

    好像Exchanges are created by MassTransit when publishing messages, based on the message types. Publishing does not create any queues. Queues are where messages are stored for delivery to consumers.

    Queues are created when receive endpoints are added to a bus. For the consumers, handlers, and sagas added to a receive endpoint, the exchanges are created and bound so that messages published to the exchanges are received by the receive endpoint (via the queue).

    所以如果我的发布者没有定义接收端点,那么我发送的任何消息都会丢失,因为没有队列或绑定?

    进一步阅读这里https://groups.google.com/forum/#!topic/masstransit-discuss/oVzZkg1os9o 似乎进一步证实了这一点。

    因此,基于上面的链接,为了实现我想要的,即创建交换并将其绑定到队列,我需要在 Uri 中指定它

    var sendEndpoint = bus.GetSendEndpoint(new Uri("rabbitmq://localhost/vhost1/exchange1?bind=true&queue=queue1"));
    

    exchange1 是 Exchange,queue1 是队列,bind=true 会将队列绑定到交换机。

    如果坚持最初的 MT 设计,是否需要先运行消费者才能设置交换和队列,然后生产者才能开始发布?这似乎减少了发布者的灵活性?

    【讨论】:

    • 当我意识到您要求为交换和队列指定不同的名称后,我更新了我的答案。我没有从你原来的问题中收集到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多