前言
Laravel队列默认使用的是sync作为队列驱动(可以看config/queue.php配置文件),使用RabbitMQ作为驱动,需要引入vladimir-yuldashev/laravel-queue-rabbitmq包,这个貌似仅仅作为laravel队列的RabbitMQ驱动适配包,如果需要完全使用RabbitMQ,需要使用php-amqplib/php-amqplib这个包,这个包是个通用包,并不适配laravel。
整合 RabbitMQ 消息队列(laravel 5.6)
laravel-queue-rabbitmq文档
1、Composer 安装 laravel-queue-rabbitmq
1 |
composer require vladimir-yuldashev/laravel-queue-rabbitmq |
2、在 config/app.php 文件中,providers 中添加:
1 |
VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class, |
3、在 app/config/queue.php 配置文件中的 connections 数组中加入以下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
'connections' => [ // ... 'rabbitmq' => [ 'driver' => 'rabbitmq', 'queue' => env('RABBITMQ_QUEUE', 'default'), 'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class, 'hosts' => [ [ 'host' => env('RABBITMQ_HOST', '127.0.0.1'), 'port' => env('RABBITMQ_PORT', 5672), 'user' => env('RABBITMQ_USER', 'guest'), 'password' => env('RABBITMQ_PASSWORD', 'guest'), 'vhost' => env('RABBITMQ_VHOST', '/'), ], ], 'options' => [ 'ssl_options' => [ '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), ], 'queue' => [ 'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class, ], ], /* * Set to "horizon" if you wish to use Laravel Horizon. */ 'worker' => env('RABBITMQ_WORKER', 'default'), ], // ... ], |
4、修改 .env 文件
1 2 3 4 5 6 7 8 9 10 |
QUEUE_CONNECTION=rabbitmq #使用RabbitMQ作为驱动,也可以改为redis、sync等 #以下是新增配置 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、创建任务类
1 |
php artisan make:job Queue |
执行之后会生成一个文件 app/Jobs/Queue.php
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?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 队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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、消费队列
执行命令进行消费:
1 |
php artisan queue:work |
或者
1 |
php artisan queue:listen --queue=test |
--queue
:监听队列名
效果如下:
1 2 3 4 5 6 7 8 9 10 11 |
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 |
参考: