1

我只是在做一个测试,试图让 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
4

1 回答 1

2

您的代码中有几个问题:

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

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

$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'));
于 2018-06-12T12:44:16.870 回答