tp6.0 rabbitmq死信队列

rabbitMq交换机,队列情况,先手动创建

1. 创建普通交换机exchange,普通队列order_queue_expire,队列设置属性:
消息过期时间:60000毫秒,过期绑定dead_exchange交换机,routing_key:dead_queue

2. 创建deal_exchange交换机,dead_queue,通过路由key:dead_queue 绑定




tp6.0为例

生产者

    public function producer()
    {
        // 设置队列的TTL和死信交换机
        $deadExchangeName = 'dead_exchange';
        $deadQueueName = 'dead_queue';
        $messageTtl = 1 * 60 * 1000; // 1分钟

        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();

        //死信交换机和路由
        $tale = new AMQPTable();
        $tale->set('x-dead-letter-exchange', $deadExchangeName);
        $tale->set('x-dead-letter-routing-key', $deadQueueName);
        $tale->set('x-message-ttl', $messageTtl);

        // 声明普通队列
        $queueName = 'order_queue_expire';

        $channel->queue_declare($queueName, false, true, false, false, false,$tale);

        // 将普通队列绑定到普通交换机
        $channel->queue_bind($queueName, 'exchange', $queueName);

        $channel->exchange_declare($deadExchangeName, 'direct', false, true, false);
        $channel->queue_declare($deadQueueName, false, true, false, false);
        $channel->queue_bind($deadQueueName, $deadExchangeName, $deadQueueName);

        for($i = 1000; $i < 1010; $i++) {
            $payload = json_encode(['order_id' => $i]);
            $msg = new AMQPMessage($payload, array('delivery_mode' => 2));
            sleep(1);
            $channel->basic_publish($msg, '', $queueName);
        }

        // 确保在脚本结束时关闭通道和连接
        register_shutdown_function(function () use ($channel, $connection) {
            $channel->close();
            $connection->close();
        });

        dd('ok');
    }

消费者

<?php
declare (strict_types = 1);

namespace app\command;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Order extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('cancelOrder')
            ->setDescription('the cancelOrder command');
    }

    protected function execute(Input $input, Output $output)
    {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();

        $consumer = new DeadLetterConsumer($connection, $channel, 'dead_queue');

        $consumer->consume(
            function (AMQPMessage $message) {
                $body = $message->getBody();

                echo "Received message: ", $body, "\n";

                sleep(1);
                //$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
            }
        );

        // 当你完成消费后,关闭连接
        $consumer->close();
    }

}

class DeadLetterConsumer
{
    public function __construct($connection, $channel, $queueName)
    {
        $this->connection = $connection;
        $this->channel = $channel;
        $this->queueName = $queueName;

        $this->channel->queue_declare($queueName, false, true, false, false);
        $this->channel->exchange_declare('dead_exchange', 'direct', false, true, false);

        $this->channel->queue_bind($queueName, 'dead_exchange', 'dead_queue');
    }

    public function consume($callback)
    {
        $this->channel->basic_consume(
            $this->queueName,
            '',
            false,
            true,
            false,
            false,
            $callback
        );

        while (count($this->channel->callbacks)) {
            $this->channel->wait();
        }
    }

    public function close()
    {
        $this->channel->close();
        $this->connection->close();
    }
}
相关推荐
bug_null1 天前
RabbitMQ消息可靠性保证机制4--消费端限流
rabbitmq
青石路1 天前
不依赖 Spring,你会如何自实现 RabbitMQ 消息的消费(一)
rabbitmq
呼啦啦啦啦啦啦啦啦1 天前
【Rabbitmq篇】RabbitMQ⾼级特性----持久性,发送⽅确认,重试机制
分布式·rabbitmq
斯普信专业组2 天前
RabbitMQ实战启程:从配置到故障排查的实战处理(下)
网络·分布式·rabbitmq
呼啦啦啦啦啦啦啦啦2 天前
【Rabbitmq篇】RabbitMQ⾼级特性----消息确认
分布式·rabbitmq
泰山小张只吃荷园2 天前
快速入门消息队列MQ、RabbitMQ
java·spring boot·分布式·spring·spring cloud·rabbitmq·java-rabbitmq
呼啦啦啦啦啦啦啦啦2 天前
基于SpringBoot+RabbitMQ完成应⽤通信
spring boot·rabbitmq·java-rabbitmq
小马爱打代码2 天前
SpringBoot 整合 RabbitMQ
spring boot·rabbitmq·java-rabbitmq
漫天转悠4 天前
RabbitMQ的基本概念和入门
分布式·rabbitmq
dvlinker4 天前
大数据技术Kafka详解 ① | 消息队列(Messages Queue)
大数据·kafka·rabbitmq·rocketmq·activemq·分布式发布订阅消息系统·messages queue