c# - RabbitMQ 客户端连接到多个主机

标签 c# rabbitmq failover failovercluster

主要目标是拥有多个 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 配置中就足够了)(例如 HAPROXYcross-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/

相关文章:

c# - 将 .net 类库移植到 Windows 应用商店应用程序类库

c# - 检测并处理应用程序突然结束任务、重启和关闭?

hadoop - 分布式系统-主服务器故障

spring-boot - 相当于 RabbitMq Listener 的 Servlet Filter 是什么?

ActiveMQ、STOMP、Java 示例

Amazon Cloud 上的 MySql 复制故障转移

c# - 从 XmlElement 创建 XPathDocument

c# - 多重等待什么时候有意义?

rabbitmq - 在 docker-compose 文件中提供 rabbitmq.conf 会得到 "sed: cannot rename/etc/rabbitmq/sedMaHqMa: Device or resource busy"

java - 多线程环境中 RabbitMQ 的性能问题