如何更改 MassTransit for RabbitMQ 中的错误队列和交换的名称?

问题描述 投票:0回答:3

我尝试在一个简单的配置中抛出一些异常,并注意到创建了一些与错误相关的交换和一个队列:

交流:

  • MassTransit:Fault
  • MassTransit:Fault--ConsoleApp1:Program-YourMessage--

队列:

  • ReceiveEndPointQueue_error

如何更改或自定义上述名称?

Program.cs

public static class Program
{
    public class YourMessage
    {
        public string Text { get; set; }
    }

    public class MyMessage
    {
        public int Number { get; set; }
    }

    public static async Task Main(params string[] args)
    {
        var bus = Bus.Factory.CreateUsingRabbitMq(configuration =>
        {
            configuration.OverrideDefaultBusEndpointQueueName("DefaultBusEndpointQueue");
            configuration.Message<YourMessage>(x =>
            {
                x.SetEntityName("YourMessageEntity");
            });
            configuration.Message<MyMessage>(x =>
            {
                x.SetEntityName("MyMessageEntity");
            });


            var host = configuration.Host(new Uri("rabbitmq://localhost"), h =>
            {
            });
            configuration.ReceiveEndpoint(host, "ReceiveEndPointQueue", ep =>
            {
                ep.Handler<YourMessage>(async context => throw new Exception("YourMessage"));
                ep.Handler<MyMessage>(async context => await Console.Out.WriteLineAsync($"Received MyMessage: {context.Message.Number}"));
            });
        });

        await bus.StartAsync(); 
        await bus.Publish(new YourMessage{Text = "Hi"});
        await bus.Publish(new MyMessage {Number= 42});
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
        await bus.StopAsync();
    }
}
c# .net-core rabbitmq masstransit
3个回答
1
投票

直接引用https://gitter.im/MassTransit/MassTransit

Ehouarn Perret
@ehouarn-perret
May 30 20:14
"I am still looking for a way to change the default names of Error Exchanges / Queues when using MassTransit with RabbitMQ, can't find anything in the documentation"

Chris Patterson
@phatboyg
May 30 20:31
"There isnt' a way to change them, it's always queue_error"

0
投票

有一种方法可以更改错误队列名称,尽管不太优雅:

        Bus.Factory.CreateUsingRabbitMq(cfg =>
        {
            cfg.SendTopology.ConfigureErrorSettings = settings =>
            {
                if (settings is RabbitMqErrorSettings rabbitMqErrorSettings)
                {
                    rabbitMqErrorSettings.QueueName = "Your queue name";
                }
            };
        });

应该记住,这不会改变现有的拓扑。如果创建了具有默认名称的队列,它将保留相同的绑定。


0
投票

鉴于这是谷歌上的第一名结果,这就是您可以更改上述问题示例中的默认故障交换名称的方法:


public static class Program
{
    public class YourMessage
    {
        public string Text { get; set; }
    }

    public class MyMessage
    {
        public int Number { get; set; }
    }

    public static async Task Main(params string[] args)
    {
        var bus = Bus.Factory.CreateUsingRabbitMq(configuration =>
        {
            configuration.OverrideDefaultBusEndpointQueueName("DefaultBusEndpointQueue");
            configuration.Message<YourMessage>(x =>
            {
                x.SetEntityName("YourMessageEntity");
            });
            configuration.Message<MyMessage>(x =>
            {
                x.SetEntityName("MyMessageEntity");
            });
            
            // customization of fault starts here
            configuration.Message<Fault>(x =>
            {
                x.SetEntityName("Fault");
            });

            configuration.Message<Fault<MyMessageEntity>>(x =>
            {
                x.SetEntityName("FaultMyMessageEntity");
            });
            
            configuration.Message<Fault<YourMessageEntity>>(x =>
            {
                x.SetEntityName("FaultYourMessageEntity");
            });
            // customization of fault ends here            

            var host = configuration.Host(new Uri("rabbitmq://localhost"), h =>
            {
            });
            configuration.ReceiveEndpoint(host, "ReceiveEndPointQueue", ep =>
            {
                ep.Handler<YourMessage>(async context => throw new Exception("YourMessage"));
                ep.Handler<MyMessage>(async context => await Console.Out.WriteLineAsync($"Received MyMessage: {context.Message.Number}"));
            });
        });

        await bus.StartAsync(); 
        await bus.Publish(new YourMessage{Text = "Hi"});
        await bus.Publish(new MyMessage {Number= 42});
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
        await bus.StopAsync();
    }
}

_error
队列,我还没有找到更改它的方法,但它将遵循接收端点名称加上
_error

https://masstransit.io/documentation/configuration/topology/message

© www.soinside.com 2019 - 2024. All rights reserved.