4-09 1,651 views
参考:https://learnku.com/articles/15176/laravel-implementation-of-kafka-message-push-and-receive-processing
安装
安装librdkafka 库
git clone https://github.com/edenhill/librdkafka.git
./configure
make
sudo make install
安装rdkafka
sudo pecl install rdkafka
配置扩展到php.ini
extension=rdkafka.so
测试
<?php
#写代码测试
$conf = new \RdKafka\Conf();
// enable debug mode
$conf->set('log_level', LOG_DEBUG);
$conf->set('debug', 'all');
$rk = new \RdKafka\Producer($conf);
$rk->addBrokers("192.168.5.170:9092,192.168.5.99:9092");
$topic = $rk->newTopic("php_topic");
$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");
$producer = new \RdKafka\Producer();
$producer->addBrokers("192.168.5.170:9092,192.168.5.99:9092");
$obj_topic = $producer->newTopic("php_topic");
//
$input_handler = fopen('php://stdin', 'r');
while (true) {
echo "\nPlease input messages:\n";
$payload = trim(fgets($input_handler));
// empty message will be quit
if (empty($payload)) {
break;
}
// send message
$obj_topic->produce(RD_KAFKA_PARTITION_UA, 0, $payload);
}
echo "done\n";
exit(0);
使用方法
https://www.jianshu.com/p/e4bc3f56354d