RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP)。它可以在生产者和消费者之间传递消息,并且可以保证消息的传递。本教程将指导你通过简单的步骤来搭建并使用 RabbitMQ。
Linux
```bash
sudo apt-get update
sudo apt-get install rabbitmq-server
```
Windows
- 下载安装包: https://www.rabbitmq.com/download.html
- 运行安装程序并按照提示操作
macOS
```bash
brew install rabbitmq
```
```bash
rabbitmq-server
```
```bash
rabbitmq-plugins enable rabbitmq_management
```
浏览器访问: http://localhost:15672/
默认用户名/密码: guest/guest
我们将创建一个简单的消息发布/订阅系统。
producer.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()
```
consumer.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()
```
- 在一个终端窗口运行 `producer.py`:
```bash
python producer.py
```
- 在另一个终端窗口运行 `consumer.py`:
```bash
python consumer.py
```
producer_confirm.py
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.confirm_delivery()
for i in range(1, 6):
channel.basic_publish(exchange='',
routing_key='hello',
body=f'Message {i}')
print(f" [x] Sent 'Message {i}'")
connection.close()
```
consumer_persistent.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', durable=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='hello',
on_message_callback=callback,
auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
```
work_queue_producer.py
```python
import pika
import sys
message = ' '.join(sys.argv[1:]) or "Hello World!"
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
channel.basic_publish(
exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE
))
print(" [x] Sent %r" % message)
connection.close()
```
work_queue_consumer.py
```python
import pika
import time
def callback(ch, method, properties, body):
print(" [x] Received %r" % body.decode())
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()