sudo apt update
.sudo apt command
install -y erlang
.
echo "deb <http://www.rabbitmq.com/debian/>
testing main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
.
wget -O- <https://www.rabbitmq.com/rabbitmq-release-signing-key.asc>
| sudo apt-key add -
.
sudo apt update
.sudo apt install -y rabbitmq-server
.After completing these steps, RabbitMQ will be installed on your Ubuntu server.
After installing and configuring the RabbitMQ web interface, you you can start configuring RabbitMQ itself. Here is an example of a basic configuration:
sudo nano /etc/rabbitmq/rabbitmq.config
.[
{rabbit, [
{loopback_users, []},
{tcp_listeners, ["127.0.0.1", 5672]},
{default_vhost, <<"/">>},
{default_user, <<"guest">>},
{default_pass, <<"guest">>}
]}
].
Here:
loopback_users
allows users to connect via
localhost without restrictions.
tcp_listeners
specifies what RabbitMQ should listen to
connections on port 5672 on localhost.
default_vhost
, default_user
and
default_pass
set default values for virtual host, username
and password.
sudo service rabbitmq-server restart
.RabbitMQ is now configured with a basic configuration. Remember that you can change this configuration according to your requirements.
After installing and configuring RabbitMQ on the server, you can interact with it using Laravel. Here is an example configuration for Laravel:
composer require
vladimir-yuldashev/laravel-queue-rabbitmq
.
nano config/queue.php
.connections
section:'rabbitmq' => [
'driver' => 'rabbitmq',
'queue' => env('RABBITMQ_QUEUE', 'default'),
'connection' => PhpAmqpLib\\Connection\\AMQPLazyConnection::class,
'hosts' => [
[
'host' => env('RABBITMQ_HOST', '127.0.0.1'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_USER', 'guest'),
'password' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', '/'),
],
],
],
RABBITMQ_QUEUE
set.
RABBITMQ_HOST
, RABBITMQ_PORT
, RABBITMQ_USER
,
RABBITMQ_PASSWORD
and RABBITMQ_VHOST
.
Laravel is now configured to work with RabbitMQ as queue driver.
Here is a basic example of using queues in Laravel:
use Illuminate\\Support\\Facades\\Queue;
Queue::push(function($job) {
// Here we can do any heavy processing that our application server requires!
$job->delete();
});
In this example, Laravel's push
function adds
task to be executed in queue. When the queue server is ready to process a new job, it will start the job and
will remove it from the queue when the job completes successfully.
If you need to postpone the execution of a task for a certain
time, you can use the later
:
$when = now()->addMinutes(10);
Queue::later($when, function($job) {
// This task will be added to the queue and executed no earlier than in 10 minutes!
$job->delete();
});
In this example, the job will be added to the queue, but not will be executed until the delayed time arrives.
menuclose
Спасибо! Заявка отправлена. Свяжемся с вами в течении часа!