gpt4 book ai didi

php - RabbitMQ 收到消息但无处可去

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:08 25 4
gpt4 key购买 nike

我在 PuPHPet 上安装了 RabbitMQ - 在 Windows 10 之上生成虚拟框 (Ubuntu 16.04 x64)。

设置完成后,我使用 rabbitmqctl 配置了一个新用户:

# rabbitmqctl add_user root root
# rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
# rabbitmqctl set_user_tags root administrator

PHP & RabbitMQ Tutorial 之后, 我设置了一个发送者和接收者脚本。

发送脚本如下:

<?php
require_once 'vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'root', 'root');
$channel = $connection->channel();
$channel->queue_declare('my_queue', false, false, false, false);

$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');

echo "Sent 'Hello World!'\n";

$channel->close();
$connection->close();

接收脚本如下:

<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('localhost', 5672, 'root', 'root');
$channel = $connection->channel();
$channel->queue_declare('my_queue', 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('my_queue', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>

我为接收方脚本打开了一个屏幕,并触发了发送方脚本的几次执行:

$ php sender.php
Sent 'Hello World!'

发送方脚本没有遇到任何错误(我能够验证队列是在 rabbit 中声明的),但是接收方没有输出它已经接收/消费了任何消息。

此外,使用 admin manager 插件进行的快速检查显示队列中根本没有消息:

$ curl -i -u root:root http://localhost:15672/api/queues
...
{
"messages_details":{
"rate":0.0
},
"messages":0,
"messages_unacknowledged_details":{
"rate":0.0
},
"messages_unacknowledged":0,
"messages_ready_details":{
"rate":0.0
},
"messages_ready":0,
"reductions_details":{
"rate":0.0
},
"reductions":9294,
"node":"rabbit@leon",
"arguments":{

},
"exclusive":false,
"auto_delete":false,
"durable":false,
"vhost":"/",
"name":"my_queue",
"message_bytes_paged_out":0,
"messages_paged_out":0,
"backing_queue_status":{
"avg_ack_egress_rate":0.0,
"avg_ack_ingress_rate":0.0,
"avg_egress_rate":0.0,
"avg_ingress_rate":0.0,
"delta":[
"delta",
"undefined",
0,
0,
"undefined"
],
"len":0,
"mode":"default",
"next_seq_id":0,
"q1":0,
"q2":0,
"q3":0,
"q4":0,
"target_ram_count":"infinity"
},
"head_message_timestamp":null,
"message_bytes_persistent":0,
"message_bytes_ram":0,
"message_bytes_unacknowledged":0,
"message_bytes_ready":0,
"message_bytes":0,
"messages_persistent":0,
"messages_unacknowledged_ram":0,
"messages_ready_ram":0,
"messages_ram":0,
"garbage_collection":{
"minor_gcs":12,
"fullsweep_after":65535,
"min_heap_size":233,
"min_bin_vheap_size":46422,
"max_heap_size":0
},
"state":"running",
"recoverable_slaves":null,
"consumers":0,
"exclusive_consumer_tag":null,
"effective_policy_definition":[

],
"operator_policy":null,
"policy":null,
"consumer_utilisation":null,
"idle_since":"2018-01-28 15:21:22",
"memory":9640
},
...

看起来消息已被接受,但立即被丢弃且未记录。说到日志,rabbit 日志中也没有任何可疑之处。只是很多这样的:

2018-01-28 15:47:43.654 [info] <0.1417.0> accepting AMQP connection <0.1417.0> ([::1]:49058 -> [::1]:5672)
2018-01-28 15:47:43.696 [info] <0.1417.0> connection <0.1417.0> ([::1]:49058 -> [::1]:5672): user 'root' authenticated and granted access to vhost '/'
2018-01-28 15:47:43.742 [info] <0.1417.0> closing AMQP connection <0.1417.0> ([::1]:49058 -> [::1]:5672, vhost: '/', user: 'root')

为什么消息没有通过?

最佳答案

除了您将消息发送到错误的队列之外,您的代码看起来完全没问题。当您向不存在的队列发送消息时,RabbitMQ 将简单地丢弃该消息,因为它不知道将消息发送到哪里。

在您的代码中,您使用默认交换来发送消息。即:

$channel->basic_publish($msg, '', 'hello');

Here we use the default or nameless exchange: messages are routed to the queue with the name specified by routing_key, if it exists. The routing key is the third argument to basic_publish

https://www.rabbitmq.com/tutorials/tutorial-three-php.html

所以当你使用default exchange时,你必须指定routing key作为第三个参数,这是你的队列名称。当您使用默认交换时,RabbitMQ 会创建与队列同名的路由键。

要修复您的代码,只需将 hello 更改为您的队列名称,即 my_queue,它将开始发送和接收。

希望对您有所帮助:)

关于php - RabbitMQ 收到消息但无处可去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48488662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com