May 2, 2020

Common Command Line Operations of RabbitMQ

systemctl start/stop/restart/status/enable rabbitmq-server
# Start, stop, restart, check status, and enable RabbitMQ to run at boot
rabbitmq-plugins enable [plugin_name]   # Enable RabbitMQ plugin
rabbitmq-plugins list                   # List all available plugins
rabbitmqctl version                     # Show RabbitMQ server version
rabbitmqctl list_exchanges              # List all exchanges
rabbitmqctl list_queues                 # List all queues
rabbitmqctl list_bindings               # List all bindings

PHP Implementation of RabbitMQ Client

  • RabbitMQ’s six messaging patterns (Official tutorial): https://www.rabbitmq.com/tutorials
  • Official PHP extension (not recommended): https://pecl.php.net/package/amqp/1.11.0/windows
    The official site provides the latest version of php_amqp.dll for Windows.
    However, using it caused errors in my case, so I abandoned this method.
  • Recommended by RabbitMQ:
    Install via Composer: bashCopyEditcomposer require php-amqplib/php-amqplib
  • Basic Concept:
    Using PHP to interact with RabbitMQ is not difficult. It mainly revolves around how to send, route, and process messages based on six working models.
  • Laravel Framework:
    You can use php-amqplib/php-amqplib or the package: bashCopyEditcomposer require vladimir-yuldashev/laravel-queue-rabbitmq
  • Consumer: Keep process running using: phpCopyEditwhile (count($channel->callbacks)) { $channel->wait(); } // or simply: $channel->consume();

Simple Mode (Hello World!)

Summary:
The producer sends a message to the consumer.
Used for inter-process or cross-system communication.

Producer Code:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('192.168.0.180', 5672, 'admin', '12345678');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

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

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

Consumer Code:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('192.168.0.180', 5672, 'admin', '12345678');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

$channel->basic_consume('hello', '', false, true, false, false, function ($msg) {
    echo $msg->body;
});

$channel->consume();

Work Queues Pattern

Summary:
Similar to Redis’s LPUSH + RPOP, but in RabbitMQ, each message can only be consumed once, even with multiple consumers.

Use Case:
Tasks that are time-consuming and don’t require sequential processing, such as sending SMS, emails, or importing/exporting large datasets.

Producer

Run 10 times with different inputs (1~10):

$msg = new AMQPMessage($argv[1]);
$channel->basic_publish($msg, '', 'hello');

Consumer 1

Handles odd numbers, slower processing (5s delay):

sleep(5);

Consumer 2

Handles even numbers, slower processing (10s delay):

sleep(10);

Observation:
RabbitMQ dispatches messages in a round-robin manner regardless of how long each consumer takes.


Publish/Subscribe Pattern (Fanout)

Summary:
Producer broadcasts to all bound queues (like a chatroom). Achieved using fanout exchange type.

Use Case:
Broadcast-style message delivery, such as notifications or chatrooms.

Producer Code:

$channel->exchange_declare('fanout_test', 'fanout', false, false, false);
$msg = new AMQPMessage('Fanout test');
$channel->basic_publish($msg, 'fanout_test');

Consumer Code (abbreviated): Each consumer binds to the same exchange and receives a copy of the message.