风来了.fox
1.安装libzookeeper
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
tar -xf zookeeper-3.4.9.tar.gz
cd zookeeper-3.4.9/src/c
./configure -prefix=/usr/local/zookeeper/zookeeper-3.4.9/
make && make install
2.安装php zookeeper扩展
wget http://pecl.php.net/get/zookeeper-0.2.2.tgz
tar -zxvf zookeeper-0.2.2.tgz
cd zookeeper-0.2.2
phpize
./configure -with-php-config=/www/lanmps/php5.6.23/bin/php-config -with-libzookeeper-dir=/usr/local/zookeeper/zookeeper-3.4.9/
make && make install
注意:phpize 为你的PHP版本目录的 phpize
注意最新版kafka请使用7(3和4PASS)
3.安装librdkafka
wget https://github.com/edenhill/librdkafka/archive/master.zip
mv master.zip librdkafka-master.zip
unzip librdkafka-master.zip
cd librdkafka-master
./configure
make
make install
4.安装php-kafka扩展
wget https://github.com/EVODelavega/phpkafka/archive/master.zip
mv master.zip phpkafka-master.zip
unzip phpkafka-master.zip
cd phpkafka-master
phpize
./configure --enable-kafka --with-php-config=/www/lanmps/php5.6.23/bin/php-config
make #编译
make install #安装
注意:phpize 为你的PHP版本目录的 phpize
5.配置扩展
编辑PHP配置文件
vim php.ini
末尾增加
extension=zookeeper.so
extension=kafka.so
注意:先查找 extension_dir 是否已经配置过,如果没有配置,请自行配置。
每个扩展编译安装成功后都会输出(类似如下)
Installing shared extensions: /安装目录/php5.6.23/lib/php/extensions/no-debug-non-zts-20131226/
只要把地址复制,改成如下格式,加入到php.ini里
extension_dir=/安装目录/php5.6.23/lib/php/extensions/no-debug-non-zts-20131226/
6.查看是否安装完成
在
phpinfo();
里面查看
是否有 kafka,zookeeper 扩展,如果有表示安装成功
7.PHP-kafka扩展(3和4安装完成后无法使用时)
https://github.com/nmred/kafka-php
使用此扩展,支持最新版kafka
这里使用composer安装的,以下是示例(example文件夹下):
producer.php
require 'vendor/autoload.php';
$part = mt_rand(0, 1);
$produce = \Kafka\Produce::getInstance('localhost:2181', 3000);
// get available partitions
$partitions = $produce->getAvailablePartitions('topic_name');
var_dump($partitions);
// send message
$produce->setRequireAck(-1);
$produce->setMessages('topic_name', 0, array(date('Y-m-d H:i:s'));
consumer.php
require 'vendor/autoload.php';
$consumer = \Kafka\Consumer::getInstance('localhost:2181');
$group = 'topic_name';
$consumer->setGroup($group);
$consumer->setFromOffset(true);
$consumer->setTopic('topic_name', 0);
$consumer->setMaxBytes(102400);
$result = $consumer->fetch();
print_r($result);
foreach ($result as $topicName => $partition) {
foreach ($partition as $partId => $messageSet) {
var_dump($partition->getHighOffset());
foreach ($messageSet as $message) {
var_dump((string)$message);
}
var_dump($partition->getMessageOffset());
}
}