所以我将RabbitMq与PHP一起使用,我的服务器包含 RabbitMq 和两个 Vms(ubuntu 服务器),有关更多详细信息,这是我的代码;
在 RabbitmQ 服务器中:send.php:
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
//here when i use localhost i don't get error
$connection = new AMQPConnection('192.168.33.10', 5672, 'root', 'root');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World PI');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
这是第一个 Vm 的代码:recive.php:
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
$connection = new AMQPConnection('192.168.33.12', 5672, 'root', 'root');
$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();
注意:在我的 RobbitmQ 中,当我使用 localhost 时我没有收到错误,但是使用 localhost 我无法控制女巫 Vm 我将发送消息