主要目标是拥有多个 RabbiMQ 服务器主机 ( clustering ) 是否有任何最佳实践来实现拥有多个 RabbitMQ 主机,并在上一个连接关闭的情况下重新连接到下一个主机。
教程说:
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
如何从客户端实现?
最佳答案
解决这个典型问题的一种方法是为您的集群配置一个负载均衡器(在 roud-robin 配置中就足够了)(例如 HAPROXY 、 cross-road ..或其他)。
client(s)--->ip-load-balacer--brokerS
通过这种方式,您可以在客户端连接中仅使用负载均衡器 IP。
为了获得更大的灵活性,您可以添加绑定(bind)到负载均衡器的 DNS(本地 dns),这样您也可以在不更改客户端配置的情况下更改均衡器。
client(s)--->-load-balacer-DNSNAME--brokerS
如果一个客户端失去连接,您必须将其重新连接到相同的 IP 或 DNS。
您应该有一个适用于 C# 的 HA 客户端,例如 this对于java,或者只是管理连接关闭事件。
也就是说,如果您有一个小型静态集群,您可以使用客户端的 IP。
从客户端(如果您只想处理断开连接):
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();
}
关于c# - RabbitMQ 客户端连接到多个主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010908/