🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ 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); $queue = new \AMQPQueue($channel); $queue->setName('hello'); $exchange = new \AMQPExchange($channel); $result = $exchange->publish('hello world!','hello',AMQP_IMMEDIATE,array()); var_dump($result); 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('hello'); $queue->declareQueue(); $result = $queue->get(); var_dump($result); if (!empty($result)) { echo $result->getBody(); } ~~~ 在send.php中,并未指定exchange的名称。实际上但未定义exchangeName时,会使用rabbitMQ中系统默认的exchange,默认的exchange是一个name为空的exchange。在使用publish时,会将消息发送到与rote key同名的queue中。 在receive.php中,如果未使用bind exchange,则默认绑定,RabbitMQ中默认的exchange,即为空名的exchange. 这个默认的exchange的Type是direct.