laravel队列使用场景(Laravel使用RabbitMQ的方法示例)
类别:编程学习 浏览量:750
时间:2022-01-24 00:02:24 laravel队列使用场景
Laravel使用RabbitMQ的方法示例导语
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 就可以了。
参考资料:laravel-queue-rabbitmq
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://segmentfault.com/a/1190000019501459
您可能感兴趣
- rabbitmq延迟队列原理(Rabbitmq heartbea心跳检测机制原理解析)
- laravel队列使用场景(Laravel使用RabbitMQ的方法示例)
- linuxrabbitmq安装和使用教程(用docker部署RabbitMQ环境的详细介绍)
- docker rabbitmq集群部署(Docker部署rabbitmq遇到的两个问题)
- rabbitmq客户端内存占用(mysql-canal-rabbitmq 安装部署超详细教程)
- java入坑rabbitmq(Python操作rabbitMQ的示例代码)
- docker 启动rabbitmq(docker安装rabbitmq无法进入管理页面的问题)
- rabbitmq docker配置(Docker部署Rabbitmq容器实现过程解析)
- centos7rabbitmq怎么装(如何在centos上使用yum安装rabbitmq-server)
- 春节放假几天(春节放假几天2023法定几天)
- 今天 3月13日,31年前,一个英雄少年感动了中国(今天3月13日31年前)
- 你好,新成理人丨成都理工大学2019级新生开学典礼隆重举行(新成理人丨成都理工大学2019级新生开学典礼隆重举行)
- 这部民警编演的红色话剧,讲述了一个不断追寻的故事(这部民警编演的红色话剧)
- 日本菜有什么好吃(日本菜有什么好吃的做法)
- 韩国泡菜做法(韩国泡菜的做法步骤)
热门推荐
- laravel数据库操作方式(Laravel 实现数据软删除功能)
- try...catch... finally 的用法
- sp_reset_connection的作用
- 织梦dedecms教程(详解各种dedeCMS织梦后台登陆验证码错误或不显示解决方法大全)
- dockerfile镜像介绍(使用Dockerfile构建docker镜像)
- pythonsql注入检测(Python版Mssql爆破小脚本)
- vue设置div大小(Vue实现div滚轮放大缩小)
- php使用yield处理并发(Yii框架中使用PHPExcel的方法分析)
- mybatis执行sql源码解析(mybatis调用sqlserver存储过程返回结果集的方法)
- canvas小程序海报(使用canvas生成含有微信头像的邀请海报没有微信头像问题)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9