A note when I tried RabbitMQ to introduce asynchronous processing in PHP.
yum --enablerepo=epel -y install rabbitmq-server
systemctl start rabbitmq-server
systemctl enable rabbitmq-server
rabbitmqctl add_user mquser password
rabbitmqctl list_users
rabbitmqctl add_vhost myhost
rabbitmqctl list_vhosts
rabbitmqctl set_permissions -p myhost mquser ".*" ".*" ".*"
rabbitmqctl list_permissions -p myhost
yum -y install --enablerepo=epel,remi-php73 php php-bcmath composer
cd
mkdir mq
cd mq
composer require php-amqplib/php-amqplib
composer install
vi send_msg.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'mquser', 'password', 'myhost');
$channel = $connection->channel();
$channel->queue_declare('Hello_World', false, false, false, false);
$msg = new AMQPMessage('Hello RabbitMQ World!');
$channel->basic_publish($msg, '', 'Hello_World');
echo " [x] Sent 'Hello_World'\n";
$channel->close();
$connection->close();
?>
php send_msg.php
vi receive_msg.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'mquser', 'password', 'myhost');
$channel = $connection->channel();
$channel->queue_declare('Hello_World', 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('Hello_World', '', false, true, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
?>
php receive_msg.php
(CTRL+Stop at C)
Recommended Posts