Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The cluster is composed of two nodes placed in two different host (url_1 and url_2). How can I make my Python client to subscribe to both nodes?

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()
like image 234
sergi Avatar asked May 14 '14 09:05

sergi


1 Answers

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 L.B.

clients-----> LB ------> rabbitmq(s) instances cluster.

EDIT:

Load balancer as HAPROXY or for example http://crossroads.e-tunity.com/ they are light and simple to use.

I'd like to add also this: RabbitMQ Client connect to several hosts

like image 161
Gabriele Santomaggio Avatar answered Nov 09 '22 14:11

Gabriele Santomaggio