文档地址:https://learnku.com/docs/laravel/7.x/horizon/7514
安装
提示:由于 Horizon 使用了异步进程信号,所以 PHP 7.1+ 以上版本才可以使用。
队列数据是存在Redis里的,所以要确保队列连接在 queue 配置文件的 redis 项已经正确设置。
我们使用 Composer 安装 Horizon 到 Laravel 项目:
composer require laravel/horizon
安装完成后,使用 Artisan 命令 vendor:publish 发布前端资源:
php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
配置
分别是配置文件 config/horizon.php
和存放在 public/vendor/horizon
文件夹中的 CSS 、JS 等页面资源文件。
balance 配置项
Horizon 提供了三种负载均衡策略以供选择:simple、auto 和 false,simple 是默认策略,在进程之间平均分配进入任务:
'balance' => 'simple',
auto 策略基于队列当前负载调整每个队列的工作进程数量。例如,如果 notifications 队列有 1000 个等待执行的任务而 render 队列是空的,那么 Horizon 将会为 notifications 队列分配更多的工作进程直到队列为空。
如果把 balance 选项设置为 false,就会使用默认的 Laravel 行为,也就是按照配置文件中的排列顺序处理队列。
<?php
return [
/*
|--------------------------------------------------------------------------
| Horizon Domain
|--------------------------------------------------------------------------
|
| This is the subdomain where Horizon will be accessible from. If this
| setting is null, Horizon will reside under the same domain as the
| application. Otherwise, this value will serve as the subdomain.
|
*/
'domain' => null,
/*
|--------------------------------------------------------------------------
| Horizon Path
|--------------------------------------------------------------------------
|
| This is the URI path where Horizon will be accessible from. Feel free
| to change this path to anything you like. Note that the URI will not
| affect the paths of its internal API that aren't exposed to users.
|
*/
'path' => 'horizon',
/*
|--------------------------------------------------------------------------
| Horizon Redis Connection
|--------------------------------------------------------------------------
|
| This is the name of the Redis connection where Horizon will store the
| meta information required for it to function. It includes the list
| of supervisors, failed jobs, job metrics, and other information.
|
*/
'use' => 'default',
/*
|--------------------------------------------------------------------------
| Horizon Redis Prefix
|--------------------------------------------------------------------------
|
| This prefix will be used when storing all Horizon data in Redis. You
| may modify the prefix when you are running multiple installations
| of Horizon on the same server so that they don't have problems.
|
*/
'prefix' => env('HORIZON_PREFIX', 'horizon:'),
/*
|--------------------------------------------------------------------------
| Horizon Route Middleware
|--------------------------------------------------------------------------
|
| These middleware will get attached onto each Horizon route, giving you
| the chance to add your own middleware to this list or change any of
| the existing middleware. Or, you can simply stick with this list.
|
*/
'middleware' => ['web'],
/*
|--------------------------------------------------------------------------
| Queue Wait Time Thresholds
|--------------------------------------------------------------------------
|
| This option allows you to configure when the LongWaitDetected event
| will be fired. Every connection / queue combination may have its
| own, unique threshold (in seconds) before this event is fired.
|
*/
'waits' => [
'redis:default' => 600,
'DtCubeTuyaDeviceData' => 600,
'DtCubeTuyaDeviceEvent' => 600,
'DtCubeOrderAutoClose' => 600,
'DtCubeDeaultQueue' => 600,
'DtCubeAzureData' => 600,
'DtCubeAzureEvent' => 600
],
/*
|--------------------------------------------------------------------------
| Job Trimming Times
|--------------------------------------------------------------------------
|
| Here you can configure for how long (in minutes) you desire Horizon to
| persist the recent and failed jobs. Typically, recent jobs are kept
| for one hour while all failed jobs are stored for an entire week.
|
*/
'trim' => [
'recent' => 60,
'completed' => 60,
'recent_failed' => 10080,
'failed' => 10080,
'monitored' => 10080,
],
/*
|--------------------------------------------------------------------------
| Fast Termination
|--------------------------------------------------------------------------
|
| When this option is enabled, Horizon's "terminate" command will not
| wait on all of the workers to terminate unless the --wait option
| is provided. Fast termination can shorten deployment delay by
| allowing a new instance of Horizon to start while the last
| instance will continue to terminate each of its workers.
|
*/
'fast_termination' => false,
/*
|--------------------------------------------------------------------------
| Memory Limit (MB)
|--------------------------------------------------------------------------
|
| This value describes the maximum amount of memory the Horizon worker
| may consume before it is terminated and restarted. You should set
| this value according to the resources available to your server.
|
*/
'memory_limit' => 64,
/*
|--------------------------------------------------------------------------
| Queue Worker Configuration
|--------------------------------------------------------------------------
|
| Here you may define the queue worker settings used by your application
| in all environments. These supervisors and settings handle all your
| queued jobs and will be provisioned by Horizon during deployment.
|
*/
'environments' => [
'production' => [
'production-1' => [
'connection' => 'rabbitmq',
'queue' => [],
'balance' => 'auto',
'processes' => 50,
'tries' => 3,
],
],
'local' => [
'local-1' => [
'connection' => 'rabbitmq',
'queue' => [
'test1',
'test2',
],
'balance' => 'auto',
'minProcesses' => 1,
'minProcesses' => 10,
'tries' => 3,
],
'local-2' => [
'connection' => 'rabbitmq',
'queue' => [
'test6',
'test7',
],
'balance' => 'auto',
'minProcesses' => 1,
'minProcesses' => 10,
'tries' => 3,
],
],
],
];
?>
至此安装配置完毕,浏览器打开 https://pi.fogbow.cn/horizon 访问控制台
运行 Horizon
如果你已经在配置文件 config/horizon.php 中配置过工作进程,就可以使用 Artisan 命令 horizon 来启动 Horizon,该命令会启动所有配置的工作进程:
Linux 命令后面加 & 表示设置此进程为后台进程
php artisan horizon
你可以使用 Artisan 命令 horizon:pause 和 horizon:continue 来暂停或继续处理队列任务:
php artisan horizon:pause
php artisan horizon:continue
你还可以使用 Artisan 命令 horizon:terminate 来优雅地终止 Horizon 主进程 —— Horizon 会在所有当前正在执行的任务全部完成后退出:
php artisan horizon:terminate
这个时候后可以把 php artisan queue:work
省略
ok✌️
【相关链接】
(一)CentOS7安装RabbitMQ https://blog.youkuaiyun.com/weixin_37689230/article/details/112276503
(二)laravel整合rabbitmq消息队列 https://blog.youkuaiyun.com/weixin_37689230/article/details/112321216
(三)Horizon 队列管理工具 https://blog.youkuaiyun.com/weixin_37689230/article/details/112366571
(四)RabbitMQ基础知识 https://blog.youkuaiyun.com/weixin_37689230/article/details/112542844