Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to specify multiple connection points in RMQ .NET?

Tags:

As the title states I would like to provided multiple URI's for the RMQ .NET ConnectionFactory in the hope that it will fail over automatically to the first available one, rather than being limited to a single URI.

Setup

Using the Docker Quickstart Terminal (part of Docker Toolbox) I have created four containers, each with it's on RMQ instance running on it. I have clustered the RMQ nodes as follows:

  • Downstream (node1 and node2)
  • Upstream (node3 and node4)

The Downstream nodes share a Federated Queue with the Upstream nodes to increase the throughput.

Use

I have written a simple console application in C# that will generate and publish messages to node4 (the primary upstream RMQ instance).

I would like to test the redundancy/automatic failover of my RMQ configuration as I have set the flags for AutomaticRecoveryEnabled(docs) and TopologyRecoveryEnabled(docs) and have the Federated Queue setup.

However, the ConnectionFactory provided by the RMQ .NET library doesn't appear to support specifying multiple URI's (docs). So I have had to manually code in the handling of switching between the nodes when they go down - I do this by catching the exception thrown when a node is no longer accessible then pinging all nodes to see which is active.

Is there a way to give ConnectionFactory multiple Rabbit endpoints so that it can failover automatically?

like image 306
Matt Stannett Avatar asked Apr 17 '16 23:04

Matt Stannett


1 Answers

If you look at ConnectionFactory it can create a connection with a list of hosts

    /// <summary>
    /// Create a connection using a list of hostnames. The first reachable
    /// hostname will be used initially. Subsequent hostname picks are determined
    /// by the <see cref="IHostnameSelector" /> configured.
    /// </summary>
    /// <param name="hostnames">
    /// List of hostnames to use for the initial
    /// connection and recovery.
    /// </param>
    /// <returns>Open connection</returns>
    /// <exception cref="BrokerUnreachableException">
    /// When no hostname was reachable.
    /// </exception>
    public IConnection CreateConnection(IList<string> hostnames)
    {
        return CreateConnection(hostnames, null);
    }

And by the way the below code:

ConnectionFactory factory = new ConnectionFactory();
factory.Uri = "amqp://user:pass@hostName:port/vhost";
IConnection conn = factory.CreateConnection();

Will call CreateConnection({UriHostname}) with a list of single element

If you need a connections list between different cluster (different vhost, username, password...), so the answer is no, you cannot do that with the pivotal client. If only the hostname change, I may works.

like image 94
Nicolas Labrot Avatar answered Sep 28 '22 03:09

Nicolas Labrot