简体   繁体   English

如何使用pika使用Python客户端连接到RabbitMQ集群?

[英]How to connect to a RabbitMQ cluster with a Python Client using pika?

I have a Python client that uses Pika package (0.9.13) and retrieves data from one node in a RabbitMQ cluster. 我有一个使用Pika软件包(0.9.13)并从RabbitMQ群集中的一个节点检索数据的Python客户端。 The cluster is composed of two nodes placed in two different host (url_1 and url_2). 群集由放置在两个不同主机(url_1和url_2)中的两个节点组成。 How can I make my Python client to subscribe to both nodes? 如何使我的Python客户端同时订阅两个节点?

That is the main structure of my code: 那是我的代码的主要结构:

import pika
credentials = pika.PlainCredentials(user, password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=url_1,
                                     credentials=credentials, ssl=ssl, port=port))
channel = connection.channel() 
channel.exchange_declare(exchange=exchange.name, 
                         type=exchange.type, durable=exchange.durable)

result = channel.queue_declare(queue=queue.name, exclusive=queue.exclusive, 
                             durable=queue.durable, auto_delete=queue.autoDelete)
channel.queue_bind(exchange=exchange.name, queue=queue.name, 
                   routing_key=binding_key)
channel.basic_consume(callback,
                  queue=queue.name,
                  no_ack=True)

channel.start_consuming()

If you have a cluster it is not important in which node you are connected. 如果您有集群,那么连接哪个节点并不重要。

Usually in to solve this typical problem it is enough to configure a simple load-balancer and connect the clients to the LB 通常,为了解决此典型问题,只需配置一个简单的负载均衡器并将客户端连接到LB就足够了。

clients-----> LB ------> rabbitmq(s) instances cluster. clients -----> LB ------> Rabbitmq实例集群。

EDIT: 编辑:

Load balancer as HAPROXY or for example http://crossroads.e-tunity.com/ they are light and simple to use. 负载均衡器为HAPROXY或例如http://crossroads.e-tunity.com/,它们轻巧易用。

I'd like to add also this: RabbitMQ Client connect to several hosts 我还要添加: RabbitMQ Client连接到多个主机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM