概述
RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP) 0-9-1。本教程将引导你完成搭建和使用 RabbitMQ 的过程。
安装与配置
安装 RabbitMQ
Ubuntu/Debian
```bash
sudo apt update
sudo apt install rabbitmq-server
```
macOS (Homebrew)
```bash
brew install rabbitmq
```
启动服务
```bash
rabbitmq-server
```
配置环境变量
```bash
export RABBITMQ_NODENAME=rabbit
```
基础概念
- Exchange - 收到消息后将其路由到不同的队列。
- Queue - 存储消息直到它们被消费。
- Binding - 告诉交换机如何将消息路由到队列。
- Routing Key - 交换机向队列发送消息的规则。
Python 示例
安装客户端库
```bash
pip install pika
```
发布消息
```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()
```
接收消息
```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()
```
进阶主题
Direct Exchange
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
```
Fanout Exchange
```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()
```
Topic Exchange
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
```
高级特性
持久化消息
```python
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!',
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
```
事务
```python
channel.confirm_delivery()
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
```
手动确认消息
```python
channel.basic_consume(queue='hello',
on_message_callback=callback,
auto_ack=False)
```
发布确认
```python
channel.confirm_delivery()
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')