donatello-za/laravel-rabbitmq

v0.1.1 2022-03-07 13:25 UTC

This package is auto-updated.

Last update: 2024-09-07 19:21:54 UTC


README

重要

这不是直接分支,而是对已从github消失的名为kenokokoro/laravel-rabbitmq的包的克隆。此存储库是一个非支持的占位符,直到实现一个有效的替代方案。

描述

Laravel/Lumen的RabbitMQ队列(队列-工作)和消息(发布-订阅)包装器。此包为Laravel\Lumen队列扩展使用单独的配置选项,并为Pub\Sub使用另一个实现。

安装

  1. 使用composer安装此包:

    composer require kenokokoro/laravel-rabbitmq

  2. 添加服务提供者

    • 对于Laravel,在config/app.php文件的providers部分中添加V9\RabbitMQ\ServiceProvider::class,。之后,只需使用以下命令发布配置:php artisan vendor:publish --provider=V9\RabbitMQ\ServiceProvider --tag=config

    • 对于Lumen,在bootstrap/app.php文件中添加$app->register(V9\RabbitMQ\ServiceProvider::class)。之后,您需要在您的配置文件夹中创建一个新文件:config/messaging.php,并将从vendor/kenokokoro/laravel-rabbitmq/config/messaging-sample.php中找到的示例内容放入其中。最后,只需使用以下命令将此配置文件包含在bootstrap/app.php文件中:$app->configure('messaging')

  3. 尽管两种实现有不同的配置,但连接配置是相同的

     QUEUE_DRIVER=rabbitmq
     RABBITMQ_HOST=127.0.0.1
     RABBITMQ_PORT=5672
     RABBITMQ_VHOST=/
     RABBITMQ_LOGIN=guest
     RABBITMQ_PASSWORD=guest
    

    可用环境值的列表可以在vendor/kenokokoro/laravel-rabbitmq/config/queue.php中找到

    注意:环境配置值仅在laravel队列扩展中使用。对于消息(发布-订阅),它使用不同类型的配置

使用

  1. 队列(Laravel官方文档

    • 在Laravel上:Queue::push(App\Jobs\DummyJob::class)
    • 在Lumen上,如果您在bootstrap/app.php文件中添加了$app->withFacades(),则可以使用相同的命令,或者简单使用app('queue')->push(App\Jobs\SomeJob::class)
    • 要消费这些中的任何一个,只需使用Laravel\Lumen队列工作器:php artisan queue:work
  2. 消息

    消息使用不同的配置进行队列管理(除了RabbitMQ连接)。要了解RabbitMQ交换和队列参数的重要性的示例,请检查RabbitMQ示例

    1. 使用依赖注入

      发布示例

      <?php
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use V9\RabbitMQ\Messaging\Pub\PublishInterface;
      use V9\RabbitMQ\Messaging\Pub\Data;
      class PublishCommand extends Command
      {
          protected $signature = 'publish';
          /**
           * @var PublishInterface
           */
          private $publish;
      
          public function __construct(PublishInterface $publish)
          {
              parent::__construct();
              $this->publish = $publish;
          }
      
          public function handle()
          {
              $this->publish->route(['test.1', 'test.2', 'test.3'], str_random());
              # Or if you want to send array you can use the dedicated class
              # $this->publish->route(['test.1', 'test.2', 'test.3'], new Data(['hello' => 'world']);
          }
      }
      

      消费发布者示例

      <?php
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use PhpAmqpLib\Message\AMQPMessage;
      use V9\RabbitMQ\Messaging\Sub\ConsumeInterface;
      class DummyCommand extends Command
      {
          protected $signature = 'consume';
      
          /**
           * @var ConsumeInterface
           */
          private $consume;
      
          public function __construct(ConsumeInterface $consume)
          {
              parent::__construct();
              $this->consume = $consume;
          }
      
          public function handle()
          {
              $this->consume->route(['test.*'], function ($msg) {
                  return $this->msg($msg);
              });
          }
      
          private function msg(AMQPMessage $msg)
          {
              $this->line($msg->body);
          }
      }
      
    2. 可以使用app(V9\RabbitMQ\Messaging\Sub\ConsumeInterface::class)app(V9\RabbitMQ\Messaging\Pub\PublishInterface:class)实现相同的效果

许可证

Laravel\Lumen RabbitMQ包是开源软件,许可证为MIT许可