PHP code example of nirmalsharma / laravel-kafka-php
1. Go to this page and download the library: Download nirmalsharma/laravel-kafka-php library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nirmalsharma / laravel-kafka-php example snippets
bash
composer
bash
namespace App\Console\Commands;
use App\Handlers\TestHandler;
use Illuminate\Console\Command;
use KafkaConsumer;
class TestTopicConsumer extends Command
{
protected $signature = 'kafka:test-consume {--partition=} {--consumer-group=} {--topic=} {--dlq-topic=}';
protected $description = 'Kafka consumer!!';
public function handle(): void
{
KafkaConsumer::createConsumer(new TestHandler);
}
public function setKafkaConfig(){
$partition = $this->option('partition');
if( $partition != null){
config([
"kafka.partition" => $partition
]);
}
$consumer_group_id = $this->option('consumer-group');
if( !empty($consumer_group_id)){
config([
"kafka.consumer_group_id" => $consumer_group_id
]);
}
$topic = $this->option('topic');
if( !empty($topic)){
config([
"kafka.topic" => $topic
]);
}
$dlq_topic = $this->option('dlq-topic');
if( !empty($dlq_topic)){
config([
"kafka.dlq_topic" => $dlq_topic
]);
}
}
}
TestHandler.php
-----------------
namespace App\Handlers;
use Illuminate\Support\Facades\Log;
class TestHandler
{
public function __invoke( $message)
{
dump([
"partition" => $message['raw']->partition,
"key" => $message['key']
]);
}
}
php artisan kafka:test-consume --consumer-group=test-local --topic=demo-topic --dlq-topic=demo-dlq