Rabbitmq - Connecting to cluster from C#

2,535 views
Skip to first unread message

Devrath N D

unread,
Sep 29, 2016, 6:37:01 AM9/29/16
to rabbitmq-users
We have created a Rabbitmq cluster with two nodes(rabbit and rabbit1). We have 4 queues which are configured to be highly available queues by following http://www.rabbitmq.com/clustering.html and http://www.rabbitmq.com/ha.html

Before clustering we used to connect to the node using the below snippet

var factory = new ConnectionFactory(){ HostName = _rabbitMQ_Hostname, UserName = _rabbitMQ_Username, Password = _rabbitMQ_Password};


using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
    channel
.QueueDeclare(queue: _autoCancellationPNS_QueueName,
    durable
: true,
    exclusive
: false,
    autoDelete
: false,
    arguments
: null);


   
string message = appointmentId.ToString();
   
var body = Encoding.UTF8.GetBytes(message);
   
IBasicProperties properties = channel.CreateBasicProperties();
    properties
.DeliveryMode = 2;
    channel
.BasicPublish(exchange: _rabbitMQ_Exchange,
        routingKey
: _autoCancellationPNS_RoutingKey,
        basicProperties
: properties,
        body
: body);
    returnMessage
.ShortMessage = "Added to queue";
    returnMessage
.LongMessage = "Added to queue";
    logger
.Debug("|Added to queue");
}

How should we deal with cluster?

Karl Nilsson

unread,
Sep 29, 2016, 7:07:31 AM9/29/16
to rabbitm...@googlegroups.com
Hi,

The CreateConnection method on the ConnectionFactory has an overload that allows you to pass a list of hostnames to try. Alternatively you could put a load balancer in front of the cluster nodes so that clients only has a single location to connect to.

Cheers
Karl

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Nilsson

Staff Software Engineer, Pivotal/RabbitMQ

Mal Clarke

unread,
Jul 5, 2017, 10:10:52 AM7/5/17
to rabbitmq-users
How does that work when you set the hostname in the ConnectionFactory? 

What is the best way to connect to a cluster and pick which node is available? Currently, I have a try catch, picking up the next one in the list until I get success. Doesn't seem efficient. 

Also how to deal with a connection to a node that goes down. Again I have that in a try catch, and attempt to connect to the next one in the List<> but again is that the right way to do it? Doesn't quite sit right with me?

Thanks

Mal
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Karl Nilsson

unread,
Jul 5, 2017, 10:48:42 AM7/5/17
to rabbitm...@googlegroups.com
On 5 July 2017 at 15:10, Mal Clarke <mal.cl...@gmail.com> wrote:
How does that work when you set the hostname in the ConnectionFactory? 

It ignores the value of the hostname property.
 

What is the best way to connect to a cluster and pick which node is available? Currently, I have a try catch, picking up the next one in the list until I get success. Doesn't seem efficient. 

Use the CreateConnection overload as previously suggested.
 

Also how to deal with a connection to a node that goes down. Again I have that in a try catch, and attempt to connect to the next one in the List<> but again is that the right way to do it? Doesn't quite sit right with me?


If you used the CreateConnection overload it will pick a random node during retries.

 
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Karl Nilsson

Pivotal/RabbitMQ

Mal Clarke

unread,
Jul 6, 2017, 4:38:05 AM7/6/17
to rabbitmq-users

Just to point out for anyone reading, this only seems to work with the latest (4.1.3).

Another question, if the first node is down there is a lengthy wait to connect to the second node. How can this be sped up?

 connection = factory.CreateConnection(new List<string>
            {
                "172.17.0.2",     <----- down
                "172.17.0.3"      <----- 10 seconds to connect to this one
            });

Also, if a node goes down  _consumer.Queue.Dequeue(); fails with an exception. Is there a way to have it automatically switch to other nodes in the CreateConnection list?

Thanks
Karl Nilsson

Pivotal/RabbitMQ

Karl Nilsson

unread,
Jul 6, 2017, 5:18:59 AM7/6/17
to rabbitm...@googlegroups.com
I'd recommend not using the deprecated QueuingBasicConsumer and instead use EventingBasicConsumer which should work better with auto-recovery.

