1、安装rabbitmq请参考教程RabbitMq安装教程(超详细)_普通网友的博客-CSDN博客_rabbitmq安装
2、php安装amqp扩展
点击链接PECL :: Package :: amqp 选择对应PHP版本的扩展包,由于本文用的Laravel6最高支持laravel-queue-rabbitmq:10,建议选择下载PHP7.2
我的本地是phpstudy,首先将php_amqp.dll和php_amqp.pdb放到phpStudy安装目录\Extensions\php\php-7.2.9-nts\ext目录下,再将rabbitmq.4.dll和rabbitmq.4.pdb放到PHP安装目录,即和php.exe在一个目录,然后在php.ini增加配置然后重启。
extension=php_amqp
最后使用php -m命令查看是否有amqp,有则说明安装成功
3、Laravel安装vladimir-yuldashev/laravel-queue-rabbitmq
点击https://github.com/vyuldashev/laravel-queue-rabbitmq查看laravel对应版本支持,本文用的是laravel6.5,所以这里安装laravel-queue-rabbitmq:10
composer require vladimir-yuldashev/laravel-queue-rabbitmq:^10
注意:如果出现以下错误,则需要开启sockets扩展。
- php-amqplib/php-amqplib[v2.11.0, ..., 2.x-dev] require ext-sockets * -> it is missing from your system. Install or enable PHP's sockets extension.
如果composer出现Installation failed, reverting ./composer.json and ./composer.lock to their original content,请参考https://www.cnblogs.com/zhangyouwu/p/15499294.html
4、Laravel使用rabbitmq示例
①修改config/queue.php
文件在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),
],
],
/*
* Set to "horizon" if you wish to use Laravel Horizon.
*/
'worker' => env('RABBITMQ_WORKER', 'default'),
],
②在.env增加对应的参数设置
③使用artisan
创建任务类php artisan make:job Queue
④在对应的方法使用dispatch调用即可,详细请参考链接队列 | 综合话题 |《Laravel 6 中文文档 6.x》| Laravel China 社区