源码编译安装zookeeper
下载地址:https://archive.apache.org/dist/zookeeper/
cd /usr/local/
wget https://archive.apache.org/dist/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-3.4.9/
make
make install
设置全局变量
vim ~/.bash_profile
最后一行加入
export PATH=/usr/local/zookeeper-3.4.9/bin:$PATH
使之生效
source ~/.bash_profile
配置文件位置
/usr/local/zookeeper-3.4.9/conf/zoo.cfg
注意:默认配置文件并没有,直有一个案例文件 zoo_sample.cfg
使用的时候要 自行复制一个
cp /usr/local/zookeeper-3.4.9/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.9/conf/zoo.cfg
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
./configure --with-php-config=/opt/lampp/bin/php-config --with-libzookeeper-dir=/usr/local/zookeeper-3.4.9/
make
make install
3.安装librdkafka
cd /usr/local/src #进入安装包存放目录
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.安装phpkafka
cd /usr/local/src #进入安装包存放目录
wget https://github.com/EVODelavega/phpkafka/archive/master.zip #下载
mv master.zip phpkafka-master.zip #修改包名
unzip phpkafka-master.zip #解压
cd phpkafka-master #进入安装文件夹
/usr/local/php/bin/phpize #加载php扩展模块
./configure --enable-kafka --with-php-config=/usr/local/php/bin/php-config #配置
make #编译
make install #安装
5.配置扩展
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.安装composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
8、composer 安装 nmred/kafka-php
使用 :进入composer.json 所在的目录 执行:composer install
composer.json 内容如下:
{
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
},
"require": {
"nmred/kafka-php": "0.1.*"
}
}
repositories的作用是使得 composer安装的时候 速度更快
require 则是需要下载的类库
8、PHP 调用下载的类库
require 'vendor/autoload.php';
$consumer = \Kafka\Consumer::getInstance('localhost:2181/kafka');
$consumer->setGroup(1);
$consumer->setPartition('topicName',0,9);
while(true)
{
$result = $consumer->fetch();
if($result)
{
foreach ($result as $topicName => $topic) {
foreach ($topic as $partId => $partition) {
foreach ($partition as $message) {
var_dump((string)$message);
}
}
}
}
sleep(1);
}
转载于:https://blog.51cto.com/11910656/2085323