🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
使用场景,日志系统(引用官方),假设产生一条日志的级别有debug, info, notice, warning, error, critical, alert, emergency八种级别(按照psr-3的日志标准,日志的级别有8个),我们希望日志消息发送到exchange后,warning, error, critical, alert, emergency转发到queue.log.error队列,notice转发到queue.log.notice队列,debug、info转发到queue.log.debug中。 ~~~ route_send.php <?php $config = [ 'host' => '127.0.0.1', 'vhost' => '/', 'port' => 5672, 'login' => 'guest', 'password' => 'guest' ]; $conn = new \AMQPConnection($config); $conn->connect(); $channel = new \AMQPChannel($conn); $channel->qos(0,0); $exchange = new \AMQPExchange($channel); $exchange->setName('exchange.logs'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->setFlags(AMQP_DURABLE); $exchange->declare(); $logs =[ 'debug' =>'message [debug]', 'info' =>'message [info]', 'notice' =>'message [notice]', 'warning' =>'message [warning]', 'error' =>'message [error]', 'critical' =>'message [critical]', 'alert' =>'message [alert]', 'emergency' =>'message [emergency]' ]; foreach ($logs as $routeKey => $log) { $result = $exchange->publish($log, $routeKey); var_dump($result); } ~~~ route_receive.php ~~~ <?php $config = [ 'host' => '127.0.0.1', 'vhost' => '/', 'port' => 5672, 'login' => 'guest', 'password' => 'guest' ]; $conn = new \AMQPConnection($config); $conn->connect(); $channel = new \AMQPChannel($conn); $channel->qos(0,0); $queue = new \AMQPQueue($channel); $queue->setName("queue.log.error"); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); $queue->bind('exchange.logs','warning'); $queue->bind('exchange.logs','error'); $queue->bind('exchange.logs','critical'); $queue->bind('exchange.logs','alert'); $queue->bind('exchange.logs','emergency'); $queue->consume('processMessage',AMQP_AUTOACK); function processMessage($envelope, $queue) { global $i; echo "Message $i: " . $envelope->getBody() . "\n"; $i++; } ~~~