<?php require_once './vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable; use PhpAmqpLib\Exchange\AMQPExchangeType; $connection = new AMQPStreamConnection('127.0.0.1', 5672, 'test', 'test', '/'); $channel = $connection->channel(); $channel->exchange_declare('exchange.dlx', AMQPExchangeType::DIRECT, false, true); $channel->exchange_declare('exchange.normal', AMQPExchangeType::FANOUT, false, true); $args = new AMQPTable(); // 消息过期方式:设置 queue.normal 队列中消息10s之后过期 $args->set('x-message-ttl', 10000); $args->set('x-dead-letter-exchange', 'exchange.dlx'); $args->set('x-dead-letter-routing-key', 'routingkey'); $channel->queue_declare('queue.normal', false, true, false, false, false, $args); $channel->queue_declare('queue.dlx', false, true, false, false); $channel->queue_bind('queue.normal', 'exchange.normal'); $channel->queue_bind('queue.dlx', 'exchange.dlx', 'routingkey'); $message = new AMQPMessage('死信队列message'); $channel->basic_publish($message, 'exchange.normal', 'rk'); $channel->close(); $connection->close();