欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Laravel使用RabbitMQ的方法示例

程序员文章站 2022-05-14 15:01:56
导语 rabbitmq 想必大家都有了解,不做多介绍来。这里实现的是用 rabbitmq 作为 larvel 队列的驱动,替代 redis。下面以 laradock...

导语

rabbitmq 想必大家都有了解,不做多介绍来。这里实现的是用 rabbitmq 作为 larvel 队列的驱动,替代 redis。下面以 laradock 中安装示例。

安装

  1. 切换到 laradock 目录,将 .env 中关于 install_amqp 的值修改为 true
  2. docker-compose stop workspace php-fpm php-worker
  3. docker-compose build workspace php-fpm php-worker rabbitmq
  4. docker-compose up -d workspace php-fpm php-worker rabbitmq

扩展包安装以及配置

  1. 进入到 workspace 容器中,在项目目录安装扩展包 composer require vladimir-yuldashev/laravel-queue-rabbitmq
  2. 接下来在 config/queue.php 文件中 connections 添加 rabbitmq 配置,根据情况自行修改
'rabbitmq' => [

  'driver' => 'rabbitmq',

  /*
   * set to "horizon" if you wish to use laravel horizon.
   */
  'worker' => env('rabbitmq_worker', 'default'),

  'dsn' => env('rabbitmq_dsn', null),

  /*
   * could be one a class that implements \interop\amqp\amqpconnectionfactory for example:
   * - \enqueueamqpext\amqpconnectionfactory if you install enqueue/amqp-ext
   * - \enqueueamqplib\amqpconnectionfactory if you install enqueue/amqp-lib
   * - \enqueueamqpbunny\amqpconnectionfactory if you install enqueue/amqp-bunny
   */

  'factory_class' => enqueue\amqplib\amqpconnectionfactory::class,

  'host' => env('rabbitmq_host', '127.0.0.1'),
  'port' => env('rabbitmq_port', 5672),

  'vhost' => env('rabbitmq_vhost', '/'),
  'login' => env('rabbitmq_login', 'guest'),
  'password' => env('rabbitmq_password', 'guest'),

  'queue' => env('rabbitmq_queue', 'default'),

  'options' => [

    'exchange' => [

      'name' => env('rabbitmq_exchange_name'),

      /*
       * determine if exchange should be created if it does not exist.
       */

      'declare' => env('rabbitmq_exchange_declare', true),

      /*
       * read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
       */

      'type' => env('rabbitmq_exchange_type', \interop\amqp\amqptopic::type_direct),
      'passive' => env('rabbitmq_exchange_passive', false),
      'durable' => env('rabbitmq_exchange_durable', true),
      'auto_delete' => env('rabbitmq_exchange_autodelete', false),
      'arguments' => env('rabbitmq_exchange_arguments'),
    ],

    'queue' => [

      /*
       * determine if queue should be created if it does not exist.
       */

      'declare' => env('rabbitmq_queue_declare', true),

      /*
       * determine if queue should be binded to the exchange created.
       */

      'bind' => env('rabbitmq_queue_declare_bind', true),

      /*
       * read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
       */

      'passive' => env('rabbitmq_queue_passive', false),
      'durable' => env('rabbitmq_queue_durable', true),
      'exclusive' => env('rabbitmq_queue_exclusive', false),
      'auto_delete' => env('rabbitmq_queue_autodelete', false),
      'arguments' => env('rabbitmq_queue_arguments'),
    ],
  ],

  /*
   * determine the number of seconds to sleep if there's an error communicating with rabbitmq
   * if set to false, it'll throw an exception rather than doing the sleep for x seconds.
   */

  'sleep_on_error' => env('rabbitmq_error_sleep', 5),

  /*
   * optional ssl params if an ssl connection is used
   * using an ssl connection will also require to configure your rabbitmq to enable ssl. more details can be founds here: https://www.rabbitmq.com/ssl.html
   */

  'ssl_params' => [
    'ssl_on' => env('rabbitmq_ssl', false),
    '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),
  ],

],

在 .env 中修改 queue_connection 为 rabbitmq ,并添加以下值

rabbitmq_worker=horizon
rabbitmq_host=rabbitmq
rabbitmq_port=5672
rabbitmq_login=guest
rabbitmq_password=guest
rabbitmq_queue=default

有两个值说明一下,因为是在 laradock 中,所以 rabbitmq_host 设置为 rabbitmq ;如果之前使用了laravel horizon,那么 rabbitmq_worker 的设置为 horizon 就可以了。

参考资料:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。