Laravel 使用 RabbitMQ

1

导语

RabbitMQ 想必大家都有了解,不做多介绍来。这里实现的是用 RabbitMQ 作为 Larvel 队列的驱动,替代 Redis。下面以 Laradock 中安装示例。

安装

  1. 切换到 laradock 目录,将 .env 中关于 INSTALL_AMQPINSTALL_BCMATH 的值修改为 true
  2. docker-compose stop workspace php-fpm php-worker
  3. docker-compose build workspace php-fpm php-worker rabbitmq
  4. docker-compose up -d workspace php-fpm php-worker rabbitmq

扩展包安装以及配置

  1. 进入到 workspace 容器中,在项目目录安装扩展包 composer require vladimir-yuldashev/laravel-queue-rabbitmq
  2. 接下来在 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),
    ],

],
  1. .env 中修改 QUEUE_CONNECTIONrabbitmq,并添加以下值
RABBITMQ_WORKER=horizon
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_QUEUE=default

有两个值说明一下,因为是在 Laradock 中,所以 RABBITMQ_HOST 设置为 rabbitmq;扩展包支持 Laravel Horizon,如果之前使用了 Laravel Horizon,那么 RABBITMQ_WORKER 的设置为 horizon ,并修改 config/horizon.phpenvironments 相关 connection

  1. 开启相关的端口,重启容器

补充

好事多磨,部署的时候出了点问题,好在最终解决了。
首先是使用 php-worker 配置 supervisor 守护 php artisan horizon,日志一直报错 Call to undefined function PhpAmqpLib\Wire\bcadd()。从错误信息看是没有安装 bcmath 模块,查了一下 php-fpmphp-worker 容器都是有安装 bcmath 模块的。
这个时候,看到是有 laravel-horizon 单独的容器,看了一下 Dockerfile 也是用了 supervisor 守护进程。那就用 laravel-horizon 容器来守护进程。配置方面没什么好说的,也给了示例文件,修改就好了。
这个时候,没有在报错 bcmath,但是有了另一个错误 Use of undefined constant SOCKET_EWOULDBLOCK - assumed 'SOCKET_EWOULDBLOCK',可以看到是关于 sockets 模块的。
查了一些资料,解决方法是在 laravel-horizon 容器中 docker-php-ext-install sockets,然后再重启就好了。


参考资料:laravel-queue-rabbitmq

你可能感兴趣的

键盘侠 · 7月5日

.env 里面有很多INSTALL_AMQP 全都设置为true么 ?

回复

0

如果用到 php-worker,就把 PHP_WORKER_INSTALL_AMQP 设置为 true;另外 WORKSPACE_INSTALL_AMQPPHP_FPM_INSTALL_AMQP 都需要设置为 true

haoyq 作者 · 7月5日
0

好的,谢谢回复

键盘侠 · 7月5日
载入中...