概述
RabbitMQ 是一个开源的消息代理和队列服务器,实现了 AMQP 0-9-1 标准。它可以在完全不同的应用程序之间作为消息的中转站。
安装 RabbitMQ
1. 安装 RabbitMQ Server
Ubuntu:
```bash
sudo apt update
sudo apt install rabbitmq-server
```
macOS (通过 Homebrew):
```bash
brew install rabbitmq
```
2. 启动服务
```bash
sudo service rabbitmq-server start
```
3. 开启管理插件
```bash
sudo rabbitmq-plugins enable rabbitmq_management
```
4. 访问管理界面
- 打开浏览器访问: http://localhost:15672/
- 默认用户名/密码: guest/guest
创建第一个应用
我们将创建两个简单的程序:
1. Producer - 发送消息到 RabbitMQ。
2. Consumer - 接收并处理消息。
1. 设置环境
确保安装了 Python 和 `pika` 库。
```bash
pip install pika
```
2. Producer 示例
```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()
```
3. Consumer 示例
```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()
```
高级功能
1. 工作队列
工作队列用于负载均衡,将任务分散到多个工作者上。
Producer:
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = "A new task"
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()
```
Consumer:
```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()
```
2. 发布/订阅模式
发布/订阅模式允许多个消费者订阅同一个交换器。
Publisher:
```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()
```
Consumers:
```python
import pika
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
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')
channel.basic_consume(queue=queue_name,
on_message_callback=callback,
auto_ack=True)
channel.start_consuming()