Kafka client written in PHP

小云云
Release: 2023-03-17 19:06:02
Original
2473 people have browsed it

Kafka-php uses a kafka client written in pure PHP. It currently supports versions of Kafka 0.8.x or above. The project v0.2.x is not compatible with v0.1.x. If you use the original v0.1 For .x, you can refer to the document Kafka PHP v0.1.x Document, but it is recommended to switch to v0.2.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 language, it can be used without compiling any extensions, which reduces access and maintenance costs.

Installation environment requirements

  • PHP version is greater than 5.5

  • Kafka Server version is greater than 0.8.0

  • The consumption module Kafka Server version needs to be greater than 0.9.0

Installation

Use Composer to install

Add composer to depend on nmred/kafka -php to the composer.json file of the project, such as:

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

Produce

<?php
require &#39;../vendor/autoload.php&#39;;
date_default_timezone_set(&#39;PRC&#39;);
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger(&#39;my_logger&#39;);
// Now add some handlers
$logger->pushHandler(new StdoutHandler());

// 设置生产相关配置,具体配置参数见 [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 after login

Consumer

<?php
require &#39;../vendor/autoload.php&#39;;
date_default_timezone_set(&#39;PRC&#39;);
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger(&#39;my_logger&#39;);
// 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();
$consumer->setLogger($logger);
$consumer->start(function($topic, $part, $message) {
	var_dump($message);
});
Copy after login

The above content is a Kafka client tutorial written in PHP. I hope it can help everyone.

Related recommendations:

Example of handshake between client and server in Socket in python

php gets the visitor (client) IP and geographical location text tutorial

How to quickly develop a webservice client?

The above is the detailed content of Kafka client written in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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