|
| 1 | +# PHP Kafka boilerplate wrapper around RdKafka |
| 2 | + |
| 3 | +[](https://travis-ci.org/simPod/Kafka) |
| 4 | +[](https://packagist.org/packages/simpod/kafka) |
| 5 | +[](https://packagist.org/packages/simpod/kafka) |
| 6 | +[](https://packagist.org/packages/simpod/kafka) |
| 7 | +[](https://scrutinizer-ci.com/g/simPod/Kafka) |
| 8 | +[](https://scrutinizer-ci.com/g/simPod/Kafka) |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Add as [Composer](https://getcomposer.org/) dependency: |
| 13 | + |
| 14 | +```sh |
| 15 | +composer require simpod/kafka |
| 16 | +``` |
| 17 | + |
| 18 | +## Config Constants |
| 19 | + |
| 20 | +Some config constants are provided like `ConsumerConfig`, `ProducerConfig` or `CommonClientConfigs`. |
| 21 | + |
| 22 | +However, they are copied from Java API and not all are applicable to librdkafka. Consult with librdkafka documentation before use. |
| 23 | + |
| 24 | +## Clients |
| 25 | + |
| 26 | +### Consumer |
| 27 | + |
| 28 | +`KafkaConsumer` boilerplate is available with `startBatch()` method ([(to suplement this example in librdkafka)](https://github.com/edenhill/librdkafka/blob/master/examples/rdkafka_consume_batch.cpp#L97)) and with `start()`. |
| 29 | +They also handle termination signals for you. |
| 30 | + |
| 31 | +```php |
| 32 | +<?php |
| 33 | + |
| 34 | +declare(strict_types=1); |
| 35 | + |
| 36 | +namespace Your\AppNamespace; |
| 37 | + |
| 38 | +use RdKafka\Message; |
| 39 | +use SimPod\Kafka\Clients\Consumer\ConsumerConfig; |
| 40 | +use SimPod\Kafka\Clients\Consumer\ConsumerRecords; |
| 41 | +use SimPod\Kafka\Clients\Consumer\KafkaConsumer; |
| 42 | + |
| 43 | +final class ExampleBatchConsumer |
| 44 | +{ |
| 45 | + public function run() : void |
| 46 | + { |
| 47 | + $kafkaConsumer = new KafkaConsumer($this->getConfig()); |
| 48 | + |
| 49 | + $kafkaConsumer->subscribe(['topic1']); |
| 50 | + |
| 51 | + $kafkaConsumer->startBatch( |
| 52 | + static function (Message $message) : void { |
| 53 | + // Process record |
| 54 | + }, |
| 55 | + static function (ConsumerRecords $consumerRecords) use ($kafkaConsumer) : void { |
| 56 | + // Process records batch |
| 57 | + |
| 58 | + $kafkaConsumer->commit($consumerRecords->getLast()); |
| 59 | + }, |
| 60 | + 200000, |
| 61 | + 120 * 1000 |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + private function getConfig() : ConsumerConfig |
| 66 | + { |
| 67 | + $config = new ConsumerConfig(); |
| 68 | + |
| 69 | + $config->set(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, '127.0.0.1:9092'); |
| 70 | + $config->set(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG, false); |
| 71 | + $config->set(ConsumerConfig::CLIENT_ID_CONFIG, gethostname()); |
| 72 | + $config->set(ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, 'earliest'); |
| 73 | + $config->set(ConsumerConfig::GROUP_ID_CONFIG, 'consumer_group_name'); |
| 74 | + |
| 75 | + return $config; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +```php |
| 81 | +<?php |
| 82 | + |
| 83 | +declare(strict_types=1); |
| 84 | + |
| 85 | +namespace Your\AppNamespace; |
| 86 | + |
| 87 | +use RdKafka\Message; |
| 88 | +use SimPod\Kafka\Clients\Consumer\ConsumerConfig; |
| 89 | +use SimPod\Kafka\Clients\Consumer\KafkaConsumer; |
| 90 | + |
| 91 | +final class ExampleConsumer |
| 92 | +{ |
| 93 | + public function run() : void |
| 94 | + { |
| 95 | + $kafkaConsumer = new KafkaConsumer($this->getConfig(), Logger::get()); |
| 96 | + |
| 97 | + $kafkaConsumer->subscribe(['topic1']); |
| 98 | + |
| 99 | + $kafkaConsumer->start( |
| 100 | + 120 * 1000, |
| 101 | + static function (Message $message) use ($kafkaConsumer) : void { |
| 102 | + // Process message here |
| 103 | + |
| 104 | + $kafkaConsumer->commit($message); // Autocommit is disabled |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + private function getConfig() : ConsumerConfig |
| 110 | + { |
| 111 | + $config = new ConsumerConfig(); |
| 112 | + |
| 113 | + $config->set(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, '127.0.0.1:9092'); |
| 114 | + $config->set(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG, false); |
| 115 | + $config->set(ConsumerConfig::CLIENT_ID_CONFIG, gethostname()); |
| 116 | + $config->set(ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, 'earliest'); |
| 117 | + $config->set(ConsumerConfig::GROUP_ID_CONFIG, 'consumer_group_name'); |
| 118 | + |
| 119 | + return $config; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Development |
| 125 | + |
| 126 | +There is `kwn/php-rdkafka-stubs` listed as a dev dependency so it properly integrates php-rdkafka extension with IDE. |
0 commit comments