RabbitMQ used by laravel under Homestead (ubuntu) of laravel queue -- practical part

Posted by knight on Sat, 18 Sep 2021 19:57:00 +0200

  • preface

    The configuration of RabbitMQ server has been written before. For details, please see this article—— RabbitMQ used by laravel under Homestead (ubuntu) of laravel queue - server configuration.
    The next step is how to use RabbitMQ.

  • reference material

    Thanks to all the authors!
    laravel + rabbitmq
    The most complete RabbitMQ in the world - Summary
    Relationship among exchange, route and queue in RabbitMQ

  • Usage 1: use laravel queue rabbitmq alone

  • laravel installing RabbitMQ:
    First, pay attention to the version information of laravel supported by laravel queue rabbitmq:

    Package Version Laravel Version Bug Fixes Until
    9 6 October 5th, 2021 Documentation
    10 6, 7 October 5th, 2021 Documentation
    11 8 April 6th, 2021 Documentation
  • Execute the installation command:

    composer require vladimir-yuldashev/laravel-queue-rabbitmq
  • After waiting for the installation to complete, add the following in config/queue.php:

    'connections' => [
      // ...
      'rabbitmq' => [
         'driver' => 'rabbitmq',
         'queue' => env('RABBITMQ_QUEUE', 'default'),
         'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
         'hosts' => [
                 'host' => env('RABBITMQ_HOST', ''),
                 'port' => env('RABBITMQ_PORT', 5672),
                 'user' => env('RABBITMQ_USER', 'guest'),
                 'password' => env('RABBITMQ_PASSWORD', 'guest'),
                 'vhost' => env('RABBITMQ_VHOST', '/'),
         'options' => [
             'ssl_options' => [
                 'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                 'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                 'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                 'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                 'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
             'queue' => [
                 'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
          * Set to "horizon" if you wish to use Laravel Horizon.
         'worker' => env('RABBITMQ_WORKER', 'default'),
      // ...    
  • And modify it in config/queue.php:

    //Change sync to rabbitmq. Similarly, if you use redis driver, change it to redis
    'default' => env('QUEUE_CONNECTION', 'sync'),
    'default' => env('QUEUE_CONNECTION', 'rabbitmq'),
    //Or modify queue under. env_ CONNECTION=rabbitmq
  • Execute command to generate task class:

    php artisan make:job TestQueue
  • Modify the TestQueue.php Code:


namespace App\Jobs;

use App\Models\UserInfo;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class TestQueue implements ShouldQueue
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $data;

     * Create a new job instance.
     * @return void
    public function __construct($data)
        $this->data = $data;

     * Execute the job.
     * @return void
    public function handle()
        $data = $this->data;
            ["phone" => $data["phone"]],
            ["name" => $data["name"]]
  • Call queue function:
public function test(Request $request){
    $data = ["name" => "Li Da", "phone" => "12345678901"];
    $this->dispatch(new TestQueue($data));
  • Open queue:
    php artisan queue:work
  • Execution result after calling the function:

The queue consumption is successful. Check the database and write the data normally.

  • Usage 2: use laravel queue rabbitmq + rabbitmq server

    Here, there will be a problem. Since method 1 can be used normally, why add rabbitmq server? What does rabbitmq server do?
    According to the official description - Rabbit MQ is a feature rich, multi protocol messaging broker. It supports:

  • AMQP 0-9-1

  • AMQP 1.0

  • MQTT 3.1.1

  • STOMP 1.0 through 1.2
    RabbitMQ is a multi protocol message broker with rich functions. It supports the following protocols.
    according to RabbitMQ official website Description of:
    RabbitMQ runs on many operating systems and cloud environments, and provides a wide range of developer tools for most popular languages.
    Translated as:
    RabbitMQ runs in many operating systems and cloud environments and provides a wide range of development tools for most popular languages.

    To sum up, method 1 uses the use method of laravel queue, which is relatively simple and does not involve too many conceptual things in the queue (such as producers and consumers). Using RabbitMQ server provides you with a server. No matter what language, you can use RabbitMQ by connecting to the corresponding host and port with an account and password. Similar to EMQX (mqtt server) and so on. And RabbitMQ server provides a management background, which can view the relevant information of the queue for easy management.

  • Start rabbitmq server:

    sudo service rabbitmq-server start
  • Open queue:

    php artisan queue:work
  • Test queue (code is the same as method 1 code) result:

  • Other uses

    1. Laravel queue rabbitmq + supervisor daemon. In essence, the queue driver is changed, and the usage is no different from redis;
    2. You can use something like this: java operation RabbitMQ , it breaks away from laravel and uses the way of connection + producer + consumer;
    3. You can also use more complex usage, such as accessing other communication protocols (such as MQTT), subscribing to topics, etc. for specific usage, please refer to the RabbitMQ official website.

  • summary

    The routine use of laravel+redis queue + Supervisor can meet our daily needs. According to Qian Lao's "engineering cybernetics", the latest or most advanced technology of a system may not be the best, but the one suitable for the system and ensuring the stability of the system is the best.
    Of course, on the contrary, we can't stick to the rules, and our skills package should be updated and improved in time, so as to be worthy of ourselves and the cause we love.

Topics: Laravel RabbitMQ IoT homestead