System Dependency Installation
Install required system packages:
yum install ncurses-devel unixODBC unixODBC-devel xmlto libtool autoconf
Erlang Runtime Setup
Download and compile Erlang, which is required by RabbitMQ:
wget http://erlang.org/download/otp_src_18.1.tar.gz
tar -zxvf otp_src_18.1.tar.gz
cd otp_src_18.1
./configure --prefix=/usr/local/erlang
make
make install
Configure Erlang environment variables:
echo 'export PATH="$PATH:/usr/local/erlang/bin"' >> /etc/profile
source /etc/profile
Verify Erlang installation by running erl and then exiting with halt().
RabbitMQ Server Installation
Download and install RabbitMQ server:
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.7/rabbitmq-server-3.5.7.tar.gz
tar zxvf rabbitmq-server-3.5.7.tar.gz
cd rabbitmq-server-3.5.7/
make
make install TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin \
MAN_DIR=/usr/local/rabbitmq/man DOC_INSTALL_DIR=/usr/local/rabbitmq/doc
Configure hostname resolution in /etc/hosts:
192.168.226.1xx localhost.localdomain
RabbitMQ management commands:
/usr/local/rabbitmq/sbin/rabbitmq-server start & # Start server
/usr/local/rabbitmq/sbin/rabbitmqctl status # Check status
/usr/local/rabbitmq/sbin/rabbitmqctl stop # Stop server
Access the management interface at http://192.168.226.1xx:15672
Create startup script for automatic initialization:
#!/bin/sh
sudo /usr/local/rabbitmq/sbin/rabbitmq-server & > /usr/local/rabbitmq/logs/rabbitmq.log 2>&1
PHP AMQP Extension Installation
Install rabbitmq-c library dependencies:
wget -c https://github.com/alanxz/rabbitmq-c/releases/download/v0.8.0/rabbitmq-c-0.8.0.tar.gz
tar zxf rabbitmq-c-0.8.0.tar.gz
cd rabbitmq-c-0.8.0
./configure --prefix=/usr/local/rabbitmq-c-0.8.0
make && make install
Install PHP AMQP extension:
wget -c http://pecl.php.net/get/amqp-1.9.3.tgz
tar zxf amqp-1.9.3.tgz
cd amqp-1.9.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config \
--with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c-0.8.0
make && make install
Enable the extension in php.ini:
extension=amqp.so
Restart PHP-FPM serrvice:
/etc/init.d/php-fpm restart
Verify installation with php -m | grep amqp or check phpinfo() output.
Basic Message Queue Implementation
Producer example:
<?php
$connection = new AMQPConnection([
'host' => '192.168.226.1xx',
'port' => 5672,
'login' => 'guest',
'password' => 'guest'
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName('demo_exchange');
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$queue = new AMQPQueue($channel);
$queue->setName('demo_queue');
$queue->declareQueue();
$queue->bind('demo_exchange', 'demo_routing_key');
for ($i = 1; $i <= 5; $i++) {
$exchange->publish("Message $i", 'demo_routing_key');
echo "Published message $i\n";
}
$connection->disconnect();
Consumer implementation:
<?php
$connection = new AMQPConnection([
'host' => '192.168.226.1xx',
'port' => 5672,
'login' => 'guest',
'password' => 'guest'
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName('demo_queue');
$queue->declareQueue();
$queue->consume(function(AMQPEnvelope $envelope, AMQPQueue $queue) {
$message = $envelope->getBody();
echo "Processing: $message\n";
$queue->ack($envelope->getDeliveryTag());
return true;
});
$connection->disconnect();
Multiple consuemr processes can be started to handle messages concurrently, with RabbitMQ distributing messages in a round-robin fashion between conencted consumers.