主頁 > 知識庫 > Laravel使用RabbitMQ的方法示例

Laravel使用RabbitMQ的方法示例

熱門標(biāo)簽:400電話蘭州申請請 電銷機(jī)器人問門薩維品牌my 外呼系統(tǒng)能給企業(yè)帶來哪些好處 廣東廣州在怎么申請400電話 余姚電話機(jī)器人 咸寧銷售電銷機(jī)器人系統(tǒng) 百度地圖標(biāo)注偏差 開發(fā)地圖標(biāo)注類網(wǎng)站 百度地圖怎樣標(biāo)注圖標(biāo)

導(dǎo)語

RabbitMQ 想必大家都有了解,不做多介紹來。這里實(shí)現(xiàn)的是用 RabbitMQ 作為 Larvel 隊(duì)列的驅(qū)動(dòng),替代 Redis。下面以 Laradock 中安裝示例。

安裝

  1. 切換到 laradock 目錄,將 .env 中關(guān)于 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

擴(kuò)展包安裝以及配置

  1. 進(jìn)入到 workspace 容器中,在項(xiàng)目目錄安裝擴(kuò)展包 composer require vladimir-yuldashev/laravel-queue-rabbitmq
  2. 接下來在 config/queue.php 文件中 connections 添加 rabbitmq 配置,根據(jù)情況自行修改
'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

有兩個(gè)值說明一下,因?yàn)槭窃?Laradock 中,所以 RABBITMQ_HOST 設(shè)置為 rabbitmq ;如果之前使用了Laravel Horizon,那么 RABBITMQ_WORKER 的設(shè)置為 horizon 就可以了。

參考資料:laravel-queue-rabbitmq 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 詳解Spring Cloud Stream使用延遲消息實(shí)現(xiàn)定時(shí)任務(wù)(RabbitMQ)
  • Rabbitmq延遲隊(duì)列實(shí)現(xiàn)定時(shí)任務(wù)的方法
  • Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例
  • 關(guān)于利用RabbitMQ實(shí)現(xiàn)延遲任務(wù)的方法詳解
  • Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
  • C#實(shí)現(xiàn)rabbitmq 延遲隊(duì)列功能實(shí)例代碼
  • JAVA獲取rabbitmq消息總數(shù)過程詳解
  • SpringBoot+RabbitMq具體使用的幾種姿勢
  • RabbitMQ延遲隊(duì)列及消息延遲推送實(shí)現(xiàn)詳解

標(biāo)簽:鷹潭 衡陽 麗江 十堰 銅陵 重慶 臨沂 巴彥淖爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel使用RabbitMQ的方法示例》,本文關(guān)鍵詞  Laravel,使用,RabbitMQ,的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Laravel使用RabbitMQ的方法示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于Laravel使用RabbitMQ的方法示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章