Home  >  Article  >  PHP Framework  >  What is the use of Apache Kafka extension package in Laravel?

What is the use of Apache Kafka extension package in Laravel?

藏色散人
藏色散人forward
2021-09-24 15:15:342062browse

The following is the Apache Kafka expansion package recommended by the Laravel tutorial column. I hope it will be helpful to friends in need!

What is the use of Apache Kafka extension package in Laravel?

Laravel Kafka extension package (https://github.com/mateusjunges/laravel-kafka) allows you to use Apache Kafka producers and consumers in Laravel applications Simple. Using the publishOn 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)

  • Maximum message consumption configuration
  • Dead letter queue - Wikipedia configuration
  • Middleware configuration
  • Using in testingKafka::fake () Method to simulate Kafka producer
  • Enable debugging in development mode
  • The message body is configurable

This 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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete