为发送端点配置死信队列

Configure DeadLetter queue for send endpoint

我正在尝试将生产者配置为向配置了死信队列的消费者发送消息。生产者正在使用 SendEndpoint(或者更确切地说是 request/response 模式),但我从 RabbitMQ 得到一个例外。

我有以下消费者:

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

    services.AddMassTransit(x =>
    {
        x.AddConsumer<SomeMessageRequestConsumer>();

        x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(busConfig =>
        {
            busConfig.Host(new Uri("rabbitmq://rabbit@localhost"), "/", hostConfigurator =>
            {
                hostConfigurator.Password("Guest");
                hostConfigurator.Username("Guest");
            });

            busConfig.ReceiveEndpoint(nameof(SomeMessage), x =>
            {
                x.ConfigureConsumer<SomeMessageRequestConsumer>(provider);
                x.Durable = false;
                x.ConfigureConsumeTopology = false;

                x.BindDeadLetterQueue("SomeMessageDeadLetter", "SomeMessageDeadLetter", null);
             });
          }));
       });

    services.AddMassTransitHostedService();
}

我有以下制作人:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSingleton<IReplyToClientFactory, ReplyToClientFactory>();

    services.AddMassTransit(x =>
    {
        x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(busConfig =>
        {
            busConfig.Host(new Uri("rabbitmq://rabbit@localhost"), "/", hostConfigurator =>
            {
                hostConfigurator.Password("Guest");
                hostConfigurator.Username("Guest");
            });
        }));
    });

    services.AddMassTransitHostedService();
}

在 Producer 项目中,我有一个发送消息的控制器,如下所示:

    public ProducerController(IReplyToClientFactory clientFactory)
    {
        this.clientFactory = clientFactory;
    }

    [HttpPost]
    public async Task<IActionResult> Post(CancellationToken cancellationToken)
    {
        var serviceAddress = new Uri($"queue:{nameof(SomeMessage)}?durable=false");
        var client = this.clientFactory.GetFactory().CreateRequestClient<SomeMessage>(serviceAddress);

        var (successResponse, failResponse) = await client.GetResponse<SomeMessageSuccessResponse, SomeMessageFailResponse>(new SomeMessage()
        {
            Text = "Hello",
        }, cancellationToken, TimeSpan.FromSeconds(5));

        return Ok();
    }

我在 RabbitMQ 上收到以下错误:

operation queue.declare caused a channel exception precondition_failed: inequivalent arg 'x-dead-letter-exchange' for queue 'SomeMessage' in vhost '/': received none but current is the value 'SomeMessageDeadLetter' of type 'longstr'

我尝试在发布、发送和消息拓扑上配置死信,但没有成功。我想做的事情是可行的还是我在这里追风?

您可以将目标地址从队列更改为交换器,以将生产者与消费者队列配置分离。要发送到交易所,请将您的地址格式更改为:

$"exchange:{nameof(SomeMessage)}"

这样一来,您无需了解队列配置即可发送请求。