当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-queue-rabbitmq

授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 韩宏朗
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

RabbitMQ Queue driver for Laravel

Build Status

Support Policy

Only the latest version will get new features. Bug fixes will be provided using the following scheme:

Package Version Laravel Version Bug Fixes Until
9 6 October 5th, 2021 Documentation
10 6, 7 October 5th, 2021 Documentation
11 8 April 6th, 2021 Documentation

Installation

You can install this package via composer using this command:

composer require vladimir-yuldashev/laravel-queue-rabbitmq

The package will automatically register itself.

Add connection to config/queue.php:

'connections' => [
    // ...

    'rabbitmq' => [
    
       'driver' => 'rabbitmq',
       'queue' => env('RABBITMQ_QUEUE', 'default'),
       'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
   
       'hosts' => [
           [
               'host' => env('RABBITMQ_HOST', '127.0.0.1'),
               'port' => env('RABBITMQ_PORT', 5672),
               'user' => env('RABBITMQ_USER', 'guest'),
               'password' => env('RABBITMQ_PASSWORD', 'guest'),
               'vhost' => env('RABBITMQ_VHOST', '/'),
           ],
       ],
   
       'options' => [
           'ssl_options' => [
               '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),
           ],
           'queue' => [
               'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
           ],
       ],
   
       /*
        * Set to "horizon" if you wish to use Laravel Horizon.
        */
       'worker' => env('RABBITMQ_WORKER', 'default'),
        
    ],

    // ...    
],

Optional Config

Optionally add queue options to the config of a connection.Every queue created for this connection, get's the properties.

When you want to prioritize messages when they were delayed, then this is possible by adding extra options.

  • When max-priority is omitted, the max priority is set with 2 when used.
'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'prioritize_delayed' =>  false,
                'queue_max_priority' => 10,
            ],
        ],
    ],

    // ...    
],

When you want to publish messages against an exchange with routing-key's, then this is possible by adding extra options.

  • When the exchange is omitted, RabbitMQ will use the amq.direct exchange for the routing-key
  • When routing-key is omitted the routing-key by default is the queue name.
  • When using %s in the routing-key the queue_name will be substituted.

Note: when using exchange with routing-key, u probably create your queues with bindings yourself.

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'exchange' => 'application-x',
                'exchange_type' => 'topic',
                'exchange_routing_key' => '',
            ],
        ],
    ],

    // ...    
],

In Laravel failed jobs are stored into the database. But maybe you want to instruct some other process to also do something with the message.When you want to instruct RabbitMQ to reroute failed messages to a exchange or a specific queue, then this is possible by adding extra options.

  • When the exchange is omitted, RabbitMQ will use the amq.direct exchange for the routing-key
  • When routing-key is omitted, the routing-key by default the queue name is substituted with '.failed'.
  • When using %s in the routing-key the queue_name will be substituted.

Note: When using failed_job exchange with routing-key, u probably need to create your exchange/queue with bindings yourself.

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'reroute_failed' => true,
                'failed_exchange' => 'failed-exchange',
                'failed_routing_key' => 'application-x.%s',
            ],
        ],
    ],

    // ...    
],

Use your own RabbitMQJob class

Sometimes you have to work with messages published by another application.
Those messages probably won't respect Laravel's job payload schema.The problem with these messages is that, Laravel workers won't be able to determine the actual job or class to execute.

You can extend the build-in RabbitMQJob::class and within the queue connection config, you can define your own class.When you specify an job key in the config, with your own class name, every message retrieved from the broker will get wrapped by your own class.

An example for the config:

'connections' => [
    // ...

    'rabbitmq' => [
        // ...

        'options' => [
            'queue' => [
                // ...

                'job' => \App\Queue\Jobs\RabbitMQJob::class,
            ],
        ],
    ],

    // ...    
],

An example of your own job class:

<?php

namespace App\Queue\Jobs;

use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;

class RabbitMQJob extends BaseJob
{

    /**
     * Fire the job.
     *
     * @return void
     */
    public function fire()
    {
        $payload = $this->payload();

        $class = WhatheverClassNameToExecute::class;
        $method = 'handle';

        ($this->instance = $this->resolve($class))->{$method}($this, $payload);

        $this->delete();
    }
}

Or maybe you want to add extra properties to the payload:

<?php

namespace App\Queue\Jobs;

use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;

