Home>Article>PHP Framework> What is the use of Apache Kafka extension package in Laravel?
The following is the Apache Kafka expansion package recommended by theLaraveltutorial column. I hope it will be helpful to friends in need!
Laravel Kafka extension package (https://github.com/mateusjunges/laravel-kafka) allows you to use Apache Kafka producers and consumers in Laravel applications Simple. Using thepublishOn
method allows you to configure and publish messages smoothly:
use Junges\Kafka\Facades\Kafka; Kafka::publishOn('broker', 'topic') ->withConfigOption('property-name', 'property-value') ->withConfigOptions([ 'property-name' => 'property-value' ]);
The following shows how to send messages to Kafka in the Laravel application through this package:
use Junges\Kafka\Facades\Kafka; /** @var \Junges\Kafka\Producers\ProducerBuilder $producer */ $producer = Kafka::publishOn('broker', 'topic') ->withConfigOptions(['key' => 'value']) ->withKafkaKey('your-kafka-key') ->withKafkaKey('kafka-key') ->withHeaders(['header-key' => 'header-value']); $producer->send();
Here This is an example of a consumer subscribing to a message:
use Junges\Kafka\Facades\Kafka; $consumer = Kafka::createConsumer('broker')->subscribe('topic'); // 通过回调函数处理: $consumer->withHandler(function(\RdKafka\Message $message) { // 消息处理 }); // Invokable handler: class Handler { public function __invoke(\RdKafka\Message $message){ //消息处理 } } $consumer->withHandler(Handler::class)
For other functions of this package, please see readme: (https://github.com/mateusjunges/laravel-kafka/blob/master/README.md)
Kafka::fake ()
Method to simulate Kafka producerThis package requires the rdkafka extension to provide Kafka generation level efficiency PHP client. There are detailed installation instructions and source code for the package on Github.
Original address: https://laravel-news.com/laravel-kafka-package
Translation address: https://learnku.com/laravel/t/61072
The above is the detailed content of What is the use of Apache Kafka extension package in Laravel?. For more information, please follow other related articles on the PHP Chinese website!