安装Laravel的RabbitMQ队列驱动:

composer require vladimir-yuldashev/laravel-queue-rabbitmq

env文件配置 

#rabbitmq

QUEUE_CONNECTION=rabbitmq #修改一下

RABBITMQ_HOST=192.168.11.4 #要连接的主机名

RABBITMQ_PORT=5671 #端口号

RABBITMQ_VHOST=/

RABBITMQ_USER=root #mq登录名

RABBITMQ_PASSWORD=root #mq登录密码

RABBITMQ_QUEUE=test11111 #mq连接的名称(随便写)

创建一个新的队列任务类 

php artisan make:job ProcessPodcast

在ProcessPodcast任务类中实现handle方法 

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;

use Illuminate\Support\Facades\Log;

class ProcessPodcast implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private $data;

/**

* Create a new job instance.

*/

public function __construct($data)

{

$this->data = $data;

}

/**

* Execute the job.

*/

public function handle(): void

{

// 处理业务...

echo json_encode(['1----'.date('Y-m-d H:i:s',time())]);

sleep(5);

echo json_encode(['2----'.date('Y-m-d H:i:s',time())]);

sleep(5);

echo json_encode(['3----'.date('Y-m-d H:i:s',time())]);

sleep(5);

echo json_encode(['4----'.date('Y-m-d H:i:s',time())]);

$this->data['exec_time'] = date('Y-m-d H:i:s');

Log::info($this->data);

echo json_encode($this->data);

}

}

配置  config/app.php 增加

VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,

配置 config/queue.php 增加

'rabbitmq' => [

'driver' => 'rabbitmq',

'host' => env('RABBITMQ_HOST', '192.168.11.4'),

'port' => env('RABBITMQ_PORT', 5671),

'vhost' => env('RABBITMQ_VHOST', '/'),

'login' => env('RABBITMQ_LOGIN', 'root'),

'password' => env('RABBITMQ_PASSWORD', 'root'),

'queue' => env('RABBITMQ_QUEUE'), // name of the default queue,

'exchange_declare' => env('RABBITMQ_EXCHANGE_DECLARE', true), // create the exchange if not exists

'queue_declare_bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true), // create the queue if not exists and bind to the exchange

'queue_params' => [

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

],

'exchange_params' => [

'name' => env('RABBITMQ_EXCHANGE_NAME', null),

'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'), // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html

'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),

'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true), // the exchange will survive server restarts

'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),

],

],

控制器 TestController

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use App\Jobs\ProcessPodcast;

class TestController extends Controller

{

/**

* 队列

* */

public function rabbitmqTest(){

// 在控制器或其他逻辑中

$job = new ProcessPodcast(['zh'=>1]);

dd(dispatch($job));

}

}

运行 

php artisan queue:work

 ACCESS_REFUSED -使用身份验证机制AMQPLAIN时登录被拒绝

解决方案,ACCESS_REFUSED -使用身份验证机制AMQPLAIN时登录被拒绝-CSDN博客

 

相关阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。

大家都在找:

laravel:lARAVEL搬家教程

rabbitmq:rabbitMQ server可以卸载吗

php:ph破解版

大家都在看: