Back to the list of posts

Очереди Rabbit Mq установка, конфигурация, примеры кода в Laravel

Installing RabbitMQ on Ubuntu
  1. Update Ubuntu packages using sudo apt update.
  1. Install Erlang, which is required for RabbitMQ to work, using the sudo apt command install -y erlang.
  1. Add the RabbitMQ repository using the command echo "deb <http://www.rabbitmq.com/debian/> testing main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list.
  1. Add the RabbitMQ key using the command wget -O- <https://www.rabbitmq.com/rabbitmq-release-signing-key.asc> | sudo apt-key add -.
  1. Update your Ubuntu packages again using sudo apt update.
  1. Install RabbitMQ using the command 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:

  1. Open the RabbitMQ configuration file: sudo nano /etc/rabbitmq/rabbitmq.config.
  1. Add the following lines to the file:
[
   {rabbit, [
      {loopback_users, []},
      {tcp_listeners, ["127.0.0.1", 5672]},
      {default_vhost, <<"/">>},
      {default_user, <<"guest">>},
      {default_pass, <<"guest">>}
   ]}
].

Here:

  1. Save and close the file.
  1. Restart RabbitMQ: 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:

  1. Install the Laravel RabbitMQ package using Composer: composer require vladimir-yuldashev/laravel-queue-rabbitmq.
  1. Open the Laravel queue configuration file: nano config/queue.php.
  1. Add the following code to the 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', '/'),
         ],
     ],
],
  1. Next, make sure your Laravel project's .env file has the correct values for 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:

method
$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.

#DevOps #RabbitMq #Laravel

Back to the list of posts Next post

menuclose

start a project

Заявка отправлена

Спасибо! Заявка отправлена. Свяжемся с вами в течении часа!