博主看了一些消息队列,大多数消息队列都没有对 php
比较友好的 SDK
,这边 rabbitmq
算是一个不错的消息队列,给大家介绍一下在 Laravel
中如何使用。
安装rabbitmq和amqp
代码语言:javascript复制docker pull rabbitmq:3.8.3-management
docker run -d --hostname my-rabbit --network docker_server_default --name rabbit -p 5672:5672 -p 15672:15672 -p 25672:25672 -v /Users/{user}/data/rabbitmq:/var/lib/rabbitmq rabbitmq:3.8.3-management
博主这边使用 docker
快速安装,给大家介绍一下以上命令:
rabbitmq
镜像有两种 我们拉带web管理界面的,即management
--hostname
设置该节点默认主机名称 避免随机主机名--network
因为我前面php是一个网络,现在要把rabbitmq加入前面的网络-v
即是挂载,避免容器数据丢失-p
指定端口映射,此处5672
端口用于程序访问RabbitMQ
的接口;15672
端口是用于RabbitMQ
可视化UI
管理的暴露端口,可以通过在浏览器中输入localhost:15672
访问RabbitMQ
的管理控制台,默认登陆账号密码是guest,guest
;25672
端口用于RabbitMQ
集群各节点之间的通讯。- 查看容器状态
- 可以登录查看一下
localhost:15672
- php安装amqp
PHP
使用 AMQP
协议来连接 Rabbitmq
, AMQP
协议即 “Advanced Message Queuing Protocol ”
,高级消息队列协议。
apt-get update
apt-get install php-amqp
博主这边是ubuntu
镜像,直接就apt
了
这下都准备好啦,准备在 laravel
里面配置
在Laravel中配置 Rabbitmq
- 安装
我是Laravel5.5
,按照文档上说我只能用6版本
composer require vladimir-yuldashev/laravel-queue-rabbitmq=6.0
这个包依赖illuminate/queue
,在composer.json
里面一并添加
"illuminate/queue": "^5.5.44",
- 在
config/app.php
文件中,providers
中添加
VladimirYuldashevLaravelQueueRabbitMQLaravelQueueRabbitMQServiceProvider::class,
- 在
app/config/queue.php
配置文件中的connections
数组中加入以下配置
'rabbitmq' => [
'driver' => 'rabbitmq',
'dsn' => env('RABBITMQ_DSN', null),
/*
* Could be one a class that implements InteropAmqpAmqpConnectionFactory for example:
* - EnqueueAmqpExtAmqpConnectionFactory if you install enqueue/amqp-ext
* - EnqueueAmqpLibAmqpConnectionFactory if you install enqueue/amqp-lib
* - EnqueueAmqpBunnyAmqpConnectionFactory if you install enqueue/amqp-bunny
*/
'factory_class' => EnqueueAmqpLibAmqpConnectionFactory::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', InteropAmqpAmqpTopic::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),
],
],
- .env
QUEUE_DRIVER=rabbitmq # 走rabbitmq
RABBITMQ_HOST=rabbit # 我的容器是 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=
- 创建任务
QueueJob
php artisan make:job Queue
执行之后会生成一个文件 app/Jobs/Queue.php
<?php
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
class Queue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $id;
private $title;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($id, $title)
{
//
$this->id = $id;
$this->title = $title;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
echo 'id =='. $this->id;
app('log')->info('id ===>'. $this->id. 'title ====>' . $this->title);
}
}
- 生产,把数据放进
mq
队列
<?php
/**
* Created by 憧憬.
*/
namespace AppHttpControllersApp;
use AppHttpControllersController;
use AppJobsQueue;
class IndexController extends Controller
{
public function index()
{
echo phpinfo();
$arr = [
['id' => 1, 'title' => '张三'],
['id' => 2, 'title' => '李四'],
['id' => 3, 'title' => '王五'],
];
foreach ($arr as $v) {
$queue = new Queue($v['id'], $v['title']);
$this->dispatch($queue);
}
return response(0, '200');
}
}
- 消费队列
php artisan queue:work rabbitmq --queue=test_queue --tries=3
- 也可以在web端查看连接和duilie
好啦就到这里了,大家也可以尝试一下。