English Document
Kafka- PHP – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client – Kafka client Kafka PHP v0.1.x Document is recommended if you are using v0.1.x. V0.2. x uses PHP asynchronous execution to interact with Kafka Broker, which is more stable and efficient than V0.1. x. Since it is written in PHP, it can be used without compiling any extensions, reducing access and maintenance costs
Installation Environment Requirements
- PHP version greater than 5.5
- Kafka Server version is greater than 0.8.0
- The consumption module Kafka Server version must be greater than 0.9.0
Install using Composer
Add composer dependency nmred/kafka-php to the composer. Json file of your project, for example:
{"require": {"nmred/kafka-php": "0.2.*"}}
Copy the code
require '../vendor/autoload.php';
date_default_timezone_set('PRC');
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StdoutHandler());
//Set production-related configurations. For details, see [Configuration](configuration.md).
$config = \Kafka\ProducerConfig: :getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('10.13.4.159:9192');
$config->setBrokerVersion('0.9.0.1');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer(function() {
return array(
array(
'topic' = > 'test'.
'value' = > 'test.... message.'.
'key' = > 'testkey'.
),
);
});
$producer->setLogger($logger);
$producer->success(function($result) {
var_dump($result);
});
$producer->error(function($errorCode.$context) {
var_dump($errorCode);
});
$producer->send();Copy the code
Consumer
require '../vendor/autoload.php';
date_default_timezone_set('PRC');
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StdoutHandler());
$config = \Kafka\ConsumerConfig: :getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('10.13.4.159:9192');
$config->setGroupId('test');
$config->setBrokerVersion('0.9.0.1');
$config->setTopics(array('test'));
//$config->setOffsetReset('earliest');
$consumer = new \Kafka\Consumer(a);
$consumer->setLogger($logger);
$consumer->start(function($topic.$part.$message) {
var_dump($message);
});Copy the code
For details about how to call basic protocol APIS, see Example