您的位置:首页 > 娱乐 > 八卦 > RabbitMQ 入门教程

RabbitMQ 入门教程

2024/12/23 6:38:30 来源:https://blog.csdn.net/qq_40698086/article/details/141805643  浏览:    关键词:RabbitMQ 入门教程

介绍

RabbitMQ 是一个消息代理和队列服务器,实现了 AMQP (Advanced Message Queuing Protocol)。它能够接收、存储并转发消息数据。本教程将带你了解如何使用 RabbitMQ,并提供一些基本的示例。

安装和配置

安装 RabbitMQ

1. Ubuntu:

```bash

sudo apt update

sudo apt install rabbitmq-server

```

2. MacOS:

```bash

brew install rabbitmq

```

3. Windows:

- 下载安装包并运行。

- 配置环境变量 `RABBITMQ_SERVER` 指向安装目录。

启动服务

```bash

rabbitmq-server

```

管理插件

```bash

rabbitmq-plugins enable rabbitmq_management

```

访问管理界面: `http://localhost:15672/`

创建用户

```bash

rabbitmqctl add_user myuser mypassword

rabbitmqctl set_permissions -p / myuser ".*" ".*" ".*"

```

发布/订阅模型

发布者(Publisher)发送消息到交换器(Exchange),交换器根据规则把消息路由到一个或多个队列(Queue)。

Python 示例

发布者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

message = "Hello World!"

channel.basic_publish(exchange='logs', routing_key='', body=message)

print(" [x] Sent %r" % message)

connection.close()

```

订阅者

```python

import pika

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)

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

```

工作队列

工作队列允许你分配任务给多个工作者(worker)。

Python 示例

生产者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = "A new task"

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

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

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)

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue', on_message_callback=callback)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

```

路由

通过路由键(routing key)和绑定(binding)来决定消息的去向。

Python 示例

生产者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

severity = 'info'

message = 'This is an info message'

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)

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

```

主题交换

主题交换允许使用通配符。

Python 示例

生产者

```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 = 'A critical kernel error'

channel.basic_publish(

exchange='topic_logs',

routing_key=routing_key,

body=message)

print(" [x] Sent %r:%r" % (routing_key, 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='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)

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

```

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com