RabbitMQ实战指南(三)—— 高级特性

RabbitMQ实战指南(三)—— 高级特性

RabbitMQ是一个功能强大的消息队列系统,提供了许多高级特性来满足各种消息传递的需求。下面是一些常用的高级特性的详细描述和代码示例:

详细描述

1.TTL(Time-To-Live)是指消息的生存时间,它可以设定在消息发送时或队列创建时。一旦消息的生存时间超过了设定的 TTL,就会被标记为过期消息并被丢弃或转发到死信队列。

2.死信(Dead Letter)队列是用于存放被标记为过期消息或被拒绝的消息的特殊队列。当消息被标记为过期或者被拒绝时,可以将其转发到死信队列以备后续处理,例如记录日志或进一步分析。

3.延迟队列(Delay Queue)是一种特殊的队列,它可以延迟消息的传递。当消息被发送到延迟队列时,不会立即传递给消费者,而是在设定的延迟时间后才会被传递。这种机制可以用于处理需要延迟处理的任务,如定时任务或订单超时处理。

4.优先级队列(Priority Queue)是一种按照消息优先级排序的队列。通过设置消息的优先级,可以确保高优先级的消息在队列中被优先处理,避免低优先级的消息长时间阻塞在队列中。

5.RPC(Remote Procedure Call)是一种远程过程调用的机制,它允许一个应用程序向另一个应用程序请求执行某个特定的操作。在使用RabbitMQ进行RPC时,客户端发送请求消息到队列,并等待服务器返回响应消息,从而实现应用程序之间的通信。

6.消息持久化是指将消息存储到磁盘,以防止消息在RabbitMQ重启或崩溃时丢失。在RabbitMQ中,默认情况下,消息是非持久化的,但通过将消息的delivery mode设置为2可以实现消息的持久化。当生产者发送持久化消息时,RabbitMQ会将其存储到磁盘上的日志文件中,以确保消息的安全。

7.在生产端,消息确认(Publish Confirm)是一种机制,用于确保消息成功发送到RabbitMQ服务器。通过设置消息的confirm模式,生产者可以在消息成功到达服务器后得到确认。

代码示例:

TTL(Time-To-Live):消息的生存时间,可以在消息发送时设置。当消息的生存时间超过设定的时间后,消息将被删除。这对于一些需要限定消息处理时间的场景非常有用。

import pika

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 设置队列的TTL10秒
args = {
    'x-message-ttl': 10000
}
channel.queue_declare(queue='my_queue', arguments=args)

# 发送消息时设置消息的TTL5秒
channel.basic_publish(exchange='', routing_key='my_queue', body='Hello, RabbitMQ!', properties=pika.BasicProperties(expiration='5000'))

# 关闭连接
connection.close()

死信(Dead Letter Exchange):当消息被拒绝、过期或达到最大重试次数时,可以将其发送到一个指定的交换机,这个交换机被称为死信交换机。通过使用死信交换机,可以对无法处理的消息进行统一处理。

import pika

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 声明死信交换机和队列
channel.exchange_declare(exchange='dlx_exchange', exchange_type='direct')
channel.queue_declare(queue='dlx_queue')
channel.queue_bind(exchange='dlx_exchange', queue='dlx_queue', routing_key='dlx_key')

# 设置队列的死信交换机
args = {
    'x-dead-letter-exchange': 'dlx_exchange',
    'x-dead-letter-routing-key': 'dlx_key'
}
channel.queue_declare(queue='my_queue', arguments=args)

# 发送消息时设置消息的TTL5秒
channel.basic_publish(exchange='', routing_key='my_queue', body='Hello, RabbitMQ!', properties=pika.BasicProperties(expiration='5000'))

# 关闭连接
connection.close()

延迟队列:通过TTL和死信结合,可以实现延迟队列的功能。可以将需要延迟发送的消息发送到一个队列中,并设置相应的TTL,当消息过期时会被发送到死信队列中。

import pika

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 声明死信交换机和队列
channel.exchange_declare(exchange='dlx_exchange', exchange_type='direct')
channel.queue_declare(queue='dlx_queue')
channel.queue_bind(exchange='dlx_exchange', queue='dlx_queue', routing_key='dlx_key')

# 设置队列的TTL和死信交换机
args = {
    'x-dead-letter-exchange': 'dlx_exchange',
    'x-dead-letter-routing-key': 'dlx_key',
    'x-message-ttl': 10000
}
channel.queue_declare(queue='delay_queue', arguments=args)

# 发送消息到延迟队列
channel.basic_publish(exchange='', routing_key='delay_queue', body='Hello, RabbitMQ!')

# 关闭连接
connection.close()

优先级队列:可以为消息设置优先级,高优先级的消息会被先消费。默认情况下,RabbitMQ不支持优先级队列,需要安装插件rabbitmq-priority-queue。

import pika

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 声明插件
channel.exchange_declare(exchange_type='x-delayed-message', exchange='delayed_exchange', arguments={
    'x-delayed-type': 'direct'
})

# 声明队列
channel.queue_declare(queue='priority_queue', arguments={
    'x-max-priority': 10  # 设置队列的优先级
})

# 绑定队列和交换机
channel.queue_bind(queue='priority_queue', exchange='delayed_exchange', routing_key='priority_key')

# 发送消息时设置优先级为1
channel.basic_publish(exchange='delayed_exchange', routing_key='priority_key', body='Hello, RabbitMQ!', properties=pika.BasicProperties(priority=1))

# 关闭连接
connection.close()

RPC(Remote Procedure Call):允许客户端发送请求消息到服务器端,服务器端处理请求并返回结果消息给客户端。RabbitMQ提供了一个实现RPC的示例。

import pika

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 声明队列
channel.queue_declare(queue='rpc_queue')

# 接收到请求时的回调函数
def on_request(ch, method, props, body):
    n = int(body)

    # 处理请求
    response = fibonacci(n)

    # 返回结果
    ch.basic_publish(exchange='', routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id=props.correlation_id),
                     body=str(response))

    ch.basic_ack(delivery_tag=method.delivery_tag)


# 定义斐波那契数列函数
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)


# 监听队列
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='rpc_queue', on_message_callback=on_request)

# 开始监听
channel.start_consuming()

示例代码:

import pika
import uuid

# 创建连接和通道
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 声明队列
channel.queue_declare(queue='rpc_queue')

在消费端,消息确认(Message
Acknowledgement)是一种机制,用于确保消息成功处理,并从队列中删除。RabbitMQ提供了两种消息确认模式:自动确认模式和手动确认模式。在自动确认模式下,消费者在收到消息后立即确认;在手动确认模式下,消费者需要明确地发送确认消息给RabbitMQ,以告诉它该消息已被正确处理。手动确认模式可以确保消息不会在消费者崩溃或网络故障时丢失。

你可能感兴趣的:(rabbitmq,java)