所以我将 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我将发送消息
答案 0 :(得分:0)
将所需的ip添加到路由密钥并利用topic
(或direct)
交换机电源。您还可以将ip添加到消息头并在headers
交换逻辑上进行中继。
答案 1 :(得分:0)
@ zaq178miami的解决方案肯定会有效,但您也可以使用独占队列。这将使队列排除一个连接。