Search code examples
c#rabbitmqmicroservicesmasstransit

MassTransit configuration issue with a RabbitMQ cluster


I have a RabbitMQ cluster xx.xx.xx.xx6, xx.xx.xx.xx.xx7 and xx.xx.xx.xx8. I'm trying to register them with the UseCluster method:

busConfigurator.UsingRabbitMq((context, configurator) =>
                {
                    var settings = context.GetRequiredService<MessageBrokerSettings>();

                    if (settings.UseMultiNodes)
                    {
                        configurator.Host(settings.VirtualHost,hostConfig =>
                        {
                            hostConfig.Username(settings.UserName);
                            hostConfig.Password(settings.Password);
                        
                            hostConfig.UseCluster(cluster =>
                            {
                                foreach (var host in settings.Hosts)
                                {
                                    cluster.Node(host);
                                }
                            });
                        });
                    }
                });
            });

I understand that using HAProxy infront of my cluster would make more sense and be much better but that is not an option now so I would like to make use of this feature in MassTransit (UseCluster). When running the application, my entire application fails to run due to this error:

Unhandled exception. System.ArgumentException: Invalid node address: amqp://xx.xx.xx.xx6:5672 (Parameter 'address').

Bare in mind that probably nothing is wrong with my RabbitMQ cluster because everything worked when using the RabbitMQ.Client library but MassTransit is causing the issue when trying to migrate to MassTransit. That is what I think where the problem is at least.


Solution

  • Don't include the scheme in the host address, it should only include:

    <hostname|ip address>[:port]
    

    If the default port is used, the port is not required.

    Update

    For your virtual host, it's the second parameter on the .Host call, not the first.

    configurator.Host("cluster-host-name", settings.VirtualHost,hostConfig =>
    

    The cluster-host-name can be anything, since the host name is display-only when using cluster nodes.