maddyscout / laravel-phprdkafka
Laravel与php-rdkafka之间的桥梁包
dev-master
2023-02-28 04:41 UTC
Requires
- php: ^7.2 || ^7.3 || ^8.0
- ext-rdkafka: ^4.0 || ^5.0
- illuminate/contracts: ^7 || ^8
Requires (Dev)
- brianium/paratest: ^6.2
- nunomaduro/collision: ^5.3
- orchestra/testbench: ^6.15
- phpunit/phpunit: ^9.3
- spatie/laravel-ray: ^1.9
- vimeo/psalm: ^4.4
This package is not auto-updated.
Last update: 2024-09-25 10:12:32 UTC
README
已弃用,请使用https://github.com/ensi-platform/laravel-php-rdkafka代替
此包允许您在config/kafka.php中描述Kafka生产者和消费者,然后可以在任何地方重复使用它们。
安装
您可以通过composer安装此包
composer require maddyscout/laravel-php-rdkafka
使用以下命令发布配置文件:
php artisan vendor:publish --provider="Greensight\LaravelPhpRdKafka\LaravelPhpRdKafkaServiceProvider" --tag="kafka-config"
现在转到config/kafka.php
并配置您的主机和消费者。通常,每个Kafka集群需要一个主机/消费者。配置参数可以在Librdkafka配置参考中找到。
使用示例
生产者示例
$producer = \Kafka::producer('producer-name'); // returns a configured RdKafka\Producer singleton. // or $producer = \Kafka::producer(); if you want to get the default producer. // or $producer = $kafkaManager->producer(); where $kafkaManager is an instance of Greensight\LaravelPhpRdKafka\KafkaManager resolved from the service container. // now you can implement any producer logic e.g: $headers = []; $topicName = 'test-topic'; $topic = $producer->newTopic($topicName); for ($i = 0; $i < 10; $i++) { $payload = json_encode([ 'body' => "Message $i in topic [$topicName]", 'headers' => $headers ]); $topic->produce(RD_KAFKA_PARTITION_UA, 0, $payload); $producer->poll(0); } for ($flushRetries = 0; $flushRetries < 10; $flushRetries++) { $result = $producer->flush(10000); if (RD_KAFKA_RESP_ERR_NO_ERROR === $result) { break; } } if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) { // Log and/or throw "Unable to flush Kafka producer, messages of topic [$topicName] might be lost.' exception. } // If you use php-fpm and producing is slow you can move its execution to the place after response has been sent. // This can be achieved e.g. by wrapping the whole producing or at least flushing in it in a "terminating" callback. // app()->terminating(function () { ... });
消费者示例
public function handle(KafkaManager $kafkaManager) { $consumer = $kafkaManager->consumer('consumer-name'); $consumer->subscribe(['test-topic-1', 'test-topic-2']); while (true) { $message = $consumer->consume(120*1000); switch ($message->err) { case RD_KAFKA_RESP_ERR_NO_ERROR: $this->info($message->payload); $this->processMessage($message); // do something with the message // $consumer->commitAsync($message); // commit offsets asynchronously if you set 'enable.auto.commit' => false, in config/kafka.php break; case RD_KAFKA_RESP_ERR__PARTITION_EOF: echo "No more messages; will wait for more\n"; break; case RD_KAFKA_RESP_ERR__TIMED_OUT: // this also happens when there is no new messages in the topic after the specified timeout: https://github.com/arnaud-lb/php-rdkafka/issues/343 echo "Timed out\n"; break; default: throw new Exception($message->errstr(), $message->err); break; } } }
您可以在php-rdkafka示例中了解更多关于php-rdkafka生产者和消费者的信息。
以下getters可用于直接访问RdKafka\Conf
实例:
$producerConf = $kafkaManager->producerConfig('producer-name'); $consumerConf = $kafkaManager->consumerConfig('consumer-name');
测试
composer test
变更日志
请参阅CHANGELOG以了解最近更改了哪些内容。
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。