我有两台服务器,称它们为A和B.B运行RabbitMQ,而A通过Kombu连接RabbitMQ。如果我在B上重启RabbitMQ,则kombu连接中断,并且不再传递消息。然后,我必须重置A上的进程以重新建立连接。有没有更好的方法,即即使重新启动RabbitMQ进程,Kombu还是有办法自动重新连接吗?
我的基本代码实现如下,提前感谢! :)
def start_consumer(routing_key, incoming_exchange_name, outgoing_exchange_name):
global rabbitmq_producer
incoming_exchange = kombu.Exchange(name=incoming_exchange_name, type='direct')
incoming_queue = kombu.Queue(name=routing_key+'_'+incoming_exchange_name, exchange=incoming_exchange, routing_key=routing_key)#, auto_delete=True)
outgoing_exchange = kombu.Exchange(name=outgoing_exchange_name, type='direct')
rabbitmq_producer = kombu.Producer(settings.rabbitmq_connection0, exchange=outgoing_exchange, serializer='json', compression=None, auto_declare=True)
settings.rabbitmq_connection0.connect()
if settings.rabbitmq_connection0.connected:
callbacks=[]
queues=[]
callbacks.append(callback)
# if push_queue:
# callbacks.append(push_message_callback)
queues.append(incoming_queue)
print 'opening a new *incoming* rabbitmq connection to the %s exchange for the %s queue' % (incoming_exchange.name, incoming_queue.name)
incoming_exchange(settings.rabbitmq_connection0).declare()
incoming_queue(settings.rabbitmq_connection0).declare()
print 'opening a new *outgoing* rabbitmq connection to the %s exchange' % outgoing_exchange.name
outgoing_exchange(settings.rabbitmq_connection0).declare()
with settings.rabbitmq_connection0.Consumer(queues=queues, callbacks=callbacks) as consumer:
while True:
settings.rabbitmq_connection0.drain_events()
答案 0 :(得分:0)
在消费者方面,kombu.mixins.ConsumerMixin在连接消失时处理重新连接(并且还会进行心跳等,并允许您编写更少的代码)。不幸的是,似乎不是ProducerMixin
,但您可能会深入研究代码并对其进行调整......?