PHP code example of smartcoder01 / laravel-queue-kafka
1. Go to this page and download the library: Download smartcoder01/laravel-queue-kafka 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/ */
smartcoder01 / laravel-queue-kafka example snippets
return [
'connections' => [
'kafka' => [
'broker_list' => env('KAFKA_BROKER_LIST', 'localhost:9092'),
'group_id' => env('KAFKA_GROUP_ID', 'laravel-queue-group'),
'auto_offset_reset' => env('KAFKA_AUTO_OFFSET_RESET', 'earliest'),
'queue' => env('KAFKA_QUEUE', 'default'),
'default_configuration' => [
'enable.auto.commit' => 'true',
// Add other Kafka configuration options here
],
],
],
];
dispatch(new \App\Jobs\ProcessOrder($order));
use Illuminate\Support\Facades\Queue;
Queue::push(new \App\Jobs\ProcessOrder($order));
$this->app->bind('queue.kafka.producer', function ($app, $parameters) {
$conf = new \RdKafka\Conf();
$conf->set('metadata.broker.list', env('KAFKA_BROKER_LIST', 'localhost:9092'));
return new \RdKafka\Producer($conf);
});
$this->app->bind('queue.kafka.consumer', function ($app, $parameters) {
$conf = new \RdKafka\Conf();
$conf->set('metadata.broker.list', env('KAFKA_BROKER_LIST', 'localhost:9092'));
$conf->set('group.id', env('KAFKA_GROUP_ID', 'laravel-queue-group'));
$conf->set('auto.offset.reset', env('KAFKA_AUTO_OFFSET_RESET', 'earliest'));
return new \RdKafka\KafkaConsumer($conf);
});
protected function waitForAck()
{
$timeout = 30 * 1000; // Increase timeout to 30 seconds
// ...
}
bash
php artisan vendor:publish --provider="Smartcoder01\Queue\Kafka\KafkaQueueServiceProvider"
bash
php artisan queue:work