Laravel-使用-RabbitMQ

导语RabbitMQ 想必大家都有了解,不做多介绍来。这里实现的是用 RabbitMQ 作为 Larvel 队列的驱动,替代 Redis。下面以 Laradock 中安装示例。 安装切换到 laradock 目录,将 .env 中关于 INSTALL_AMQP 的值修改为 truedocker-compose stop workspace php-fpm php-workerdocker-compose build workspace php-fpm php-worker rabbitmqdocker-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=horizonRABBITMQ_HOST=rabbitmqRABBITMQ_PORT=5672RABBITMQ_LOGIN=guestRABBITMQ_PASSWORD=guestRABBITMQ_QUEUE=default有两个值说明一下,因为是在 Laradock 中,所以 RABBITMQ_HOST 设置为 rabbitmq;如果之前使用了Laravel Horizon,那么 RABBITMQ_WORKER 的设置为 horizon 就可以了。 ...

June 17, 2019 · 2 min · jiezi

laradock-中安装-Laravel-Dusk

导语在本地安装 Laravel Dusk 一直失败,查了文档才发现在 laradock 中并不是只需要 composer require 就可以的,还有其他配置。下面记录一下。 配置 laradock切换到 laradock 目录中,将 workspace 容器先暂停 docker-compose sotp workspace修改 .env 文件中的 WORKSPACE_INSTALL_LARAVEL_INSTALLER 和 WORKSPACE_INSTALL_DUSK_DEPS,将配置值改为 true重新搭建 workspace 容器 docker-compose build workspace成功之后启动 docker-compose up -d workspace安装 Laravel Dusk进入到 workspace 容器中 docker-compose exec workspace bash,并且切换到项目目录中使用 composer require --dev laravel/dusk 安装 Laravel Dusk执行 php artisan dusk:install在 tests/DuskTestCase.php 文件中,修改 driver 方法,添加 —-no-sandbox 参数,如下protected function driver(){ $options = (new ChromeOptions)->addArguments([ '—disable-gpu', '—headless', '—window-size=1920,1080', '—no-sandbox',// 添加这行 ]); return RemoteWebDriver::create( 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability( ChromeOptions::CAPABILITY, $options ) );}添加一个配置文件,cp .env .env.dusk.local,将 APP_URL 修改为 http://localhost:8000执行 php artisan serve —-quiet &最后可以使用 Laravel Dusk 进行测试了 php artisan dusk结语其实在 workspace 中有很多 alias 可是使用,为了便于理解,都是用了原命令。关于 Laravel Dusk 的使用,可以参考这个教程,其中不止关于测试的部分,其他部分很好。 ...

June 12, 2019 · 1 min · jiezi

替换-Docker-或-Laradock-中-Debian-系统镜像源解决软件安装问题

Docker Debian 镜像源替换因多数默认的 Docker 镜像为国外的,而采用的镜像源也是国外的,故访问很慢,所以我们需要替换为国内的(比如阿里云或163等)。 163 - DebianAliyun - Debian注意: 不同版本的 Debian 镜像源地址不一样Debian 7.x (wheezy)# 更新apt-get源RUN echo \ deb http://mirrors.aliyun.com/debian/ wheezy main non-free contrib\ deb http://mirrors.aliyun.com/debian/ wheezy-proposed-updates main non-free contrib\ deb-src http://mirrors.aliyun.com/debian/ wheezy main non-free contrib\ > /etc/apt/sources.listDebian 8.x (jessie)# 更新apt-get源RUN echo \ deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib\ deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib\ deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib\ > /etc/apt/sources.listDebian 9.x (stretch)# 更新apt-get源RUN echo \ deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib\ deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib\ deb http://mirrors.aliyun.com/debian-security stretch/updates main\ deb-src http://mirrors.aliyun.com/debian-security stretch/updates main\ deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib\ deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib\ deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib\ deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib\ > /etc/apt/sources.listLaradock 镜像构建失败因为默认用的是国外 Debian 镜像源,故在执行 apt-get 等命令拉取软件包时会失败,我们需手动在对于的 Dockerfile 中添加一个 RUN 指令来替换掉默认的镜像源以下仅为示例: ...

May 9, 2019 · 1 min · jiezi