我正在尝试远程向 RabbitMQ 服务器发送消息。我的机器上没有安装 RabbitMQ,但我尝试向其发送消息的设备确实安装了 RabbitMQ。每当我尝试发送消息时,我总是收到连接超时错误。我认为我遇到此问题是因为 RabbitMQ 仅监听本地主机。我的文件和错误如下所示。如能得到任何帮助,我将不胜感激。
这是我运行 send.php 时的错误:
PHP Fatal error: Uncaught PhpAmqpLib\Exception\AMQPIOException: stream_socket_client(): Unable to connect to tcp://ipaddress:5672 (Connection timed out) in /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:108
Stack trace:
#0 /home////vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(255): PhpAmqpLib\Wire\IO\StreamIO->connect()
#1 /home//vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(237): PhpAmqpLib\Connection\AbstractConnection->connect()
#2 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AMQPStreamConnection.php(64): PhpAmqpLib\Connection\AbstractConnection->__construct()
#3 /home///send.php(7): PhpAmqpLib\Connection\AMQPStreamConnection->__construct()
#4 {main}
thrown in /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php on line 108
user@user-Parallels-ARM-Virtual-Machine:~-$ php send.php
PHP Fatal error: Uncaught PhpAmqpLib\Exception\AMQPTimeoutException: The connection timed out after 3 sec while awaiting incoming data in /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPIOReader.php:102
Stack trace:
#0 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPIOReader.php(60): PhpAmqpLib\Wire\AMQPIOReader->wait()
#1 /home//I/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(35): PhpAmqpLib\Wire\AMQPIOReader->rawread()
#2 /home/l//vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(601): PhpAmqpLib\Wire\AMQPReader->read()
#3 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(658): PhpAmqpLib\Connection\AbstractConnection->wait_frame()
#4 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(235): PhpAmqpLib\Connection\AbstractConnection->wait_channel()
#5 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(352): PhpAmqpLib\Channel\AbstractChannel->next_frame()
#6 /home//I/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(266): PhpAmqpLib\Channel\AbstractChannel->wait()
#7 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(237): PhpAmqpLib\Connection\AbstractConnection->connect()
#8 /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AMQPStreamConnection.php(64): PhpAmqpLib\Connection\AbstractConnection->__construct()
#9 /home///send.php(7): PhpAmqpLib\Connection\AMQPStreamConnection->__construct()
#10 {main}
thrown in /home///vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPIOReader.php on line 102
这是我的 Send.php 文件,我通过它向其他设备/RabbitMQ 发送消息:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('ipaddress', 15672, 'user', 'pass');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);
$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');
echo " [x] Sent 'Hello World!'\n";
$channel->close();
$connection->close();
?>
这是我的 Receive.php 文件,其他设备/RabbitMQ 运行该文件:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('10.242.125.55', 15672, '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 ($channel->is_open()) {
$channel->wait();
}
$channel->close();
$connection->close();
?>