Laravel5.8调试信息队列RabbitMQ

原创
小哥 3年前 (2022-11-05) 阅读数 10 #PHP
文章标签 Laravel

windows安装php扩展amqp扩展:

http://pecl.php.net/package/amqp http://pecl.php.net/package/amqp

最后重启服务器。

安装laravel-queue-rabbitmq

composer require vladimir-yuldashev/laravel-queue-rabbitmq="7.2"

在Laravel中配置 Rabbitmq
安装

composer require vladimir-yuldashev/laravel-queue-rabbitmq=7.2
这个包依赖illuminate/queue,在composer.json里面一并添加
在 config/app.php 文件中,providers 中添加
VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,
在 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),

/*

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

/*

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 theres an error communicating with rabbitmq
  • If set to false, itll 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_CONNECTION=rabbitmq
RABBITMQ_HOST=192.168.91.165
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=admin
RABBITMQ_PASSWORD=123456
RABBITMQ_QUEUE=test123 # 队列名称。如果你没有它会默认创建 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 App\Jobs;

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

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 App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Jobs\Queue;

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=test123 --tries=3

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除