官网地址:https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq#v10.2.0
安装
1.安装插件
composer require vladimir-yuldashev/laravel-queue-rabbitmq`
报错:
因为我是laravel7的版本,因此找对应的版本
composer require vladimir-yuldashev/laravel-queue-rabbitmq:^10.2
还是报错,再来
composer require vladimir-yuldashev/laravel-queue-rabbitmq:^10.2 --ignore-platform-reqs
终于安装成功了。
配置
这个配置根据自己的实际情况配置就可以
'rabbitmq' => [
'driver' => 'rabbitmq',
/*
* Set to "horizon" if you wish to use Laravel Horizon.
*/
'worker' => env('RABBITMQ_WORKER', 'default'),
'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),
],
],
QUEUE_DRIVER=rabbitmq # 走rabbitmq
RABBITMQ_HOST=rabbitmq # 我的容器是 rabbit 你们要是ip就写ip
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=guest # 账号
RABBITMQ_PASSWORD=guest # 密码
RABBITMQ_QUEUE=test_queue # 队列名称。如果你没有它会默认创建 Exchanges和Queue
#ssl连接配置
#RABBITMQ_SSL=true
#RABBITMQ_SSL_CAFILE=/path_to_your_ca_file
#RABBITMQ_SSL_LOCALCERT=
#RABBITMQ_SSL_PASSPHRASE=
#RABBITMQ_SSL_KEY=
队列
我本地使用的docker环境
安装很简单
1.先把.env里面用到amqp的变量都改成true
WORKSPACE_INSTALL_AMQP=true
2.重现build workspace,这样本地就有amqp扩展了
docker-compose build workspace
怎么查看本地amqp的扩展是否安装成功了,php_info()
3.安装rabbitmq队列
先build生成镜像
docker-compose build rabbitmq
再启容器
docker-compose up -d rabbitmq
如果启动成功了,万事大吉,但……
我这里启动了以后几秒就死掉了
查看日志
docker logs laradock_rabbitmq_1
错误信息:“Cookie file /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only”
解决办法:改为${DATA_PATH_HOST}/rabbitmq:/var/lib/rabbitmq/mnesia,然后重新build,重新启动就可以了。
测试
1.代码
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RabbitQueue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
info("================我来了============rabbit==============");
}
}
RabbitQueue::dispatch();
2.启队列
winpty docker-compose exec workspace bash
php artisan queue:work rabbitmq
3.运行方法,看log里面是否有日志
啦啦啦,终于完成了……