SoFunction
Updated on 2025-03-10

php test kafka project example

This article describes the php test kafka project. Share it for your reference, as follows:

Overview

Kafka was originally developed by Linkedin. It is a distributed, partitioned, multi-copy, multi-subscriber-based distributed logging system (also used as an MQ system) coordinated by zookeeper. It can be commonly used for web/nginx logs, access logs, message services, etc. Linkedin contributed to the Apache Foundation in 2010 and became a top open source project.

The main application scenarios are: log collection system and message system.

Install kafka-php project dependencies

composer require nmred/kafka-php

<?php
require './vendor/';
date_default_timezone_set('PRC');
$config = \Kafka\ProducerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('127.0.0.1:9092');
$config->setBrokerVersion('0.10.2.1');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer(function() {
 $t = time();
 return array(
 array(
  'topic' => 'test',
  'value' => $t,
  'key' => $t,
 ),
 );
});
$producer->success(function($result) {
 var_export($result);
});
$producer->error(function($errorCode) {
 var_dump('error', $errorCode);
});
$producer->send();

<?php
require './vendor/';
date_default_timezone_set('PRC');
$config = \Kafka\ConsumerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('127.0.0.1:9092');
$config->setGroupId('test');
$config->setBrokerVersion('0.10.2.1');
$config->setTopics(array('test'));
$consumer = new \Kafka\Consumer();
$consumer->start(function($topic, $part, $message) {
 var_dump($message);
});

Test Producer

php 

Test consumers

php 

For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.