RabbitMQ 入门教程

打印 上一主题 下一主题

主题 859|帖子 859|积分 2577

概述

RabbitMQ 是一个开源的消息署理和队列服务器,实现了 AMQP 0-9-1 标准。它可以在完全差别的应用程序之间传递消息。本教程将带你从零开始学习怎样使用 RabbitMQ。
情况准备

1. 安装 RabbitMQ
- 在下令行中运行 `sudo apt-get install rabbitmq-server` (Ubuntu) 或者通过官方文档获取其他系统的安装指南。
2. 启动服务
- 运行 `sudo service rabbitmq-server start`。
3. 安装客户端库
- 对于 Python: `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()
    ```
    第二步:工作队列

    生产者

    ```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
    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
    import pika
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange='logs', exchange_type='fanout')
    message = "Info: Hello World!"
    channel.basic_publish(exchange='logs', routing_key='', body=message)
    print(" [x] Sent %r" % message)
    connection.close()
    ```
    吸收端 1

    ```python
    import pika
    import uuid
    def on_message(channel, method, props, 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=on_message, auto_ack=True)
    channel.start_consuming()
    ```
    吸收端 2

    ```python
    import pika
    import uuid
    def on_message(channel, method, props, 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=on_message, auto_ack=True)
    channel.start_consuming()
    ```
    第四步:路由模式

    发送端

    ```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()
    ```
    吸收端

    ```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')
    result = channel.queue_declare(queue='', exclusive=True)
    queue_name = result.method.queue
    severities = ['error', 'warning']
    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()

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

    使用道具 举报

    0 个回复

    倒序浏览

    快速回复

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

    本版积分规则

    立聪堂德州十三局店

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