Search code examples
phprabbitmqphp-amqplib

how to send message to specific Vm IP


so i use RabbitMq with PHP, i have my Server that contain the RabbitMq and two Vms (ubuntu server), for more details this is my code;

in the RabbitmQ server : 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();

this is the code of the first 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();

NB: in my RobbitmQ when i use localhost i don't got an error but with localhost i can't control to witch Vm i will send the message


Solution

  • Add desired ip to routing key and utilize topic (or direct) exchanges power. You can also may add ip to message header and relay on headers exchange logic.