The connection recovery is executed in response to the ConnectionClosed event. How quickly it detects that the connection is down would depend on whether it was trying to write at the time, how the remote node became unavailable and the configured heartbeat interval.

Cheers
Karl

To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Karl Nilsson

Pivotal/RabbitMQ

Michael Klishin

unread,
Jul 6, 2017, 5:21:54 AM7/6/17
to rabbitm...@googlegroups.com
More about heartbeats and why they exist: http://www.rabbitmq.com/heartbeats.html.
MK

Staff Software Engineer, Pivotal/RabbitMQ

Mal Clarke

unread,
Jul 10, 2017, 4:49:46 AM7/10/17
to rabbitm...@googlegroups.com
Here is the scenario I am trying to recreate. 

Let's say I have two rabbitmq servers. Rabbit1 and Rabbit 2.

In a publish/subscribe scenario, I have several subscribers. Some subscribe to Rabbit1 and some to Rabbit2 using the create connection overload. Now, this is not a problem as both queues are configured through policies to be high availability.

Now, Rabbit2 dies. published messages no longer reach the subscribers attached to Rabbit 2.

How can I configure/code the subscriber that if it's chosen server is no longer available it goes and seeks out another available server? Thus not missing out on messages.

Many thanks in advance.

  
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-user...@googlegroups.com.

To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Mal.

Mal Clarke

unread,
Jul 10, 2017, 6:04:17 AM7/10/17
to rabbitm...@googlegroups.com
Auto recovery is working but with a long delay between losing one server and finding an alternative. How can I decrease this wait, perhaps to a second
--

Mal.

Michael Klishin

unread,
Jul 10, 2017, 11:08:36 AM7/10/17
to rabbitm...@googlegroups.com
Start with http://rabbitmq.com/heartbeats.html. We DO NOT recommend setting heartbeat timeouts
to values lower than 5 seconds.

Automatic connection recovery by default attempts to reconnect
every 5 seconds (no relation to the 5 second value above).
This can be quite safely reduced to, say, half a second.



To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.

To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Karl Nilsson

Pivotal/RabbitMQ

--
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Mal.

--

Mal.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Mal Clarke

unread,
Jul 10, 2017, 11:19:18 AM7/10/17
to rabbitm...@googlegroups.com
Thanks for your reply.

Can you let me know what settings will reduce the connection time? I have played around with the settings to no avail

 return new ConnectionFactory
            {
                Port = AmqpTcpEndpoint.UseDefaultPort,
                UserName = "guest",
                Password = "guest",
                VirtualHost = "/",
                RequestedConnectionTimeout = 500,
                AutomaticRecoveryEnabled = true,
                ContinuationTimeout = TimeSpan.FromMilliseconds(500),  
                NetworkRecoveryInterval = TimeSpan.FromMilliseconds(500),
                RequestedHeartbeat = 5
            };

Thanks


You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-user...@googlegroups.com.

To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Mal.

--

Mal.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

--
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-user...@googlegroups.com.

To post to this group, send email to rabbitm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Mal.

Michael Klishin

unread,
Jul 10, 2017, 12:12:17 PM7/10/17
to rabbitm...@googlegroups.com
RequestedHeartbeat controls the heartbeat timeout.
NetworkRecoveryInterval controls delays between recovery retries (including the initial one).

RabbitMQ .NET client cannot control other factors that can delay successful connection:

 * Hostname resolution (using DNS or other means), including possible timeouts, e.g. caused by VPN (VPNs alter DNS configuration)
 * TCP connection time (several kernel and firewall/proxy settings can affect this)
 * Network latency
 * Authentication backends taking time to authenticate and authorize a connection (http://www.rabbitmq.com/access-control.html)

and so on.

Use Wireshark to find out when TCP connections are really closed and opened:

Some server-side values that are relevant are covered in http://www.rabbitmq.com/networking.html.


To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.

To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Karl Nilsson

Pivotal/RabbitMQ

--
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Mal.

--

Mal.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ

--
You received this message because you are subscribed to a topic in the Google Groups "rabbitmq-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rabbitmq-users/dY87DgR52Uk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Mal.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages