pm2+laravel+rabbitmq队列进程监控及配置

Laravel+pm2+rabbitmq创建高性能队列,并用pm2进行队列监控

 

简介

之前一直有研究rabbitmq,而且也用过supervisor,lz感觉实在是太难用了,真的是,老断,所以果断弃用,而改用pm2,一方面便于监控,另一方面,看起来也很友好,好了,废话不多说,直接上代码;

操作步骤:

一.你肯定要有laravel框架,这里如何安装就不赘述了;

二.安装pm2,在此以centos为例:使用npm全局安装npm i pm2 -g


[if !supportLists]三.[endif]安装rabbitmq,具体安装教程请参考文档:https://www.linuxprobe.com/install-rabbitmq-on-centos-7.html

[if !supportLists]四.[endif]

好了一切准备就绪,然后laravel里面配置:

1、Composer安装laravel-queue-rabbitmqcomposer require vladimir-yuldashev/laravel-queue-rabbitmq2、在config/app.php文件中,providers中添加:VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,3、在app/config/queue.php配置文件中的connections数组中加入以下配置

'rabbitmq' => [


            'driver' => 'rabbitmq',


            '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),

            ],


        ],

4、修改 .env 文件

QUEUE_CONNECTION=rabbitmq    #这个配置env一般会有先找到修改为这个

#以下是新增配置

RABBITMQ_HOST=rabbitmq  #mq的服务器地址,我这里用的是laradock,具体的就具体修改咯RABBITMQ_PORT=5672  #mq的端口RABBITMQ_VHOST=/RABBITMQ_LOGIN=guest    #mq的登录名RABBITMQ_PASSWORD=guest   #mq的密码RABBITMQ_QUEUE=queue_name   #mq的队列名称

5、创建任务类php artisan make:job Queue执行之后会生成一个文件app/Jobs/Queue.php

例子:

<?php

namespace App\Jobs;

use App\Entities\Posts;use Illuminate\Bus\Queueable;use Illuminate\Foundation\Bus\Dispatchable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;

class Queue  implements ShouldQueue{

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    private $data;


    /**

     * Queue constructor.

     * @param $data

     */

    public function __construct($data)

    {

        $this->data = $data;

    }


    /**

     * Execute the job.

     *

     * @return void

     */

    public function handle()

    {


        try{

            $insert = [

                'title'=>$this->data->title,

                'author_id'=>$this->data->author_id,

                'content'=>$this->data->content,

                'description'=>$this->data->description,

            ];

            $result = Posts::create($insert);

            echo json_encode(['code' => 200, 'msg' => $result]);

        }catch (\Exception $exception) {

            echo json_encode(['code'=>0,'msg'=>$exception->getMessage()]);

        }


    }}

6、生产,把数据放进mq队列

<?php

namespace App\Http\Controllers;

use App\Entities\CostaNews;use App\Jobs\Queue;

class IndexController extends Controller{


    public function index()

    {

        $data = CostaNews::get();

        foreach ($data as $item) {

            $this->dispatch(new Queue($item));

        }

        return response()->json(['code'=>0, 'msg'=>"success"]);

    }

}

7、消费队列执行命令进行消费:php artisan queue:work rabbitmq效果如下:

root@9e99cf9fba73:/var/www/blog# php artisan  queue:work rabbitmq[2018-12-24 07:34:32][5c208bf66e63b3.56379160] Processing: App\Jobs\Queue{"code":200,"msg":{"title":1,"author_id":2,"content":"\u5185\u5bb9","description":"\u63cf\u8ff0","updated_at":"2018-12-24 07:34:32","created_at":"2018-12-24 07:34:32","id":1}}[2018-12-24 07:34:32][5c208bf66e63b3.56379160] Processed:  App\Jobs\Queue[2018-12-24 07:34:32][5c208bf66ff7c3.20969590] Processing: App\Jobs\Queue{"code":200,"msg":{"title":2,"author_id":2,"content":"\u5185\u5bb92","description":"\u63cf\u8ff02","updated_at":"2018-12-24 07:34:32","created_at":"2018-12-24 07:34:32","id":2}}[2018-12-24 07:34:32][5c208bf66ff7c3.20969590] Processed:  App\Jobs\Queue[2018-12-24 07:34:32][5c208bf6702695.93123122] Processing: App\Jobs\Queue{"code":200,"msg":{"title":3,"author_id":2,"content":"\u5185\u5bb93","description":"\u63cf\u8ff03","updated_at":"2018-12-24 07:34:32","created_at":"2018-12-24 07:34:32","id":3}}[2018-12-24 07:34:32][5c208bf6702695.93123122] Processed:  App\Jobs\Queue[2018-12-24 07:34:32][5c208bf6706e24.78015170] Processing: App\Jobs\Queue{"code":200,"msg":{"title":4,"author_id":2,"content":"\u5185\u5bb94","description":"\u63cf\u8ff04","updated_at":"2018-12-24 07:34:32","created_at":"2018-12-24 07:34:32","id":4}}[2018-12-24 07:34:32][5c208bf6706e24.78015170] Processed:  App\Jobs\Queue[2018-12-24 07:34:32][5c208bf6709be0.07998731] Processing: App\Jobs\Queue{"code":200,"msg":{"title":5,"author_id":2,"content":"\u5185\u5bb95","description":"\u63cf\u8ff05","updated_at":"2018-12-24 07:34:32","created_at":"2018-12-24 07:34:32","id":5}}[2018-12-24 07:34:32][5c208bf6709be0.07998731] Processed:  App\Jobs\Queue


五.以上这些测试好后,才可进行下一步配置,就是pm2的配置,使用以下命令:

pm2 start 你的项目路径/artisan --name laravel-worker --interpreter php -- queue:work --daemon

结尾:

  以上这些配置好后,你就大功告成了,就可以用pm2监控队列了,当然,配置过程中也有可能遇到困难,欢迎留言,我也会找个时间来解答!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343