RabbitMQ 入门教程

介绍

RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP)。它可以在生产者和消费者之间传递消息,并且可以保证消息的传递。本教程将指导你通过简单的步骤来搭建并使用 RabbitMQ。

安装与配置

1. 安装 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

```

2. 启动服务

```bash

rabbitmq-server

```

3. 配置管理插件

```bash

rabbitmq-plugins enable rabbitmq_management

```

4. 访问管理界面

浏览器访问: http://localhost:15672/

默认用户名/密码: guest/guest

示例代码

我们将创建一个简单的消息发布/订阅系统。

5. 创建生产者

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()

```

6. 创建消费者

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()

```

7. 运行示例

- 在一个终端窗口运行 `producer.py`:

```bash

python producer.py

```

- 在另一个终端窗口运行 `consumer.py`:

```bash

python consumer.py

```

高级特性

8. 发布确认

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()

```

9. 消费者持久化

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()

```

10. 工作队列

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()

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值