Lumen安装RabbitMQ包第二种方法并消费

2019-03-21  本文已影响0人  隔岸坐看云卷云舒

首先你的lumen版本号必须大于等于5.6.(5,5.版本无法安装)

如果不满足必须条件,请修改 composer.json文件

将"laravel/lumen-framework": "5.5.*" 修改为 "laravel/lumen-framework": "5.7.*",
conposer update

第一步:

    composer require vladimir-yuldashev/laravel-queue-rabbitmq

第二步:

请修改app.php引导文件 注册
    $app->register(VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class);

修改config/queue.php文件

在connections 数组中的连接类型中添加如下:

    'rabbitmq' => [

    'driver' => 'rabbitmq',

    'dsn' => env('RABBITMQ_DSN', null),

    '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', 'username'), //你的队列账户

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

    'queue' => env('RABBITMQ_QUEUE', 'default'),

    'options' => [

        'exchange' => [

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

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

            'declare' => env('RABBITMQ_EXCHANGE_DECLARE', true),

        ],

        'queue' => [

            'declare' => env('RABBITMQ_QUEUE_DECLARE', true),

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

        ],

    ],

    'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5),

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

    ],

],

生产Job文件:

<?php
/*
    *author:shumingjian
    *time 2019/3/21 1:31 PM
    *All rights reserved
*/
namespace App\Jobs;

use Illuminate\Support\Facades\Log;

class RabbitMQJob extends Job

{

    public $data;

    /**

* RabbitMQJob constructor.

    * @param $data

*/

    public function __construct($data)

{

        $this->data = $data;

    }

    /**

    * @author mjShu

*/

    public function handle()

{

        $data = json_decode($this->data,true);

        echo $data['a'];

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

    }

}

测试:

生产者推送消息

image

执行命令:

php artisan queue:work --queue=aaa

此刻应该如图所示:

消费者正在消费

image
上一篇下一篇

猜你喜欢

热点阅读