superbalist / laravel4-pubsub
Laravel 4 的 Pub-Sub 抽象
Requires
- php: >=5.6.0
- illuminate/config: ^4.0
- illuminate/container: ^4.0
- illuminate/support: ^4.0
- superbalist/php-pubsub: ^1.0|^2.0
- superbalist/php-pubsub-google-cloud: ^5.2.0
- superbalist/php-pubsub-http: ^1.0
- superbalist/php-pubsub-redis: ^1.0|^2.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.5
This package is auto-updated.
Last update: 2024-07-25 12:01:32 UTC
README
Laravel 4 的 Pub-Sub 抽象。
此包是一个包装器,用于将 php-pubsub 集成到 Laravel 4。
要支持 Laravel 5,请使用包 https://github.com/Superbalist/laravel-pubsub
以下适配器被支持
- 本地
- /dev/null
- Redis
- Kafka(请参阅以下单独的安装说明)
- Google Cloud
- HTTP
安装
composer require superbalist/laravel4-pubsub
该包内置了默认配置。
要自定义配置文件,请使用 Artisan 发布包配置。
php artisan config:publish superbalist/laravel4-pubsub
然后您可以在 app/config/packages/superbalist/laravel4-pubsub/config.php
中编辑生成的配置。
在 app.php 中注册服务提供者
'providers' => [ // ... 'Superbalist\Laravel4PubSub\PubSubServiceProvider' ]
在 app.php 中注册外观
'aliases' => [ // ... 'PubSub' => 'Superbalist\Laravel4PubSub\PubSubFacade', ]
Kafka 适配器安装
请注意,虽然该包捆绑了对 php-pubsub-kafka 适配器的支持,但默认情况下不包括该适配器。
这是因为 KafkaPubSubAdapter 依赖于外部依赖项 librdkafka c library
和 php-rdkafka
PECL 扩展。
如果您计划使用此适配器,您需要按照以下 安装说明 安装这些依赖项。
然后您可以使用以下方式包含适配器
composer require superbalist/php-pubsub-kafka
使用
// 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); });
创建订阅者
许多 pub-sub 适配器将包含阻塞的 subscribe()
调用,因此最好将这些命令作为作为 supervisor 进程运行的守护进程运行。
以下是一个作为 artisan 命令编写的示例订阅者。
<?php namespace App\Commands; use App; use Illuminate\Console\Command; class MyExampleSubscriber extends Command { /** * The name and signature of the subscriber command. * * @var string */ protected $name = 'subscriber:name'; /** * The subscriber description. * * @var string */ protected $description = 'PubSub subscriber for ________'; /** * Execute the console command. */ public function fire() { $pubsub = App::make('pubsub'); $pubsub->subscribe('channel_name', function ($message) { }); } }
Kafka 订阅者
对于使用 php-pubsub-kafka
适配器的订阅者,您可能希望为每个订阅者更改 consumer_group_id
。
为此,您需要使用 PubSubConnectionFactory
为每个订阅者创建一个新的连接。这是因为一旦创建连接,就无法更改 consumer_group_id
。
以下是如何做到这一点的示例
<?php namespace App\Commands; use App; use Config; use Illuminate\Console\Command; class MyExampleKafkaSubscriber extends Command { /** * The name and signature of the subscriber command. * * @var string */ protected $name = 'subscriber:name'; /** * The subscriber description. * * @var string */ protected $description = 'PubSub subscriber for ________'; /** * Execute the console command. */ public function fire() { $config = Config::get('laravel4-pubsub::connections.kafka'); $config['consumer_group_id'] = self::class; $factory = App::make('pubsub.factory'); $pubsub = $factory->make('kafka', $config); $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');