MassTransit RabbitMq 发送消息

KJS*_*JSR 4 c# masstransit rabbitmq

在发送/发布消息时,我无法弄清楚如何在我的任务中指定Exchange和?QueueGetSendEndpoint())

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

GetSendEndpoint(new Uri("queue:input-queue"))
Run Code Online (Sandbox Code Playgroud)

但是,我只能做一个或另一个?

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

我在 Asp.Net Core 中这样做,所以这是我的配置:

启动文件

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>();
}
Run Code Online (Sandbox Code Playgroud)

这就是发送消息的方式

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

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

KJS*_*JSR 6

阅读克里斯在这里的回应公共交通:没有消费者

这好像是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"));
Run Code Online (Sandbox Code Playgroud)

其中exchange1是 Exchange,queue1是 Queue,bind=true并将队列绑定到 Exchange。

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


Chr*_*son 5

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

"exchange:your-exchange-name"
Run Code Online (Sandbox Code Playgroud)

如果你指定一个队列,队列和一个同名的交换将被声明,交换将被绑定到队列。消息将被传递到同名的交换,后者将它们传递到队列中。

"queue:your-queue-name"
Run Code Online (Sandbox Code Playgroud)

If you want a different exchange and queue name, you can specify both using:

"exchange:your-exchange-name?bind=true&queue=your-queue-name"
Run Code Online (Sandbox Code Playgroud)

Or you could simplify, but it's a little confusing with two queues:

"queue:your-exchange-name&queue=your-queue-name"
Run Code Online (Sandbox Code Playgroud)