laravel框架 - rabbitmq消息队列(使用laravel-queue-rabbitmq)

参考文档:https://learnku.com/docs/laravel/8.x/queues/9398

https://packagist.org/packages/vladimir-yuldashev/laravel-queue-rabbitmq

1、Composer 安装 laravel-queue-rabbitmq,安装注意应用包对应的laravel版本。
 

composer require vladimir-yuldashev/laravel-queue-rabbitmq

2、在 config/app.php 文件中,providers 中添加:

VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,

3、在 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),
 
                    /*
                     * Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html
                     */
 
                    // 'type' => env('RABBITMQ_EXCHANGE_TYPE', \Interop\Amqp\AmqpTopic::TYPE_DIRECT),
                    'type' => env('RABBITMQ_EXCHANGE_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),
            ],
 
        ],

 4、修改 .env 文件

#rabbitmq
QUEUE_CONNECTION=rabbitmq    #这个配置env一般会有先找到修改为这个
 
RABBITMQ_HOST=localhost	   		#localhost mq的服务器地址
RABBITMQ_PORT=5672				#mq的端口
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=denglei         	#mq的登录名
RABBITMQ_PASSWORD=123456      	#mq的密码
RABBITMQ_QUEUE=test101      	#mq的队列名称
QOS_PREFETCH_COUNT=100			#mq的每次消费数量
#RABBITMQ_WORKER=horizon

5、创建任务类

php artisan make:job Queue

执行之后会生成一个文件 app/Jobs/Queue.php

例子:

data = $data;
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 处理业务...
        sleep(1);
        $this->data['exec_time'] = date('Y-m-d H:i:s');
 
        Log::info($this->data);
        echo json_encode($this->data);
    }
}
 
?>

6、生产,把数据放进 mq 队列 

all();
             $params['id']=rand(1,999);
             $params['mq']='Queue';
             $params['request_time']=date('Y-m-d H:i:s');
             // for ($i=0; $i < 5; $i++) { 
             //     $this->dispatch(new Queue($params));
             // }
            $this->dispatch(new Queue($params));
 
            Log::info("\n\n\n".'-----'.$params['id']);
            
            return ['code' => Constants::SUCCESS, 'msg' => 'success','data' => $params['id']];
 
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            return ['code' => Constants::CURL_ERROR, 'msg' => $e->getMessage()];
        }
    }
}
 
?>

7、消费队列
执行命令进行消费:

php artisan queue:work rabbitmq

效果如下: 

laravel框架 - rabbitmq消息队列(使用laravel-queue-rabbitmq)_第1张图片

laravel框架 - rabbitmq消息队列(使用laravel-queue-rabbitmq)_第2张图片 

注意:使用这个 laravel-queue-rabbitmq 这个包需要开启 sockets 拓展,不然会报错

【相关链接】

(一)CentOS7安装RabbitMQ  https://blog.csdn.net/weixin_37689230/article/details/112276503
(二)laravel整合rabbitmq消息队列  https://blog.csdn.net/weixin_37689230/article/details/112321216
(三)Horizon 队列管理工具  https://blog.csdn.net/weixin_37689230/article/details/112366571
(四)RabbitMQ基础知识  https://blog.csdn.net/weixin_37689230/article/details/112542844
 

 

你可能感兴趣的:(PHP,#,laravel,laravel,rabbitmq,php)