php-amqplib连接错误

时间:2018-06-12 11:43:25

标签: php php-amqplib

我正在做一个测试,试图让RabbitMQ与php-amqplib一起工作。我刚编辑了这个问题,以便它使用正确的端口。关于这个的任何想法?

这是我的PHP文件...

1 #!/usr/bin/env php
2 <?php
3
4 require __DIR__ . '/vendor/autoload.php';
5
6 $dotenv = new Dotenv\Dotenv(__DIR__);
7 $dotenv->load();
8
9 $sample_msg = "0% chance of rain!";
10
11 $amqpConnection = new \PhpAmqpLib\Connection\AMQPStreamConnection(
12     getenv('RABBITMQ_HOST'),
13     getenv('RABBITMQ_PORT'),
14     getenv('RABBITMQ_USER'),
15     getenv('RABBITMQ_PASSWORD'),
16     getenv('RABBITMQ_VHOST')
17 );
18
19 $amqpChannel = $amqpConnection->channel();
20 $amqpChannel->queue_declare(getenv('SFTP_RABBITMQ_QUEUE'), false, true, false, false);
21
22 $msg = new AMQPMessage($sample_msg);
23 $channel->basic_publish($msg, '', 'hello');
24
25 echo " [x] Sent $sample_msg\n";

这是我的.env ...

RABBITMQ_HOST=dev.website.co.uk
RABBITMQ_PORT=5672 // ammended
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=xxxxxx
RABBITMQ_PASSWORD=xxxxxx

SFTP_RABBITMQ_QUEUE=my_test_queue

这是composer.json ......

{
    "name": "neil/sftp-user-create",
    "type": "project",
    "require": {
        "php-amqplib/php-amqplib": "^2.7",
        "vlucas/phpdotenv": "^2.4"
        },
    "license": "proprietary",
    "authors": [
        {
             "name": "Me",
             "email": "my@email-address.com"
                                                             }
     ]
}

这是我得到的错误......

PHP Fatal error:  Uncaught PhpAmqpLib\Exception\AMQPRuntimeException: Broken pipe or closed connection in /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:214
Stack trace:
#0 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(149): PhpAmqpLib\Wire\IO\StreamIO->read(7)
#1 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/AMQPReader.php(106): PhpAmqpLib\Wire\AMQPReader->rawread(7)
#2 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(522): PhpAmqpLib\Wire\AMQPReader->read(7)
#3 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(570): PhpAmqpLib\Connection\AbstractConnection->wait_frame(0)
#4 /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(225): PhpAmqpLib\Connection\Abstr in /home/neil/PhpstormProjects/sftp-user-create/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php on line 214

1 个答案:

答案 0 :(得分:2)

您的代码中有几个问题:

  1. 在.env文件中,您在代码中使用RABBITMQ_LOGIN声明了RABBITMQ_USER,在您修复了@iainn提到的连接端口之后,这会给您空登录和“Broken pipe”错误
  2. $channel->basic_publish您没有任何名为$channel的变量,它应该是$amqpChannel
  3. basic_publish中,您的最后一个参数应该是您要向其发送消息的队列的名称,因此它应与queue_declare中的getenv('SFTP_RABBITMQ_QUEUE')
  4. 相同

    在您的代码中修复此问题:

    $amqpConnection = new \PhpAmqpLib\Connection\AMQPStreamConnection(
         getenv('RABBITMQ_HOST'),
         getenv('RABBITMQ_PORT'),
         getenv('RABBITMQ_LOGIN'), // Use _LOGIN as declared in .env
         getenv('RABBITMQ_PASSWORD'),
         getenv('RABBITMQ_VHOST')
    );
    

    (...)

    // Use $amqpChannel as you declared it earlier in code
    // And use the same queue name in last parameter as you used in queue_declare
    $amqpChannel->basic_publish($msg, '', getenv('SFTP_RABBITMQ_QUEUE'));