美文网首页
通过pika连接Rabbitmq报错解决办法

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

作者: Sunnky | 来源:发表于2017-12-14 15:10 被阅读0次

连接代码如下:

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 ]  <METHOD(['channel_number=1', 'frame_type=1', 'method=<Channel.Close([\'class_id=50\', \'method_id=10\', \'reply_code=406\', "reply_text=PRECONDITION_FAILED - parameters for queue \'cart_product_queue\' in vhost \'/\' not equivalent"])>'])>
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报错解决办法

      本文链接:https://www.haomeiwen.com/subject/wiskwxtx.html