0
点赞
收藏
分享

微信扫一扫

laravel之horizon队列管理系统


horizon简介

horizon 为您的 Laravel Redis 队列提供了漂亮的仪表板和代码驱动配置。Horizon 允许您轻松监控队列系统的关键指标,例如作业吞吐量,运行时和作业失败。

您的所有工作人员配置都存储在一个简单的配置文件中,使您的配置可以保持在整个团队可以协作的源代码管理中。

laravel之horizon队列管理系统_redis

Horizon 安装

要保证你的 php 扩展 pecl 开启,如果没有开启则安装失败
composer require laravel/horizon

安装 Horizon 后,使用 Artisan 命令发布其资产:
php artisan horizon:install

如果没有报错,请忽略安装 pecl 步骤
注:由于 Horizon 使用了异步进程信号,所以 PHP 7.1+ 以上版本才可以使用。

[root@bogon laravel5]# composer require laravel/horizon
You are running composer with Xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^3.7 for laravel/horizon
./composer.json has been updated
Running composer update laravel/horizon
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking cakephp/chronos (1.3.0)
  - Locking laravel/horizon (v3.7.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing cakephp/chronos (1.3.0): Extracting archive
  - Installing laravel/horizon (v3.7.2): Extracting archive
Package jakub-onderka/php-console-color is abandoned, you should avoid using it. Use php-parallel-lint/php-console-color instead.
Package jakub-onderka/php-console-highlighter is abandoned, you should avoid using it. Use php-parallel-lint/php-console-highlighter instead.
Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested.
Package moontoast/math is abandoned, you should avoid using it. Use brick/math instead.
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Generating optimized autoload files
Class App\Http\Services\Common\CommonService located in ./app/Http/Services/Product/ProductService.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: barryvdh/laravel-ide-helper
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: dcat/laravel-admin
Discovered Package: evan766/laravel-debugbar
Discovered Package: fideloper/proxy
Discovered Package: laravel/horizon
Discovered Package: laravel/telescope
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: peinhu/aetherupload-laravel
Discovered Package: tymon/jwt-auth
Package manifest generated successfully.
68 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
[root@bogon laravel5]# php artisan horizon:install
Publishing Horizon Service Provider...
Publishing Horizon Assets...
Publishing Horizon Configuration...
Horizon scaffolding installed successfully.
[root@bogon laravel5]#

vendor:publish 发布前端资源 生成config/horizon.php
php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"

Horizon 预览

发布好前端资源后,主配置文件就会出现在 config/horizon.php。在这个配置文件中,你可以配置队列进程选项以及 > 每个包含目的描述的配置项,使用 Horizon 前可浏览下这个配置文件。

浏览器访问 http://localhost/horizon

laravel之horizon队列管理系统_laravel_02

运行 Horizon

php artisan horizon

laravel之horizon队列管理系统_laravel_03


laravel之horizon队列管理系统_php_04


比如我后台执行

php artisan horizon &

pecl 安装

php -v, 找你对应的 php 源码包 pecl
tar -zxvf php-7.1.30.tar.gz
编译安装
cd php-7.1.30/ext/pcntl
phpize

laravel之horizon队列管理系统_php_05

phpize
./configure --enable-pcntl --with-php-config=/usr/bin/php-config
make && make install
编辑 php.ini 文件,加入扩展 pcntcl.so

使用 Artisan 命令 vendor:publish 发布前端资源:

php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
Copied Directory [/vendor/laravel/horizon/public] To [/public/vendor/horizon]
Publishing complete.

Horizon 需要常驻运行,执行以下命令启动:

php artisan horizon

laravel之horizon队列管理系统

horizon 为您的 Laravel Redis 队列提供了漂亮的仪表板和代码驱动配置。Horizon 允许您轻松监控队列系统的关键指标,例如作业吞吐量,运行时和作业失败。

您的所有工作人员配置都存储在一个简单的配置文件中,使您的配置可以保持在整个团队可以协作的源代码管理中。

版本:5.6,php7.2,redis3
要求由于 Horizon 使用了异步进程信号,所以 PHP 7.1+ 以上版本才可以使用。

安装

composer require laravel/horizon

vendor:publish 发布前端资源

php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"

生成config/horizon.php

打开浏览器

你的访问地址/horizon
比如:http://192.168.91.130:85/horizon/

设置权限访问

例子:app/Provides/AppServiceProvider.php

比如我这里必须前面带名字为kongqi才可以访问,现实中,你需要写权限,比如认证之后的用户。

<?php

namespace App\Providers;

use  Illuminate\Http\Request;
use Laravel\Horizon\Horizon;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Request $request)
    {
        //
        Horizon::auth(function ($request) {
            // return true / false;
            if($request->name=='kongqi')
            {
              return true;
            }
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

这个时候,不写任何参数,访问

http://192.168.91.130:85/horizon/dashboard

laravel之horizon队列管理系统_laravel_06


带参数

http://192.168.91.130:85/horizon/dashboard?name=kongqi

laravel之horizon队列管理系统_laravel_07

horizon命令

运行 Horizon

php artisan horizon

比如我后台执行

php artisan horizon &

暂停或继续处理队列任务

php artisan horizon:pause

php artisan horizon:continue

终止 Horizon 主进程

php artisan horizon:terminate

Supervisor 配置

[program:horizon]
process_name=%(program_name)s
command=php /www/laravel56/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/www/log/horizon.log

command这个执行的命令目录,写你自己的,这个是我的,其他就配置日志,写你自己的目录
如果你还没有安装supervisor可以查看我的文章

已经监听了

写个队列测试下

php artisan make:job RedisTestJobQueue
php artisan make:controller RedisTestController

如果首次运行,需要安装,请查看官方包,查看版本,我现在是是5.6就是最新的拉,所以不指定版本好

composer require predis/predis 
//laravel5.2
composer require predis/predis ~1.0

RedisTestJobQueue.php

<?php

namespace App\Jobs;

use Log;
use Redis;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class RedisTestJobQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
     protected $redis_name;
    /**
     * Create a new job instance.
     *
     * @return void
     */
     public function __construct($redis_name)
      {
          //
          $this->redis_name=$redis_name;
      }

    /**
     * Execute the job.
     *
     * @return void
     */
     public function handle(Redis $redis)
   {

       $r=$redis::rPop($this->redis_name);

       $r?Log::info('del: '. $r .' is ok'):Log::info('del: '. $r .' is fail');;

   }
}

RedisTestController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use App\Jobs\RedisTestJobQueue;

class RedisTestController extends Controller
{
    public $redis_name='testredis';
    public function index(){

        $r=Redis::rPush($this->redis_name,str_random());
        echo $r;
    }
    public function runJob(){
      $job = (new RedisTestJobQueue($this->redis_name));
      $this->dispatch($job);
    }
}

定义模拟路由

Route::get('redisJob',['uses'=>'RedisTestController @index']);
Route::get('redisJob/run',['uses'=>'RedisTestController @runJob']);

浏览器打开index写入redis
http://192.168.91.130:85/redisJob
浏览器打开runJob调用队列
http://192.168.91.130:85/redisJob/run

监听写执行队列

php artisan queue:listen

最后horizon

laravel之horizon队列管理系统_laravel_08


日志

laravel之horizon队列管理系统_laravel_09

标签

Horizon 允许分配”标签”到任务
Horizon 会基于附加到任务的 Eloquent 模型为大部分任务以智能的方式自动打上标签
附带了一个 id 为 1 的 App\Video 实例,它将会自动打上 App\Video:1 的标签
上面的则会生成一个Hash类型的horizon:1,horizon:2,horizon:3…等

手动打标签
在Job增加一个方法

public function tags()
    {
        return ['render', 'video:'.$this->video->id];
    }

监控

Horizon 提供了一个监控后台查看任务和队列的等待时间和吞吐量信息

protected function schedule(Schedule $schedule)
{
    $schedule->command('horizon:snapshot')->everyFiveMinutes();
}

参考文献

https://www.jianshu.com/p/f8de5b9ddfc1


举报

相关推荐

0 条评论