Laravel使用RabbitMQ的方法示例
程序员文章站
2022-05-14 15:01:56
导语
rabbitmq 想必大家都有了解,不做多介绍来。这里实现的是用 rabbitmq 作为 larvel 队列的驱动,替代 redis。下面以 laradock...
导语
rabbitmq 想必大家都有了解,不做多介绍来。这里实现的是用 rabbitmq 作为 larvel 队列的驱动,替代 redis。下面以 laradock 中安装示例。
安装
- 切换到 laradock 目录,将 .env 中关于 install_amqp 的值修改为 true
- docker-compose stop workspace php-fpm php-worker
- docker-compose build workspace php-fpm php-worker rabbitmq
- docker-compose up -d workspace php-fpm php-worker rabbitmq
扩展包安装以及配置
- 进入到 workspace 容器中,在项目目录安装扩展包 composer require vladimir-yuldashev/laravel-queue-rabbitmq
- 接下来在 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 就可以了。
参考资料:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 十进制负数转换为二进制、八进制、十六进制的知识分享
下一篇: VBS教程:函数-DateAdd 函数
推荐阅读
-
实例讲解使用HTML5 Canvas绘制阴影效果的方法
-
Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
-
Android系统开发中log的使用方法及简单的原理
-
iOS应用中使用Toolbar工具栏方式切换视图的方法详解
-
教你用CorelDRAW绘制矩形和方形 矩形工具的使用方法和应用技巧介绍
-
SAI有哪些画笔? SAI画笔的设置与使用方法
-
使用HTML5 Canvas API控制字体的显示与渲染的方法
-
实例讲解使用SVG制作loading加载动画的方法
-
CorelDRAW中手绘工具的使用方法和操作技巧介绍
-
如何在CorelDRAW中使用渐变填充对象 渐变填充的操作方法和应用技巧介绍