deankh/laravel-rabbitmq

队列,消息交换等

0.1.0 2018-05-11 09:43 UTC

This package is not auto-updated.

Last update: 2024-09-22 03:41:44 UTC


README

描述

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

安装

  1. 使用 composer 安装此包:

    composer require deanhyde/laravel-rabbitmq

  2. 添加服务提供者

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

    • 对于 Lumen,在您的 bootstrap/app.php 文件中使用 $app->register(DeanKH\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 队列扩展中使用。对于消息(pub - sub),它使用不同类型的配置

用法

  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 DeanKH\RabbitMQ\Messaging\Pub\PublishInterface;
      use DeanKH\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 DeanKH\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(DeanKH\RabbitMQ\Messaging\Sub\ConsumeInterface::class)app(DeanKH\RabbitMQ\Messaging\Pub\PublishInterface:class) 来实现

许可证

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