В последние несколько дней я изучал RabbitMQ и систему обмена сообщениями в целом, и я следовал этому учебнику .Мне удалось заставить все работать как в этом уроке.Я выполнил этот проект в Laravel , и я создал издатель и потребительский сценарий, и они работают по назначению.До сих пор я отображал данные о потребителях только с помощью команды echo .Я называю это скриптами через терминал.Мой сценарий consumer выглядит следующим образом:
$host = 'secret';
$port = 5672;
$user = 'secret';
$pass = 'secret';
$vhost = 'secret';
$exchange = 'balance';
$queue = 'local_balance';
$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
$channel = $connection->channel();
/*
The following code is the same both in the consumer and the producer.
In this way we are sure we always have a queue to consume from and an
exchange where to publish messages.
*/
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, false);
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$channel->exchange_declare($exchange, 'direct', false, true, false);
$channel->queue_bind($queue, $exchange);
/**
* @param AMQPMessage $message
*/
function process_message(AMQPMessage $message){
echo $message->body;
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
}
/*
queue: Queue from where to get the messages
consumer_tag: Consumer identifier
no_local: Don't receive messages published by this consumer.
no_ack: Tells the server if the consumer will acknowledge the messages.
exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
nowait:
callback: A PHP Callback
*/
$consumerTag = 'local.consumer';
$channel->basic_consume($queue, $consumerTag, false, false, false, false, 'process_message');
/**
* @param \PhpAmqpLib\Channel\AMQPChannel $channel
* @param \PhpAmqpLib\Connection\AbstractConnection $connection
*/
function shutdown($channel, $connection){
$channel->close();
$connection->close();
}
register_shutdown_function('shutdown', $channel, $connection);
while (count($channel->callbacks)) {
$channel->wait();
}
Итак, я думаю, мне нужно поместить логику базы данных в функцию "process_message", но пока я не нашел никакого решенияо том, как это сделать.Если вам нужно больше кода или у вас есть предложения о том, как по-другому подойти к этой проблеме, дайте мне знать.Любая помощь приветствуется.