错误如下:
[root@master2 rabbitmq-python]# python send-1.py
Traceback (most recent call last):
File "send-1.py", line 9, in <module>
channel.queue_declare(queue='test-1')
File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 2507, in queue_declare
self._flush_output(declare_ok_result.is_ready)
File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1340, in _flush_output
raise self._closing_reason # pylint: disable=E0702
pika.exceptions.ChannelClosedByBroker: (406, "PRECONDITION_FAILED - parameters for queue 'test-1' in vhost '/' not equivalent")
我的send-1.py是这样的
#*-*coding:utf-8*-*
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='test-1')
# 特别注意:当未定义exchange时,routing_key需和queue的值保持一致
for i in range(10):
channel.basic_publish(exchange='', routing_key='test-1', body='Hello World!RabbitMQ,lzb.')
print(" [x] Sent 'Hello World!'")
connection.close()
我提前在web端手动Add a new queue,并可持久化,但是send-1.py中声明队列的时候没有可持久化,就会报错
解决办法就是在队列声明时,加上可持久化参数
channel.queue_declare(queue='test-1',durable=True)
OK。