本文介绍了如何覆盖MassTransit默认交换和队列拓扑约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在[关于SO的我的问题之一]中指出的那样(),MassTransit for RabbitMQ自动为给定的简单配置创建一定数量的队列并进行交换:

As pointed out [in one of my questions on SO] (Why a simple configuration in MassTransit creates 2 queues and 3 exchanges?), MassTransit for RabbitMQ creates automatically a certain number of queues and exchange for a given simple configuration:

  • ConsoleApp1:Program-YourMessage :耐用
  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt :自动删除且持久吗?
  • test_queue :耐用
  • ConsoleApp1:Program-YourMessage: Durable
  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt: Auto-delete and Durable?
  • test_queue: Durable

队列:

  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt :x-expire 60000
  • test_queue :耐用
  • VP0003748_dotnet_bus_6n9oyyfzxhyx9ybobdmpj8qeyt: x-expire 60000
  • test_queue: Durable

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

However, I found it a bit frustration to not be able to override the naming of those exchanges and queues. Is there anything I can do to change that?

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

For example, if you refactor some type or a namespace you may endup polluting your RabbitMQ instance with tons of exchanges that are no longer used =/

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

I understand test_queue cause this is something I decided so fair enough.Types are easily subject to changes / refactoring.

推荐答案

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

This is a simple and effective way to do it: https://bartwullems.blogspot.com/2018/09/masstransitchange-exchange-naming.html

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

But better to drop some dotnet core code here, to help anyone that's starting.

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

Our configuration based custom formatter:

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

    public BusEnvironmentNameFormatter(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}";
    }
}

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

Then to use it, we would do something like this:

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 BusEnvironmentNameFormatter(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);
    }));
});

这篇关于如何覆盖MassTransit默认交换和队列拓扑约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:51