队列经常用,能保持信息一致性。也能跨语言,java写的生产者,推到队列中,python写的消费者消费。
这里,生成者,我们是java,已经发了一条消息了。
python是使用pika来链接rabbitmq
pip install pika
import pika
#用户名密码,没有设置的可以省略这一步
credentials = pika.PlainCredentials('xxx','xxx')
connection = pika.BlockingConnection(pika.ConnectionParameters(
host = "xxx",port = 5672, credentials = credentials
))
channel = connection.channel()
channel.queue_declare(queue = "xxx",durable=True)#如果是持久化队列就是True
def callback(ch,method,properties,body):
body_parse=body.decode('utf-8')
print("内容是"+body_parse)
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume("auto_train_kantts", callback)
print("开始消费")
channel.start_consuming()