我用 rabbitMq 和 videlalvaro/php-amqplib php 客户端实现了一个简单的消息服务。但是当我发送两条消息时,我的接收者丢失了一条。知道会发生什么吗?使用的代码如下。
发送.php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPConnection('127.0.0.1', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
for ($i=0; $i < 10; $i++) {
$text = "SEND MESSAGE ".$i;
$msg = new AMQPMessage($text);
$channel->basic_publish($msg, '', 'hello');
echo " [x] SEND MESSAGE ".$i."\n";
}
$channel->close();
$connection->close();
接收器.php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
$connection = new AMQPConnection('127.0.0.1', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$callback = function($msg) {
echo " [x] Received: ", $msg->body, "\n";
};
$channel->basic_consume('hello', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
send.php 打印响应
[x] 发送消息 0
[x] 发送消息 1
[x] 发送消息 2
[x] 发送消息 3
[x] 发送消息 4
[x] 发送消息 5
[x] 发送消息 6
[x] 发送消息 7
[x ] 发送信息 8
[x] 发送信息 9
receiver.php 打印响应
[x] 接收:发送消息 1
[x] 接收:发送消息 3
[x] 接收:发送消息 5
[x] 接收:发送消息 7
[x] 接收:发送消息 9