导语

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 就可以了。

参考资料:laravel-queue-rabbitmq 

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

标签:
Laravel,RabbitMQ

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“Laravel使用RabbitMQ的方法示例”

暂无“Laravel使用RabbitMQ的方法示例”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?