RabbitMQ 入门教程

打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

RabbitMQ 入门教程
1. 引言

RabbitMQ 是一个开源的消息代理和队列服务器,实现高级消息队列协议 (AMQP)。它能资助开发者实现应用程序间的解耦、异步处置惩罚、流量削峰等需求。
2. 安装与设置

2.1 安装RabbitMQ

2.1.1 Ubuntu
```bash
sudo apt-get update
sudo apt-get install rabbitmq-server
```
2.1.2 Windows
1. 下载安装包: [RabbitMQ Download](https://www.rabbitmq.com/download.html)
2. 运行安装向导。
2.2 启动服务

```bash
sudo service rabbitmq-server start
```
2.3 设置管理插件

```bash
sudo rabbitmq-plugins enable rabbitmq_management
```
2.4 访问管理界面

- 浏览器访问: `http://localhost:15672/`
- 默认用户名/密码: `guest/guest`
3. Python 开发情况预备

3.1 安装pika库

```bash
pip install pika
```
4. Hello World 示例

4.1 发送端 - send.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()
```
4.2 吸收端 - receive.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()
    ```
    5. 工作队列

    5.1 创建工作队列

    ```python
    import pika
    import time
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='task_queue', durable=True)
    message = "A message"
    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()
    ```
    5.2 消耗者

    ```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()
    ```
    6. 发布/订阅模式

    6.1 发布者

    ```python
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='logs', exchange_type='fanout')
    message = "Log message"
    channel.basic_publish(exchange='logs',
    routing_key='',
    body=message)
    print(" [x] Sent %r" % message)
    connection.close()
    ```
    6.2 订阅者

    ```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()
    ```
    7. 路由模式

    7.1 发布者

    ```python
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
    severity = "info"
    message = "Info message"
    channel.basic_publish(exchange='direct_logs',
    routing_key=severity,
    body=message)
    print(" [x] Sent %r:%r" % (severity, message))
    connection.close()
    ```
    7.2 订阅者

    ```python
    import pika
    def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
    severities = ["info", "warning"]
    result = channel.queue_declare(queue='', exclusive=True)
    queue_name = result.method.queue
    for severity in severities:
    channel.queue_bind(exchange='direct_logs',
    queue=queue_name,
    routing_key=severity)
    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()
    ```
    8. 主题模式

    8.1 发布者

    ```python
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
    routing_key = "kern.critical"
    message = "Kernel critical message"
    channel.basic_publish(exchange='topic_logs',
    routing_key=routing_key,
    body=message)
    print(" [x] Sent %r:%r" % (routing_key, message))
    connection.close()
    ```
    8.2 订阅者

    ```python
    import pika
    def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
    result = channel.queue_declare(queue='', exclusive=True)
    queue_name = result.method.queue
    binding_keys = ["kern.*", "*.critical"]
    for binding_key in binding_keys:
    channel.queue_bind(exchange='topic_logs',
    queue=queue_name,
    routing_key=binding_key)
    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()
    ```


    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
  • 回复

    使用道具 举报

    0 个回复

    正序浏览

    快速回复

    您需要登录后才可以回帖 登录 or 立即注册

    本版积分规则

    tsx81429

    金牌会员
    这个人很懒什么都没写!
    快速回复 返回顶部 返回列表