almatar / rabbitmq

集成了 php-amqplib 与 Laravel & Lumen。

v1.1.0 2020-11-08 00:46 UTC

This package is auto-updated.

Last update: 2024-09-08 09:38:37 UTC


README

rabbitmq 提供适配器的 PHP 框架 Laravel/Lumen

需求

  • PHP >= 7.2
  • php-amqplib/php-amqplib >= 2.7
  • Laravel >= 6.0

安装

运行以下命令安装包

composer require almatar/rabbitmq

配置

创建配置文件

创建 config/rabbitmq.php 文件,在这里您可以定义 rabbitmq 连接、生产者和消费者。

config/rabbitmq.php 的示例

<?php

return [
    'connections' => [
        'default' => [
            'host' => 'localhost',
            'port' => 5672,
            'user' => 'guest',
            'password' => 'password',
            'vhost' => '/',
            'connection_attempts' => 10,
            'reconnect_waiting_seconds' => 3,
            'read_write_timeout' => 30, // heartbeat * 2 at least
            'heartbeat' => 15,
        ],
    ],
    'producers' => [
        'test_producer' => [
            'exchange_options' => [
                'name' => 'test_exchange',
                'type' => 'fanout'
            ],
            'queue_options' => [
                'name' => 'test_queue',
                'routing_key' => '',
            ]
        ]
    ],
    'consumers' => [
        'test_consumer' => [
            'qos_prefetch_count' => 5,
            'exchange_options' => [
                'name' => 'test_exchange',
                'type' => 'fanout'
            ],
            'queue_options' => [
                'name' => 'test_queue',
                'routing_key' => '',
            ]
        ]
    ],
];

Lumen 中,请手动加载配置文件 bootstrap/app.php

$app->configure('rabbitmq');

为 Lumen 注册 Service Provider

将以下行添加到 bootstrap/app.php

$app->register(Almatar\RabbitMQ\RabbitMQServiceProvider::class);

用法

消费者示例

<?php

namespace App\Console\Commands;

use Throwable;
use Illuminate\Console\Command;
use PhpAmqpLib\Message\AMQPMessage;
use Almatar\RabbitMQ\Adapters\Consumer;

class TestConsumer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:consumer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test rabbitmq consumer';

    /**
     *
     * @var Consumer
     */
    private $consumer;

    /**
     * TestCommand constructor.
     * @param Consumer $consumer
     * @param TestService $service
     */
    public function __construct(Consumer $consumer)
    {
        $this->consumer = $consumer;
        parent::__construct();
    }

    /**
     * @throws \Exception
     */
    public function handle()
    {
        $this->info('[x] Test rabbitmq command consumer is up');
        
        $this->consumer->subscribe(
            config('rabbitmq.consumers.test_consumer'),
            [$this, 'consume']
        );
    }

    public function consume(AMQPMessage $message)
    {
        try {
            $this->info('Message Consumed'); $this->info($message->getBody());
            $message->ack();
        } catch (Throwable $t) {
            die($t->getMessage());
        }
    }
}

生产者示例

<?php

namespace App\Services;

use Almatar\RabbitMQ\Adapters\Producer;

class TestService
{
    /**
     * @var Producer
     */
    private $producer;

    /**
     * TestService constructor.
     * @param Producer $producer
     */
    public function __construct(Producer $producer)
    {
        $this->producer = $producer;
    }

    /**
     * @param AMQPMessage $message
     * @throws Exception
     */
    public function execute()
    {
        $testMessageBody = [
            'name' => 'John Doe',
            'Age' => 7000
        ];

        $messageBody = json_encode($testMessageBody);

        $this->producer->publish(
            config('rabbitmq.producers.test_producer'), 
            $messageBody
        );
    }
}

贡献

在提交 pull request 之前,请注意以下指南

许可证

LICENSE

路线图

  • 支持直接发布到队列
  • 支持 HTTPS 连接
  • 支持事务
  • 支持批量操作
  • 添加单元测试
  • 添加默认日志记录器,并可自定义