How do I connect a C#/VB.NET client to a RabbitMQ cluster?

1,405 views
Skip to first unread message

Denis Abramov

unread,
Apr 17, 2015, 4:23:26 PM4/17/15
to rabbitm...@googlegroups.com
I am doing:

using RabbitMQ.Client;
...

itsMQConnectionFactory = New ConnectionFactory() With {.HostName = hostname, .Port = port, .UserName = user, .Password = pass}

itsMQConnection = itsMQConnectionFactory.CreateConnection()

itsMQChannel = itsMQConnection.CreateModel()

How do I connect a C# client to a RabbitMQ cluster consisting of hosts: rabbit1,rabbit2,rabbit3? I tried to pass "hostname" as "rabbit1,rabbit2,rabbit3" but that didn't work. How should this be done?

Thanks for the help,
Denis

Michael Klishin

unread,
Apr 17, 2015, 4:47:08 PM4/17/15
to rabbitm...@googlegroups.com, Denis Abramov
On 17 April 2015 at 23:23:28, Denis Abramov (da...@columbia.edu) wrote:
> How do I connect a C# client to a RabbitMQ cluster consisting
> of hosts: rabbit1,rabbit2,rabbit3? I tried to pass "hostname"
> as "rabbit1,rabbit2,rabbit3" but that didn't work. How should
> this be done?

A single connection is always to one node. You can either stick a load balancer in front of your
cluster (a fairly common thing to do) or use multiple connections.

Some clients, namely the Java one, support lists of hosts. That does not mean they connect to multiple
hosts at the same time, only that they can randomly pick a host to connect to when it has to re-connect.

It should be fairly easy to add exactly the same thing to .NET (in fact, we used to have it a while ago
before connection recovery was supported). It's not a priority for us at the moment but
we are happy to discuss and consider external contributions on GitHub. 
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Denis Abramov

unread,
Apr 17, 2015, 5:12:55 PM4/17/15
to rabbitm...@googlegroups.com, da...@columbia.edu
(1) So I am a little confused about what the point of clustering is as opposed to just keeping rabbit1,rabbit2 and rabbit3 as separate machines. Is the entire point of clustering that the exchanges are shared within the cluster?
 
(2) I think I know the answer to this (the very last question) but just throwing it out there... So let's say the Consumer is subscribed to a queue or exchange. What would happen if the rabbitmq broker that the user is connected to goes down. Is the consumer dead? or once I connect to another host in the cluster all my subscriptions will be resumed automatically? or do I need to keep track of all subscriptions and re-subscribe again if a failure occurs?

Michael Klishin

unread,
Apr 17, 2015, 5:33:47 PM4/17/15
to rabbitm...@googlegroups.com, Denis Abramov
On 18 April 2015 at 00:12:57, Denis Abramov (da...@columbia.edu) wrote:
> (1) So I am a little confused about what the point of clustering
> is as opposed to just keeping rabbit1,rabbit2 and rabbit3 as
> separate machines. Is the entire point of clustering that the
> exchanges are shared within the cluster?

vhosts, users, exchanges, bindings, queues, policies, and so on.

Queues can be mirrored (their content is replicated). Statistics database
collects metrics from the entire cluster and displays them in a single place.

> (2) I think I know the answer to this (the very last question) but
> just throwing it out there... So let's say the Consumer is subscribed
> to a queue or exchange. What would happen if the rabbitmq broker
> that the user is connected to goes down. Is the consumer dead?
> or once I connect to another host in the cluster all my subscriptions
> will be resumed automatically? or do I need to keep track of all
> subscriptions and re-subscribe again if a failure occurs?

Consumers only consume from queues.

Your client needs to be able to re-connect. If you use automatic recovery offered by the .NET  client, your topology
will be re-declared and consumers will be re-added. Again, the .NET client currently will attempt reconnecting
to the same host, so a load balancer or round-robin DNS is necessary for it to connect to a different machine.

The automatic recovery process is documented:
http://www.rabbitmq.com/dotnet-api-guide.html#connection-recovery

You can also roll your own, although most of the time it's not necessary: the procedure we have has
been battle tested in multiple clients for a few years by now.

Denis Abramov

unread,
Apr 17, 2015, 5:40:32 PM4/17/15
to rabbitm...@googlegroups.com, da...@columbia.edu
That's cool! So consumers will be rebuilt and queues will be rebuilt so picking a random server and re-establishing a connection and channel is easy for me. Thank you very much for your help...

Michael Klishin

unread,
Apr 17, 2015, 5:55:05 PM4/17/15
to rabbitm...@googlegroups.com, da...@columbia.edu
On Saturday, 18 April 2015 00:40:32 UTC+3, Denis Abramov wrote:

That's cool! So consumers will be rebuilt and queues will be rebuilt so picking a random server and re-establishing a connection and channel is easy for me. Thank you very much for your help...

Even channels will be re-opened. Please take a look at what automatic connection recovery does and give it
a try (stopping RabbitMQ for 10-20 seconds and bringing it back should be sufficient).

The only limitation it has is that any attempts to publish while the client is not connected will result in an exception.
So it's up to your app to delay publishes in that case and keep track of what's been delivered. Publisher confirms
is a feature that helps you do just that ;)

MK 

Denis Abramov

unread,
Apr 17, 2015, 5:58:39 PM4/17/15
to rabbitm...@googlegroups.com, da...@columbia.edu
One more question: What exception(s) should trigger a re-connect to some other server in my cluster? 

Michael Klishin

unread,
Apr 18, 2015, 8:46:40 AM4/18/15
to rabbitm...@googlegroups.com, Denis Abramov
On 18 April 2015 at 00:58:40, Denis Abramov (da...@columbia.edu) wrote:
> One more question: What exception(s) should trigger a re-connect
> to some other server in my cluster?

See https://github.com/rabbitmq/rabbitmq-dotnet-client/blob/master/projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs#L932-938.

Connection shutdown is triggered by basically 3 things:

 * An unhandled exception in the I/O loop
 * Connection.Close() invoked by the app
 * Server-sent connection.close (when the node wants to disconnect the  client)
Reply all
Reply to author
Forward
0 new messages