介绍
RabbitMQ 是一个消息代理和队列服务器,实现 [AMQP](https://www.rabbitmq.com/tutorials/amqp-concepts.html) (Advanced Message Queuing Protocol) 标准。它支持多种消息协议,并且可以轻松地与许多应用程序集成。
本教程将引导你完成设置和使用 RabbitMQ 的基本步骤,包括安装、配置以及如何编写简单的生产者和消费者应用。
安装 RabbitMQ
1. 安装 RabbitMQ 服务器
对于 Ubuntu/Debian:
```bash
sudo apt update
sudo apt install rabbitmq-server
```
对于 CentOS/RHEL:
```bash
sudo yum install epel-release
sudo yum install rabbitmq-server
```
启动服务:
```bash
sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server
```
2. 配置管理插件
```bash
sudo rabbitmq-plugins enable rabbitmq_management
```
3. 访问管理界面
- 打开浏览器访问 `http://localhost:15672/`
- 用户名和密码默认都是 `guest`
第一步:发送消息
创建一个简单的程序来发送消息到 RabbitMQ。
发送端 (sender.py)
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
```
第二步:接收消息
创建一个消费者程序来接收消息。
接收端 (receiver.py)
```python
import pika
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_consume(queue='hello',
on_message_callback=callback,
auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
```
第三步:工作队列
通过工作队列来实现任务的异步处理。
生产者 (task_producer.py)
```python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(
exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
))
print(" [x] Sent %r" % message)
connection.close()
```
消费者 (task_consumer.py)
```python
import pika
import time
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
time.sleep(body.count(b'.'))
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='task_queue', on_message_callback=callback)
channel.start_consuming()
```
第四步:发布/订阅模式
实现发布/订阅模型。
发布者 (publisher.py)
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
message = "Info: Hello World!"
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(" [x] Sent %r" % message)
connection.close()
```
订阅者 (subscriber.py)
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs', queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r" % body)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()