Laravel使用RabbitMQ的方法示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-26 11:35:16 浏览: 评论:0
这篇文章主要介绍了Laravel使用RabbitMQ的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
导语
RabbitMQ 想必大家都有了解,不做多介绍来。这里实现的是用 RabbitMQ 作为 Larvel 队列的驱动,替代 Redis。下面以 Laradock 中安装示例。
安装
切换到 laradock 目录,将 .env 中关于 INSTALL_AMQP 的值修改为 true
docker-compose stop workspace php-fpm php-worker
docker-compose build workspace php-fpm php-worker rabbitmq
docker-compose up -d workspace php-fpm php-worker rabbitmq
扩展包安装以及配置
进入到 workspace 容器中,在项目目录安装扩展包 composer require vladimir-yuldashev/laravel-queue-rabbitmq
接下来在 config/queue.php 文件中 connections 添加 rabbitmq 配置,根据情况自行修改
- 'rabbitmq' => [
- 'driver' => 'rabbitmq',
- /*
- * Set to "horizon" if you wish to use Laravel Horizon.
- */
- 'worker' => env('RABBITMQ_WORKER', 'default'),
- '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),
- '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_CONNECTION 为 rabbitmq ,并添加以下值
- RABBITMQ_WORKER=horizon
- RABBITMQ_HOST=rabbitmq
- RABBITMQ_PORT=5672
- RABBITMQ_LOGIN=guest
- RABBITMQ_PASSWORD=guest
- RABBITMQ_QUEUE=default
有两个值说明一下,因为是在 Laradock 中,所以 RABBITMQ_HOST 设置为 rabbitmq ;如果之前使用了Laravel Horizon,那么 RABBITMQ_WORKER 的设置为 horizon 就可以了。
参考资料:laravel-queue-rabbitmq
Tags: Laravel RabbitMQ
相关文章
- ·Laravel 5.6中的CURD操作(代码示例详解)(2020-01-15)
- ·如何在laravel 5中创建用于XSS防御的中间件? (2020-01-16)
- ·关于Laravel重定向的七种方法详解(2020-01-26)
- ·如何在laravel 5中使用DB事务?(2020-01-31)
- ·Laravel中如何给图片加水印?(2020-04-05)
- ·Laravel框架数据库CURD操作、连贯操作总结(2021-04-10)
- ·Laravel框架路由配置总结、设置技巧大全(2021-04-10)
- ·Laravel框架中扩展函数、扩展自定义类的方法(2021-04-10)
- ·跟我学Laravel之快速入门(2021-04-16)
- ·跟我学Laravel之安装Laravel(2021-04-16)
- ·跟我学Laravel之配置Laravel(2021-04-16)
- ·跟我学Laravel之请求(Request)的生命周期(2021-04-16)
- ·跟我学Laravel之路由(2021-04-16)
- ·跟我学Laravel之请求与输入(2021-04-17)
- ·跟我学Laravel之视图 & Response(2021-04-17)
- ·laravel安装和配置教程(2021-04-19)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)