下面由laravel教程栏目给大家推荐apache kafka扩展包,希望对需要的朋友有所帮助!
laravel kafka 扩展包(https://github.com/mateusjunges/laravel-kafka)让你在 laravel 应用中使用 apache kafka 生产者和消费者变得简单。使用 publishon 方法,可以让你流畅的配置和发布消息:
use junges\kafka\facades\kafka;kafka::publishon('broker', 'topic') ->withconfigoption('property-name', 'property-value') ->withconfigoptions([ 'property-name' => 'property-value' ]);
下面展示如何通过这个包在 laravel 应用中向 kafka 发送消息:
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();
这里是消费者订阅消息的示例:
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)
这个包的其他功能请见 readme:(https://github.com/mateusjunges/laravel-kafka/blob/master/readme.md)
最大消息消费数量配置死信队列 - 维基百科 配置中间件配置在测试中使用 kafka::fake() 方法模拟 kafka 生产者开发模式下开启调试消息体可配置这个包需要 rdkafka 扩展提供 kafka 生成级别高效的 php 客户端。 github 上有包详细的安装说明,和 源码 。
原文地址:https://laravel-news.com/laravel-kafka-package
译文地址:https://learnku.com/laravel/t/61072
以上就是apache kafka扩展包在laravel中有什么用?的详细内容。