通过pika连接Rabbitmq报错解决办法

连接代码如下:

try:
    connection = pika.BlockingConnection(pika.URLParameters('amqp://guest:guest@{}:5672/%2F'.format(MQ_URL)))
    channel = connection.channel()

    channel.queue_declare(queue='cart_product_queue', durable=True)
    rootLogger.info(' [*] Waiting for messages. To exit press CTRL+C')

    def callback(ch, method, properties, body):
        try:
            rootLogger.info(" [x] Received %r" % body)
            print(body, type(body))
        except Exception as e:
            rootLogger.exception(e)

    channel.basic_consume(callback,
                          queue='cart_product_queue',
                          no_ack=True)

    channel.start_consuming()
except ConnectionClosed:
    pass

运行脚本的时候,一直报错:

017-12-14 15:08:35,873 [MainThread  ] [INFO ]  Created channel=1
2017-12-14 15:08:35,898 [MainThread  ] [INFO ]  '])>
2017-12-14 15:08:35,900 [MainThread  ] [WARNI]  Received remote Channel.Close (406): PRECONDITION_FAILED - parameters for queue 'cart_product_queue' in vhost '/' not equivalent

原因是因为服务端客户端durable=True不一致,不写这个,则正常运行。
channel.queue_declare(queue='cart_product_queue', durable=True)改为
channel.queue_declare(queue='cart_product_queue')

你可能感兴趣的:(通过pika连接Rabbitmq报错解决办法)