Laravel队列的创建使用步骤流程
Laravel  /  管理员 发布于 2年前   888
Laravel队列的创建使用步骤流程
所以当我们使用队列,当您收到许多需要耗时的请求时,可以使用消息队列解决此问题。
看看一段官网说明:
Laravel 队列为不同的后台队列服务提供统一的 API,例如 Beanstalk,Amazon SQS,Redis,甚至其他基于关系型数据库的队列。队列的目的是将耗时的任务延时处理,比如发送邮件,从而大幅度缩短 Web 请求和响应的时间。
队列配置文件存放在 config/queue.php 文件中。每一种队列驱动的配置都可以在该文件中找到,包括数据库, Beanstalkd, Amazon SQS, Redis,以及同步(本地使用)驱动。其中还包含了一个 null 队列驱动用于那些放弃队列的任务。
1.创建队列表
php artisan queue:table
php artisan migrate
2.配置驱动,转到QUEUE_CONNECTION并将其更改为类似以下内容
QUEUE_CONNECTION=database
3.创造一个job
php artisan make:job SendMail
4.使用它 (控制器或您需要的任何地方调用程序)
SendMail::dispatch("hi","how are you","alex@gmail.com");
5.启动队列
php artisan queue:work
or
php artisan queue:listen
如果您想知道两者之间有什么区别,请阅读本
“或者,您可以运行queue:listen命令。
使用queue:listen命令时,当您要重新加载更新的代码或重置应用程序状态时,不必手动重新启动工作程序;
但是,此命令不是和queue:work一样有效:”
之后,您应该会看到这样的东西
[2020-06-16 17:04:08][161] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:10][161] Processed: App\Jobs\SendMail
[2020-06-16 17:04:10][162] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:12][162] Processed: App\Jobs\SendMail
[2020-06-16 17:04:12][163] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:14][163] Processed: App\Jobs\SendMail
[2020-06-16 17:04:14][164] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:16][164] Processed: App\Jobs\SendMail
[2020-06-16 17:04:16][165] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:18][165] Processed: App\Jobs\SendMail
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号