开启心跳 LVS的心跳超时时间 RabbitMQ集群实例使用LVS进行负载均衡,如图1所示,单节点实例不涉及LVS。 图1 集群实例的负载均衡 LVS对客户端连接设置了心跳超时时间,默认为90秒。如果客户端在90秒内没有向LVS发送心跳(AMQP心跳帧或消息收发),LVS会主动断开与客户端的连接,此时客户端需要重新连接。 如果存在消息收发时间间隔大于90秒的场景,请在客户端开启心跳并设置小于90秒的心跳超时时间。 客户端如何配置心跳超时时间 在Java客户端配置心跳超时时间。 在创建连接前使用ConnectionFactory setRequestedHeartbeat进行设置,示例如下: ConnectionFactory cf new ConnectionFactory(); // 将心跳超时时间设置为60秒 cf.setRequestedHeartbeat(60); 在.NET客户端配置心跳超时时间,示例如下。 var cf new ConnectionFactory(); // 将心跳超时设置为60秒 cf.RequestedHeartbeat TimeSpan.FromSeconds(60); 在Python pika客户端配置心跳超时时间,示例如下。 设置心跳时间为60秒 params pika.ConnectionParameters(host'host', heartbeat60, credentialspika.PlainCredentials('username', 'passwd')) connection pika.BlockingConnection(params) while True: channel.basicpublish(exchange'', routingkey'hello', body'Hello World!') print(" [x] Sent 'Hello World!'") 生产者需要使用connection.sleep()才能触发心跳,使用time.sleep()不会触发心跳 connection.sleep(200)