Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 10, 2022. It is now read-only.
/kafka-phpPublic archive

kafka php client

License

NotificationsYou must be signed in to change notification settings

weiboad/kafka-php

Repository files navigation

中文文档

QQ GroupBuild StatusPackagistPackagistPackagistGitHub issuesGitHub forksGitHub starsGitHub license

Kafka-php is a pure PHP kafka client that currently supports greater than 0.8.x version of Kafka, this project v0.2.x and v0.1.x are incompatible if using the original v0.1.x You can refer to the documentKafka PHP v0.1.x Document, but it is recommended to switch to v0.2.x . v0.2.x use PHP asynchronous implementation and kafka broker interaction, more stable than v0.1.x efficient, because the use of PHP language so do not compile any expansion can be used to reduce the access and maintenance costs

Requirements

  • Minimum PHP version: 7.1
  • Kafka version greater than 0.8
  • The consumer module needs kafka broker version greater than 0.9.0

Installation

Add the lib directory to the PHP include_path and use an autoloader like the one in the examples directory (the code follows the PEAR/Zend one-class-per-file convention).

Composer Install

Simply add a dependencynmred/kafka-php to your project if you use Composer to manage the dependencies of your project.

$ composer require nmred/kafka-php

Here is a minimal example of a composer.json file :

{"require": {"nmred/kafka-php": "0.2.*"}}

Configuration

Configuration properties are documented inConfiguration

Producer

Asynchronous mode

<?phprequire'../vendor/autoload.php';date_default_timezone_set('PRC');useMonolog\Logger;useMonolog\Handler\StdoutHandler;// Create the logger$logger =newLogger('my_logger');// Now add some handlers$logger->pushHandler(newStdoutHandler());$config = \Kafka\ProducerConfig::getInstance();$config->setMetadataRefreshIntervalMs(10000);$config->setMetadataBrokerList('10.13.4.159:9192');$config->setBrokerVersion('1.0.0');$config->setRequiredAck(1);$config->setIsAsyn(false);$config->setProduceInterval(500);$producer =new \Kafka\Producer(function() {return [            ['topic' =>'test','value' =>'test....message.','key' =>'testkey',            ],        ];    });$producer->setLogger($logger);$producer->success(function($result) {var_dump($result);});$producer->error(function($errorCode) {var_dump($errorCode);});$producer->send(true);

Synchronous mode

<?phprequire'../vendor/autoload.php';date_default_timezone_set('PRC');useMonolog\Logger;useMonolog\Handler\StdoutHandler;// Create the logger$logger =newLogger('my_logger');// Now add some handlers$logger->pushHandler(newStdoutHandler());$config = \Kafka\ProducerConfig::getInstance();$config->setMetadataRefreshIntervalMs(10000);$config->setMetadataBrokerList('127.0.0.1:9192');$config->setBrokerVersion('1.0.0');$config->setRequiredAck(1);$config->setIsAsyn(false);$config->setProduceInterval(500);$producer =new \Kafka\Producer();$producer->setLogger($logger);for($i =0;$i <100;$i++) {$producer->send([        ['topic' =>'test1','value' =>'test1....message.','key' =>'',        ],    ]);}

Consumer

<?phprequire'../vendor/autoload.php';date_default_timezone_set('PRC');useMonolog\Logger;useMonolog\Handler\StdoutHandler;// Create the logger$logger =newLogger('my_logger');// Now add some handlers$logger->pushHandler(newStdoutHandler());$config = \Kafka\ConsumerConfig::getInstance();$config->setMetadataRefreshIntervalMs(10000);$config->setMetadataBrokerList('10.13.4.159:9192');$config->setGroupId('test');$config->setBrokerVersion('1.0.0');$config->setTopics(['test']);//$config->setOffsetReset('earliest');$consumer =new \Kafka\Consumer();$consumer->setLogger($logger);$consumer->start(function($topic,$part,$message) {var_dump($message);});

Low-Level API

ReferExample

QQ Group

Group 1: 531522091Group 2: 657517955QQ Group

Packages

No packages published

Contributors26

Languages


[8]ページ先頭

©2009-2025 Movatter.jp