class RabbitMQJob extends BaseJob
{
   /**
     * Get the decoded body of the job.
     *
     * @return array
     */
    public function payload()
    {
        return [
            'job'  => 'WhatheverFullyQualifiedClassNameToExecute@handle',
            'data' => json_decode($this->getRawBody(), true)
        ];
    }
}

Laravel Usage

Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues

Laravel Horizon Usage

Starting with 8.0, this package supports Laravel Horizon out of the box. Firstly, install Horizon and then set RABBITMQ_WORKER to horizon.

Lumen Usage

For Lumen usage the service provider should be registered manually as follow in bootstrap/app.php:

$app->register(VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class);

Consuming Messages

There are two ways of consuming messages.

  1. queue:work command which is Laravel's built-in command. This command utilizes basic_get.

  2. rabbitmq:consume command which is provided by this package. This command utilizes basic_consume and is more performant than basic_get by ~2x.

Testing

Setup RabbitMQ using docker-compose:

docker-compose up -d rabbitmq

To run the test suite you can use the following commands:

# To run both style and unit tests.
composer test

# To run only style tests.
composer test:style

# To run only unit tests.
composer test:unit

If you receive any errors from the style tests, you can automatically fix most,if not all of the issues with the following command:

composer fix:style

Contribution

You can contribute to this package by discovering bugs and opening issues. Please, add to which version of package you create pull request or issue. (e.g. [5.2] Fatal error on delayed job)

  • 1、安装rabbitmq请参考教程RabbitMq安装教程(超详细)_普通网友的博客-CSDN博客_rabbitmq安装 2、php安装amqp扩展         点击链接PECL :: Package :: amqp 选择对应PHP版本的扩展包,由于本文用的Laravel6最高支持laravel-queue-rabbitmq:10,建议选择下载PHP7.2         我的本地是phpst

  • 导语 RabbitMQ 想必大家都有了解,不做多介绍来。这里实现的是用 RabbitMQ 作为 Larvel 队列的驱动,替代 Redis。下面以 Laradock 中安装示例。 安装 切换到 laradock 目录,将 .env 中关于 INSTALL_AMQP 和 INSTALL_BCMATH 的值修改为 true docker-compose stop workspace php-fpm p

 相关资料
  • 运行队列处理器 队列处理器的设置 Laravel 包含一个队列处理器,当新任务被推到队列中时它能处理这些任务。你可以通过 queue:work 命令来运行处理器。要注意,一旦 queue:work 命令开始,它将一直运行,直到你手动停止或者你关闭控制台: php artisan queue:work 可以指定队列处理器所使用的连接。 php artisan queue:work redis 可以自

  • 前言 在实际的项目开发中,我们经常会遇到需要轻量级队列的情形,例如发短信、发邮件等,这些任务不足以使用 kafka、RabbitMQ 等重量级的消息队列,但是又的确需要异步、重试、并发控制等功能。通常来说,我们经常会使用 Redis、Beanstalk、Amazon SQS 来实现相关功能,laravel 为此对不同的后台队列服务提供统一的 API,本文将会介绍应用最为广泛的 redis 队列。

  • Implement Queue using Stacks 描述 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek()

  • 本文向大家介绍Laravel使用Queue队列的技巧汇总,包括了Laravel使用Queue队列的技巧汇总的使用技巧和注意事项,需要的朋友参考一下 前言 Laravel 队列为不同的后台队列服务提供统一的 API,例如 Beanstalk,Amazon SQS,Redis,甚至其他基于关系型数据库的队列。队列的目的是将耗时的任务延时处理,比如发送邮件,从而大幅度缩短 Web 请求和相应的时间。 队

  • 它代表了先进先出的对象集合。 当您需要先进先出的物品时,可以使用它。 在列表中添加项目时,它将被称为enqueue ,当您删除项目时,它将被称为deque 。 队列类的属性和方法 下表列出了Queue类的一些常用properties - Sr.No 财产和描述 1 Count 获取Queue中包含的元素数。 下表列出了Queue类的一些常用methods - Sr.No. 方法名称和目的 1 Pu

  • 在等待服务的过程中,我们熟悉日常生活中的排队。 队列数据结构也意味着数据元素排列在队列中的相同。 队列的唯一性在于添加和删除项目的方式。 这些项目允许在最后但从另一端删除。 所以这是一种先入先出的方法。 可以使用python list实现队列,我们​​可以使用insert()和pop()方法添加和删除元素。 它们没有插入,因为数据元素总是在队列末尾添加。 将元素添加到队列 在下面的示例中,我们创建