averinuveren2/lumen-pubsub

Lumen 的 Pub-Sub 抽象

3.2.1 2019-11-24 13:48 UTC

This package is auto-updated.

Last update: 2024-09-25 00:16:08 UTC


README

Lumen 的 Pub-Sub 抽象。

支持以下适配器

  • 本地
  • /dev/null
  • Redis
  • Kafka(以下有单独的安装说明)
  • Google Cloud
  • HTTP

安装

composer require averinuveren2/lumen-pubsub

在 app.php 中注册服务提供者

$app->register(Averinuveren\LumenPubSub\PubSubServiceProvider::class);

在 app.php 中注册外观

$app->withFacades(true, [
    Averinuveren\LumenPubSub\PubSubFacade::class => 'PubSub'
]);

该包有一个默认配置,使用以下环境变量。

PUBSUB_CONNECTION=redis

REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379

KAFKA_BROKERS=localhost

GOOGLE_CLOUD_PROJECT_ID=your-project-id-here
GOOGLE_CLOUD_KEY_FILE=path/to/your/gcloud-key.json

HTTP_PUBSUB_URI=null
HTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis

然后您可以在 app/config/pubsub.php 中编辑生成的配置。

Kafka 适配器要求

  1. 安装 librdkafka C 库

    $ cd /tmp
    $ mkdir librdkafka
    $ cd librdkafka
    $ git clone https://github.com/edenhill/librdkafka.git .
    $ ./configure
    $ make
    $ make install
  2. 安装 php-rdkafka PECL 扩展

    $ pecl install rdkafka
  3. 将以下内容添加到您的 php.ini 文件中以启用 php-rdkafka 扩展 extension=rdkafka.so

用法

// get the pub-sub manager
$pubsub = app('pubsub');

// note: function calls on the manager are proxied through to the default connection
// eg: you can do this on the manager OR a connection
$pubsub->publish('channel_name', 'message');

// get the default connection
$pubsub = app('pubsub.connection');
// or
$pubsub = app(\Superbalist\PubSub\PubSubAdapterInterface::class);

// get a specific connection
$pubsub = app('pubsub')->connection('redis');

// publish a message
// the message can be a string, array, bool, object - anything which can be json encoded
$pubsub->publish('channel_name', 'this is where your message goes');
$pubsub->publish('channel_name', ['key' => 'value']);
$pubsub->publish('channel_name', true);

// publish multiple messages
$messages = [
    'message 1',
    'message 2',
];
$pubsub->publishBatch('channel_name', $messages);

// subscribe to a channel
$pubsub->subscribe('channel_name', function ($message) {
    var_dump($message);
});

// all the above commands can also be done using the facade
PubSub::connection('kafka')->publish('channel_name', 'Hello World!');

PubSub::connection('kafka')->subscribe('channel_name', function ($message) {
    var_dump($message);
});

创建订阅者

该包包括一个辅助命令 php artisan make:subscriber MyExampleSubscriber 用于生成新的订阅者命令类。

许多 pub-sub 适配器将包含阻塞的 subscribe() 调用,因此这些命令最好以作为 supervisor 进程运行的守护进程来运行。

此生成命令将创建文件 app/Console/Commands/MyExampleSubscriber.php,其中将包含

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Superbalist\PubSub\PubSubAdapterInterface;

class MyExampleSubscriber extends Command
{
    /**
     * The name and signature of the subscriber command.
     *
     * @var string
     */
    protected $signature = 'subscriber:name';

    /**
     * The subscriber description.
     *
     * @var string
     */
    protected $description = 'PubSub subscriber for ________';

    /**
     * @var PubSubAdapterInterface
     */
    protected $pubsub;

    /**
     * Create a new command instance.
     *
     * @param PubSubAdapterInterface $pubsub
     */
    public function __construct(PubSubAdapterInterface $pubsub)
    {
        parent::__construct();

        $this->pubsub = $pubsub;
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->pubsub->subscribe('channel_name', function ($message) {

        });
    }
}

Kafka 订阅者

对于使用 php-pubsub-kafka 适配器的订阅者,您可能希望为每个订阅者更改 consumer_group_id

为此,您需要使用 PubSubConnectionFactory 为每个订阅者创建一个新的连接。这是因为一旦创建了连接,就无法更改 consumer_group_id

以下是一个示例,说明您如何做到这一点

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Averinuveren\LumenPubSub\PubSubConnectionFactory;
use Superbalist\PubSub\PubSubAdapterInterface;

class MyExampleKafkaSubscriber extends Command
{
    /**
     * The name and signature of the subscriber command.
     *
     * @var string
     */
    protected $signature = 'subscriber:name';

    /**
     * The subscriber description.
     *
     * @var string
     */
    protected $description = 'PubSub subscriber for ________';

    /**
     * @var PubSubAdapterInterface
     */
    protected $pubsub;

    /**
     * Create a new command instance.
     *
     * @param PubSubConnectionFactory $factory
     */
    public function __construct(PubSubConnectionFactory $factory)
    {
        parent::__construct();

        $config = config('pubsub.connections.kafka');
        $config['consumer_group_id'] = self::class;
        $this->pubsub = $factory->make('kafka', $config);
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->pubsub->subscribe('channel_name', function ($message) {

        });
    }
}

添加自定义驱动

请参阅 php-pubsub 文档中的 编写适配器 部分。

要包含您的自定义驱动,您可以调用 extend() 函数。

$manager = app('pubsub');
$manager->extend('custom_connection_name', function ($config) {
    // your callable must return an instance of the PubSubAdapterInterface
    return new MyCustomPubSubDriver($config);
});

// get an instance of your custom connection
$pubsub = $manager->connection('custom_connection_name');