Code Monkey home page Code Monkey logo

php-kafka-swoole's Introduction

PHP-Rdkafka Demo#

环境依赖

Kafka高级别消费者(kafkaHighConsumer)使用

include 'kafkaHighConsumer.php';

$kafkaobj = new kafkaHighConsumer();
$kafkaobj->Main(function () {
	$this->topics = ['lowtest'];
	$this->group_id = 'lowtest';
}, function ($message) {
	echo 'offset:' . $message->offset . 'partition' . $message->partition . "\n";
});
  • $this->topics 是你的要订阅的topic
  • $this->group_id 是你的分组名称
  • Main方法的第一个参数是用的闭包设置参数,第二个参数是获取到的消息进行处理的方法

多线程高级消费者(taskKafkaConsumer)使用

支持一次性开启多个单一分组的consumer,当然最好consumer的个数不能大于producers的个数

include 'taskKafkaConsumer.php';

$kafkaobj = new taskKafkaConsumer(function () {
	$this->topics = ['lowtest'];
	$this->group_id = 'lowtest';
	$this->task_worker_num = 2;
}, function ($message) {
	echo 'offset:' . $message->offset . 'partition' . $message->partition . "\n";
});
  • $this->topics 是你的要订阅的topic
  • $this->group_id 是你的分组名称
  • $this->task_worker_num 启动consumer的个数
  • Main方法的第一个参数是用的闭包设置参数,第二个参数是获取到的消息进行处理的方法

Kafka生产者(kafkaProducer)使用

include 'kafkaProducer.php';

$kafkaobj = new kafkaProducer();
$kafkaobj->Main('msg', function () {
		$this->topic = 'lowtest';
	});
  • $this->topic 设置送消息的topic(单一)
  • Main方法的第一个参数是你要发送的消息,第二个是配置参数的匿名函数

配置文件(kafkaProducer)

需要建立一个配置文件,指定你的BORCKERS。

define('KAFKA_BORCKERS', 'ip地址:端口'); 

php-kafka-swoole's People

Contributors

chaiyue avatar wocaolecn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.