如何覆盖MassTransit默认交换和队列拓扑约定?

时间:2019-05-10 09:30:10

标签: c# .net-core rabbitmq masstransit

正如[在关于SO的我的一个问题](Why a simple configuration in MassTransit creates 2 queues and 3 exchanges?中指出的那样,MassTransit for RabbitMQ自动创建一定数量的队列并交换给定的简单配置:

  

交换,所有扇出:

     
      
  • ConsoleApp1:Program-YourMessage:持久
  •   
  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt:自动删除且持久吗?
  •   
  • test_queue:持久
  •   
     

队列:

     
      
  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt:x过期60000
  •   
  • test_queue:持久
  •   

但是,我发现无法覆盖那些交换和队列的命名有点沮丧。我有什么办法可以改变这种情况?

例如,如果您重构某种类型或名称空间,则可能最终导致大量的不再使用的交换污染您的RabbitMQ实例= /

我了解test_queue,因为这是我认为足够公平的事情。 类型很容易更改/重构。

2 个答案:

答案 0 :(得分:1)

这是一种简单有效的方法:https://bartwullems.blogspot.com/2018/09/masstransitchange-exchange-naming.html

但是最好在此处放置一些dotnet核心代码,以帮助任何刚开始的人。

我们基于配置的自定义格式器:

public class BusEnvironmentNameFormatter : IEntityNameFormatter
    {
        private readonly IEntityNameFormatter _original;
        private readonly string _prefix;

        public ServiceBusEnvironmentNameFormatter(IEntityNameFormatter original, SomeAppSettingsSection busSettings)
        {
            _original = original;
            _prefix = string.IsNullOrWhiteSpace(busSettings.Environment)
                ? string.Empty // no prefix
                : $"{busSettings.Environment}:"; // custom prefix
        }

        // Used to rename the exchanges
        public string FormatEntityName<T>()
        {
            var original = _original.FormatEntityName<T>();
            return Format(original);
        }

        // Use this one to rename the queue
        public string Format(string original)
        {
            return string.IsNullOrWhiteSpace(_prefix)
                ? original
                : $"{_prefix}{original}";
        }
    }

然后使用它,我们将执行以下操作:

            var busSettings = busConfigSection.Get<SomeAppSettingsSection>();
            var rabbitMqSettings = rabbitMqConfigSection.Get<SomeOtherAppSettingsSection>();

            services.AddMassTransit(scConfig =>
            {
                scConfig.AddConsumers(consumerAssemblies);

                scConfig.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(rmqConfig =>
                {
                    rmqConfig.UseExtensionsLogging(provider.GetRequiredService<ILoggerFactory>());

                    // Force serialization of default values: null, false, etc
                    rmqConfig.ConfigureJsonSerializer(jsonSettings =>
                    {
                        jsonSettings.DefaultValueHandling = DefaultValueHandling.Include;
                        return jsonSettings;
                    });

                    var nameFormatter = new ServiceBusEnvironmentNameFormatter(rmqConfig.MessageTopology.EntityNameFormatter, busSettings);
                    var host = rmqConfig.Host(new Uri(rabbitMqSettings.ConnectionString), hostConfig =>
                    {
                        hostConfig.Username(rabbitMqSettings.Username);
                        hostConfig.Password(rabbitMqSettings.Password);
                    });

                    // Endpoint with custom naming
                    rmqConfig.ReceiveEndpoint(host, nameFormatter.Format(busSettings.Endpoint), epConfig =>
                    {
                        epConfig.PrefetchCount = busSettings.MessagePrefetchCount;
                        epConfig.UseMessageRetry(x => x.Interval(busSettings.MessageRetryCount, busSettings.MessageRetryInterval));
                        epConfig.UseInMemoryOutbox();

                        //TODO: Bind messages to this queue/endpoint
                        epConfig.MapMessagesToConsumers(provider, busSettings);
                    });

                    // Custom naming for exchanges
                    rmqConfig.MessageTopology.SetEntityNameFormatter(nameFormatter);
                }));
            });

答案 1 :(得分:1)

可以通过以下方式使用IRabbitMqBusFactoryConfigurator的OverrideDefaultBusEndpointQueueName方法更改队列名称

library(dplyr)

df %>%
  filter(CallStatus == "Busy" | lag(CallStatus) == "Busy") %>%
  group_by(CallHandler, gr = cumsum(lag(CallStatus != "Busy", default = TRUE))) %>%
  summarise(StartTime = first(CallTime), 
            EndTime = last(CallTime)) %>%
  mutate(EndTime = replace(EndTime, StartTime == EndTime, NA)) %>%
  select(-gr)


# CallHandler StartTime         EndTime        
# <fct>       <fct>             <fct>          
#1 A           01/01/2020 00:05 01/01/2020 00:10
#2 A           01/01/2020 01:30 01/01/2020 02:50
#3 A           01/01/2020 02:55 01/01/2020 04:55
#4 A           01/01/2020 05:25 01/01/2020 05:55
#5 A           01/01/2020 06:25 NA