简体   繁体   中英

RabbitMQ receives message but it goes nowhere

I have a RabbitMQ installation on a PuPHPet -generated virtual box (Ubuntu 16.04 x64) on top of Windows 10.

After the setup was complete, I configured a new user using rabbitmqctl :

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

Following the PHP & RabbitMQ Tutorial , I set up a sender and receiver script.

The sender script is the following:

<?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();

And the receiver script is the following:

<?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();
?>

I opened a screen for the receiver script and fired off a few executions of the sender script:

$ php sender.php
Sent 'Hello World!'

The sender script didn't run into any errors (and I was able to verify that the queue was declared in rabbit), but the receiver did not output that it had received / consumed any messages.

Furthermore, a quick check using the admin manager plugin shows that the queue has no messages at all:

$ 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
},
...

It looks like the message is accepted but is immediately discarded & not logged. Speaking of logs, there's nothing suspicious in the rabbit log either. Just a lot of this:

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')

Why aren't the messages coming through?

The RabbitMQ team monitors this mailing list and only sometimes answers questions on StackOverflow.


Since you did not bind my_queue to any exchange, you must publish to the default exchange using my_queue as the routing key. All queues are bound to the default topic exchange using the queue name as the routing key.

In your code, you are using hello as the routing key.

Your code seems perfectly fine except for that you have sent the message to a wrong queue. When you send a message to a queue that doesn't exist, RabbitMQ will simply discard the message since it doesn't know where to send it.

In your code, you use the default exchange to send the message. That is:

$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

so when you use the default exchange , you have to specify the routing key as the third parameter, which is your queue name. RabbitMQ creates routing keys with the same name as queues when you use the default exchange.

To fix your code, simply change hello to your queue name, ie my_queue and it will start sending and receiving.

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM