MassTransit RabbitMq 发送消息
MassTransit RabbitMq Sending Messages
我无法弄清楚在发送/发布消息时如何在我的 GetSendEndpoint())
任务中指定 Exchange
和 Queue
?
根据 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"
});
如您所见,我只能指定队列名称。
如果您指定一个交易所,则只有该交易所在经纪人上声明。消息将直接发送到交易所。
"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"
在这里阅读克里斯的回复
好像是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 似乎进一步证实了这一点。
所以基于以上 link 为了实现我想要的,即创建交换并将其绑定到队列,我需要在 Uri
中指定它
var sendEndpoint = bus.GetSendEndpoint(new Uri("rabbitmq://localhost/vhost1/exchange1?bind=true&queue=queue1"));
其中 exchange1
是交换,queue1
是队列,bind=true
会将队列绑定到交换。
如果坚持原来的 MT 设计,消费者需要 运行 在生产者开始发布之前设置交换和队列?这似乎降低了发布商的灵活性?
我无法弄清楚在发送/发布消息时如何在我的 GetSendEndpoint())
任务中指定 Exchange
和 Queue
?
根据 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"
});
如您所见,我只能指定队列名称。
如果您指定一个交易所,则只有该交易所在经纪人上声明。消息将直接发送到交易所。
"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"
在这里阅读克里斯的回复
好像是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 似乎进一步证实了这一点。
所以基于以上 link 为了实现我想要的,即创建交换并将其绑定到队列,我需要在 Uri
中指定它
var sendEndpoint = bus.GetSendEndpoint(new Uri("rabbitmq://localhost/vhost1/exchange1?bind=true&queue=queue1"));
其中 exchange1
是交换,queue1
是队列,bind=true
会将队列绑定到交换。
如果坚持原来的 MT 设计,消费者需要 运行 在生产者开始发布之前设置交换和队列?这似乎降低了发布商的灵活性?