概述
RabbitMQ 是一个开源的消息代理和队列服务器,它实现了 AMQP (Advanced Message Queuing Protocol) 1.0 标准。RabbitMQ 可以在多语言环境中作为中间件处理应用程序之间的消息传递。
本教程将带你从零开始学习如何使用 RabbitMQ 进行消息的发布与订阅。
1. 安装 RabbitMQ
- [官方文档](https://www.rabbitmq.com/download.html) 提供了多种平台的安装指南。
2. 安装 Erlang
- RabbitMQ 基于 Erlang 开发,需要先安装 Erlang。
3. 安装客户端库
- 选择一种编程语言并安装相应的客户端库(例如 Python、Java)。
```bash
rabbitmq-server
```
```bash
rabbitmqctl add_user guest guest
rabbitmqctl set_permissions -p / guest ".*" ".*" ".*"
```
- 访问 http://localhost:15672/ (默认用户名和密码都是 `guest`)
- Exchange (交换器): 决定消息发送到哪里。
- Queue (队列): 存储消息的地方。
- Binding: Exchange 和 Queue 之间的连接。
- Routing Key: Exchange 使用它来决定消息发送给哪个 Queue。
Python 代码
```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 代码
```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()
```
Python 代码
```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()
```
Python 代码
```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()
```
Python 代码
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', exchange_type='fanout')
message = "Info: Hello from the publisher"
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(" [x] Sent %r" % message)
connection.close()
```
Python 代码
```python
import pika
import sys
def callback(ch, method, properties, body):
print(" [x] %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()