PHP test Kafka project example

Time:2021-7-27

This article describes the PHP test Kafka project as an example. Share with you for your reference, as follows:

summary

Kafka was originally developed by LinkedIn. It is a distributed, partitioned, multi replica, multi subscriber distributed logging system based on zookeeper coordination (which can also be used as an MQ system). 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.

Installing Kafka PHP project dependencies


composer require nmred/kafka-php

produce.php


<?php
require './vendor/autoload.php';
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();

consumer.php


<?php
require './vendor/autoload.php';
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 produce.php

Test consumer


php consumer.php

For more information about PHP, readers who are interested can see the topics on this site: introduction to PHP object-oriented programming, complete operation skills of PHP array, introduction to PHP basic syntax, summary of PHP operation and operator usage, summary of PHP character string usage Introduction to PHP + MySQL database operation and summary of common PHP database operation skills

I hope this article will help you in PHP programming.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]