ubuntu

RabbitMQ消息队列在Ubuntu上如何使用

小樊
37
2025-11-19 07:25:24
栏目: 智能运维

Ubuntu 上使用 RabbitMQ 的完整步骤

一 安装与启动

二 安全与用户管理

三 启用管理界面与访问

四 Python 快速上手示例

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello', durable=True)
channel.basic_publish(
    exchange='',
    routing_key='hello',
    body='Hello RabbitMQ!',
    properties=pika.BasicProperties(delivery_mode=2),  # 消息持久化
)
print(" [x] Sent 'Hello RabbitMQ!'")
connection.close()
import pika

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)  # 手动确认

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello', durable=True)
channel.basic_qos(prefetch_count=1)  # 公平分发
channel.basic_consume(queue='hello', on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

五 常用运维命令与排错

0
看了该问题的人还看了