http://blog.csdn.net/fenglailea/article/details/52458737#t3
风来了.fox
安装zookeeper
推荐1.1 直接复制
1.1.直接安装zookeeper(无须编译)
下载地址:http://mirror.bit.edu.cn/apache/zookeeper/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
tar -zxvf zookeeper-3.4.9.tar.gz
mv zookeeper-3.4.9/ /usr/local/zookeeper/
- 1
- 2
- 3
- 4
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
设置全局变量
vim ~/.bash_profile
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
最后一行加入
export PATH=/usr/local/zookeeper/bin:$PATH
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
使之生效
source ~/.bash_profile
- 1
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
配置文件位置
/usr/local/zookeeper/conf/zoo.cfg
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
注意:默认配置文件并没有,直有一个案例文件 zoo_sample.cfg
使用的时候要 自行复制一个
cp /usr/local/zookeeper/conf/zoo_sample.cfg /usr/local/zookeeper/conf/zoo.cfg
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
1.2.源码编译安装zookeeper
下载地址:http://mirror.bit.edu.cn/apache/zookeeper/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
tar -zxf 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
- 1
- 2
- 3
- 4
- 5
- 6
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
设置全局变量
vim ~/.bash_profile
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
最后一行加入
export PATH=/usr/local/zookeeper/zookeeper-3.4.9/bin:$PATH
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
使之生效
source ~/.bash_profile
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
配置文件位置
/usr/local/zookeeper/zookeeper-3.4.9/conf/zoo.cfg
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
注意:默认配置文件并没有,直有一个案例文件 zoo_sample.cfg
使用的时候要 自行复制一个
cp /usr/local/zookeeper/zookeeper-3.4.9/conf/zoo_sample.cfg /usr/local/zookeeper/zookeeper-3.4.9/conf/zoo.cfg
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
注意: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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
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 #安装
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
注意:phpize 为你的PHP版本目录的 phpize
5.配置扩展
编辑PHP配置文件
vim php.ini
- 1
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
末尾增加
extension=zookeeper.so
extension=kafka.so
- 1
- 2
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
注意:先查找 extension_dir 是否已经配置过,如果没有配置,请自行配置。
每个扩展编译安装成功后都会输出(类似如下)
Installing shared extensions: /安装目录/php5.6.23/lib/php/extensions/no-debug-non-zts-20131226/
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
只要把地址复制,改成如下格式,加入到php.ini里
extension_dir=/安装目录/php5.6.23/lib/php/extensions/no-debug-non-zts-20131226/
- 1
![](http://static.blog.csdn.net/images/save_snippets_01.png)
- 1
6.查看是否安装完成
在
phpinfo();
里面查看
是否有 kafka,zookeeper 扩展,如果有表示安装成功
7.PHP-kafka扩展(3和4安装完成后无法使用时)
https://github.com/nmred/kafka-php
使用此扩展,支持最新版kafka
这里使用composer安装的,以下是示例(example文件夹下):
producer.php
<?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'));
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
![](http://static.blog.csdn.net/images/save_snippets.png)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
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());
}
}