💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[info] return 机制 **什么是Return机制?** Return Listener用于处理一些不可路由的消息。也是生产段添加的一个监听。 我们的消息生产者,通过指定一个Exchange和Routingkey,把消息送达到某一个队列中去,然后我们的消费者监听队列,进行消息处理操作。但是在某些情况下,如果我们在发送消息的时候,当前的exchange不存在 或者指定的路由key路由不到,这个时候如果我们需要监听这种不可达的消息,就要使用Return Listener。 ***** **Retrun 机制流程图:** ![](https://img.kancloud.cn/5d/a5/5da570c8b2f472704428738f7c721603_1611x962.png) ***** **实现Return机制:basic_return_noReturn.php** ~~~ <?php require_once __DIR__ . '/../vendor/autoload.php'; use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Exchange\AMQPExchangeType; use PhpAmqpLib\Message\AMQPMessage; $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); // declare exchange but don`t bind any queue $channel->exchange_declare('hidden_exchange', AMQPExchangeType::TOPIC); $message = new AMQPMessage("Hello World!"); $wait = true; // 监听器 $returnListener = function( $replyCode, $replyText, $exchange, $routingKey, $message ) use ($wait){ $GLOBALS['wait'] = false; echo "return:", $replyCode,"\n", $replyText,"\n", $exchange,"\n", $routingKey,"\n", $message->body,"\n"; }; $channel->set_return_listener($returnListener); //echo " [x] Sent non-mandatory ..."; $channel->basic_publish( $message, 'hidden_exchange', 'rkey', ture ); //echo " done.\n"; while ($wait) { $channel->wait(); } $channel->close(); $connection->close(); ~~~ 1. 运行代码 未找到路由键,导致监听到错误码:312 错误信息:NO_ROUTE ![](https://img.kancloud.cn/e9/d1/e9d13ba257e46ff8abecc1744399ed0e_497x152.png) 2. 如果交换机不存在,PHP代码底层直接报错 ![](https://img.kancloud.cn/6c/fd/6cfdde0692a9eade295bdd7c4c7846ad_629x317.png) ![](https://img.kancloud.cn/4d/e3/4de3265fb3cf877815c0b3d958722a4d_852x286.png)