简体   繁体   English

MassTransit和RabbitMQ,使用DI时使队列在重新启动后幸存

[英]MassTransit and RabbitMQ, make queue survive restart when using DI

I'm trying to force the RabbitMQ queues to survive the Rabbit service restart, while forcing them to reuse the same name after the service restart is completed. 我试图强制RabbitMQ队列在Rabbit服务重新启动后继续生存,同时在服务重新启动完成后强制它们重新使用相同的名称。

We are using our DI container to pass the message consumers 我们正在使用我们的DI容器来传递消息使用者

return new List<Type>
{
    typeof(Consumer1), typeof(Consumer2), typeof(Consumer3), typeof(Consumer4)
};

That collection is then passed to the configuration via 然后,通过以下方式将该集合传递给配置

t => _kernel.Get(t));

And then registering them this way 然后以这种方式注册

cfg.ReceiveEndpoint(host, e =>
{
    foreach (var type in consumerTypes)
    {
        e.Consumer(type, getConsumer);
    }
});

This makes the queue to be recreated at every restart, with a GUID in the name. 这样就可以在每次重新启动时重新创建队列,并在其名称中带有GUID。

If we try to do this: 如果我们尝试这样做:

cfg.ReceiveEndpoint(host, e =>
{
    foreach (var type in consumerTypes)
    {
        e.Consumer(type, getConsumer);
        e.Durable = true;
        e.AutoDelete = false;
    }
});

Then the queue is durable, but is not created upon service restart. 然后,该队列是持久的,但不会在服务重新启动时创建。

How can we specify a queue name for each consumer? 我们如何为每个使用者指定队列名称?

Solved doing this: 解决了这样做:

foreach (var type in consumerTypes)
{
    var consumerType = type;
    var queueName = string.Format("{0}_{1}",
        Environment.MachineName, consumerType.ToString());
    cfg.ReceiveEndpoint(host, queueName,
        e =>
        {
            e.Consumer(consumerType, getConsumer);
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM