Home > PHP Framework > Laravel > body text

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

藏色散人
Release: 2021-09-24 15:15:34
forward
2054 people have browsed it

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'
    ]);
Copy after login

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();
Copy after login

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)
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template