The main goal to have several hosts of RabbiMQ servers (clustering) Are there any best practices to implement having several RabbitMQ hosts, and reconnect to the next one in case previous connection closed.
Tutorial says that:
A client can connect as normal to any node within a cluster. If that node should fail, and the rest of the cluster survives, then the client should notice the closed connection, and should be able to reconnect to some surviving member of the cluster. Generally, it's not advisable to bake in node hostnames or IP addresses into client applications
How it can be implemented from the client side ?
One way to solve this typical problem is configure an load-balancer (it's enough in roud-robin configuration ) for your cluster (like HAPROXY, cross-road ..or others) .
client(s)--->ip-load-balacer--brokerS
In this way you can use only the load-balancer IP in your client(s) connection.
For having more flexibility you can add a DNS (local dns) bound to the load balancer, in this way you can also change the balancer without change the client configuration.
client(s)--->-load-balacer-DNSNAME--brokerS
If one client lose the connection you have to re-coonect it to the same ip or dns.
You should have an HA client for c# like this for java, or simply manage the connection shutdown event.
Say that, if you have a small and static cluster you could use the IP(s) from the client side.
From the client side (if you want handle only the disconnect):
connection = connection = factory.CreateConnection();
channel = connection.CreateModel();
.......
connection.ConnectionShutdown += Connection_ConnectionShutdown;
void Connection_ConnectionShutdown(IConnection connection, ShutdownEventArgs reason)
{
Console.WriteLine("connection_ConnectionShutdown " + reason.ToString());
Reconnect_client();
}
来源:https://stackoverflow.com/questions/23010908/rabbitmq-client-connect-to-several-hosts