rabbitmq pika常用方法
时间: 2023-09-22 18:11:48 浏览: 175
1.连接到RabbitMQ服务器
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
```
2.声明队列
```python
channel.queue_declare(queue='hello')
```
3.发送消息
```python
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
```
4.消费消息
```python
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
```
5.启动消费者
```python
channel.start_consuming()
```
6.关闭连接
```python
connection.close()
```
相关问题
rabbitmq常用面试题
### RabbitMQ 常见面试题及其解答
#### 高可用性实现方式
RabbitMQ 实现高可用性的方法主要依赖于其主从复制模式。在这种架构下,消息队列的数据会被同步到多个节点上,从而确保即使某个节点发生故障,其他节点仍然可以继续提供服务[^1]。
#### 消息可靠性的保障措施
为了防止消息丢失以及避免重复消费的情况,在配置 RabbitMQ 时需启用一些特定的功能选项。例如设置消息持久化、发布确认机制、消费者签收机制等特性能够有效提升系统的健壮性和稳定性;同时针对不同的业务需求场景还需进一步优化调整相应的参数配置以达到最佳效果[^2]。
```python
# 设置消息持久化的Python代码示例
properties = pika.BasicProperties(delivery_mode=2) # 将delivery_mode设为2表示该消息应被保存至磁盘而非仅存在于内存中
channel.basic_publish(exchange='',
routing_key='queue_name',
body=message_body,
properties=properties)
```
#### 如何保证消息顺序性
对于某些应用场景而言保持消息发送接收次序非常重要。然而由于网络延迟等因素影响,默认情况下并不能绝对保证每条消息都能按照发出的时间先后到达目标端口。因此如果确实存在这方面的要求,则建议采用单线程模型或者利用插件如rabbitmq_delayed_message_exchange来辅助控制传输过程中的优先级排序等问题。
#### 核心组件介绍
作为一款功能强大的消息中间件,RabbitMQ 主要由以下几个部分构成:
- **Exchange(交换机)**: 负责接收来自生产者的原始数据包并依据预定义规则将其转发给合适的Queue;
- **Queue(队列)**: 存储待处理的信息项直到对应的Consumer准备就绪为止;
- **Binding(绑定关系)**: 连接 Exchange 和 Queue 的桥梁,决定了哪些类型的消息应该进入哪一个具体的存储空间内等待分发.
以上便是围绕着 "RabbitMQ" 展开的一些典型的技术问答内容概述[^3].
RabbitMQ 个别
RabbitMQ 是一种常用的开源消息代理软件,也被称为消息队列或者消息中间件。它是一个由 Erlang 实现的高度可伸缩的、可重用的企业消息系统。它支持多种消息协议,例如 Advanced Message Queuing Protocol(AMQP),Simple Notification Service(SNS),Simple Queue Service(SQS)等。这些协议允许开发人员在不同的语言和平台之间进行通信,并且在分布式应用程序之间传递消息。
以下是 RabbitMQ 的一些常用特性[^1]:
1. 可靠的消息传输:RabbitMQ 确保了消息的传输和接收,即使在网络不可靠的情况下,也不会丢失消息。
2. 轻量级和高性能:RabbitMQ 是一个轻量级的消息中间件,它可以处理大量的消息,并且在同时保持高性能和低延迟。
3. 可管理性:RabbitMQ 提供了一个易于使用的 Web 界面,可以监控和管理消息队列,包括创建、删除、绑定和发布消息等操作。
4. 丰富的 API:RabbitMQ 支持多种编程语言和开发框架,包括 Java、Python、.NET、Ruby、PHP、Perl、Erlang 和 Go 等。
以下是在 Python 中使用 RabbitMQ 的一个例子[^2]:
安装 Python 客户端:
```shell
pip install pika
```
使用 Python 生产者向 RabbitMQ 发布消息:
```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 消费者从 RabbitMQ 接收消息:
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello',
on_message_callback=callback,
auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
```
阅读全文
相关推荐














