1 Star 2 Fork 0

落西开源/Laravel队列应用场景示例

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Laravel队列应用场景-系列教程

应用场景

阶梯降频轮询

例如有这样的场景,某电商系统的订单支付状态需要和上游的支付系统保持实时同步,需要每秒检查一次订单状态,如果订单状态为未支付,则继续检查,直到订单已完成。

然而针对每一笔订单,每秒检查一次会对服务器和上游API造成很大的负载。

如果我们能动态调整检查频率,比如前五次尝试中每1秒检查一次订单状态, 然后在接下来的五次尝试中每5秒检查一次,依此类推。这样既能减少服务器和外部API的负载,又能确保及时更新

我们给他起个名字叫做阶梯降频轮询

真实的例子比如对接支付宝时,支付成功的异步通知会逐渐减低回调频次通知调用方。

支付宝文档示例

在进行异步通知交互时,如果支付宝收到的应答不是 success ,支付宝会认为通知失败,会通过一定的策略定期重新发起通知。通知的间隔频率为:4m、10m、10m、1h、2h、6h、15h。

本教程将带您一步步实现秒级别的阶梯降频轮询出队列方案。

实际效果:

logo

logo

1. 项目初始化

composer install

2. 配置 .env

cp .env.example .env

3. 生成 key

php artisan key:generate

4. 数据库准备

# 创建表结构
php artisan migrate

# 插入测试数据
php artisan db:seed

5. 测试出队列效果

# 先开始队列监听worker
php artisan queue:work --sleep=1

# 开一个新的终端窗口,执行测试命令,这个命令会发送一条消息到队列
php artisan app:test

6. 查看队列执行情况

# 打开日志,查看队列执行情况
tail -f storage/logs/laravel.log
MIT License Copyright (c) 2024 loxi-opensource / 落西开源 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Laravel队列应用场景示例 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/loxi-open-source/laravel-queue-example.git
git@gitee.com:loxi-open-source/laravel-queue-example.git
loxi-open-source
laravel-queue-example
Laravel队列应用场景示例
main

搜索帮助