ubuntu安装kafka单机环境及php kafka扩展(java jdk,zookeeper,kafka)单机版
程序员文章站
2024-01-12 16:10:34
...
1.安装php-rdkafka 扩展
git clone https://github.com/edenhill/librdkafka.git
cd librdkafka
./configure
make && make install
编译结果:
2.pecl安装rdkafka【一定要先安装第一步,不然这一步rdkafka安装不成功】
pecl install rdkafka
3.加入rdkafka.ini到对应的php版本中
#可用php -v查看php版本
aaa@qq.com:/librdkafka# cd /etc/php/7.2/
aaa@qq.com:/etc/php/7.2# ll
total 20
drwxr-xr-x 5 root root 4096 Nov 9 2018 ./
drwxr-xr-x 7 root root 4096 Nov 9 2018 ../
drwxr-xr-x 3 root root 4096 Nov 9 2018 cli/
drwxr-xr-x 4 root root 4096 Nov 9 2018 fpm/
drwxr-xr-x 2 root root 4096 Nov 9 2018 mods-available/
aaa@qq.com:/etc/php/7.2# cd cli/conf.d/
aaa@qq.com:/etc/php/7.2/cli/conf.d# vi rdkafka.ini
aaa@qq.com:/etc/php/7.2/cli/conf.d# cd ../../fpm/conf.d/
aaa@qq.com:/etc/php/7.2/fpm/conf.d# vi rdkafka.ini
aaa@qq.com:/etc/php/7.2/fpm/conf.d# php -m
#这里的cli/conf.d文件中,新建编辑一个rdkafka.ini文件,里面加入 extension="rdkafka" 保存退出
#这里的fpm/conf.d文件中,新建编辑一个rdkafka.ini文件,里面加入 extension="rdkafka" 保存退出
然后查看php扩展 php-m
aaa@qq.com:/etc/php/7.2/fpm/conf.d# php -m
[PHP Modules]
bcmath
blackfire
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
imap
intl
json
ldap
libxml
mbstring
memcached
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
rdkafka #我们的新扩展
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib
4.安装java的jdk,后面提供下载地址,官网提供的下载太慢了
#进入/usr/目录下 新建 java文件夹
aaa@qq.com:/usr# mkdir java
#将提供的java的jdk文件cp复制到我们的java文件夹中
aaa@qq.com:/usr/java# cp /home/vagrant/Code/kafka_resources/jdk-linux-x64.tar.gz ./
#解压
aaa@qq.com:/usr/java# tar -zxvf jdk-linux-x64.tar.gz
#解压完毕后可看到文件夹
aaa@qq.com:/usr/java# ll
total 181204
drwxr-xr-x 3 root root 4096 Jan 19 04:52 ./
drwxr-xr-x 13 root root 4096 Jan 19 04:52 ../
drwxr-xr-x 8 uucp 143 4096 Mar 15 2017 jdk1.8.0_131/
-rwxr-xr-x 1 root root 185540433 Jan 19 04:51 jdk-linux-x64.tar.gz*
#编辑 /etc/profile中的文件开头加入如下语句
export JAVA_HOME=/usr/java/jdk1.8.0_131 #这里要和你安装的地址对应上
export CLASSPATH=$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
#保存退出,执行环境更新
aaa@qq.com:/usr/java# source /etc/profile
#查看java版本
aaa@qq.com:/usr/java# java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
5.安装java,zookeeper,kafka
#新建zookeeper文件夹
aaa@qq.com:/usr# mkdir zookeeper
#将提供的zookeeper的文件cp复制到我们的zookeeper文件夹中
aaa@qq.com:/usr/zookeeper# cp /home/vagrant/Code/kafka_resources/zookeeper-3.4.5.tar.gz ./
#解压
aaa@qq.com:/usr/zookeeper# tar -zxvf zookeeper-3.4.5.tar.gz
#解压完毕后可看到文件夹
aaa@qq.com:/usr/zookeeper# ll
total 16032
drwxr-xr-x 3 root root 4096 Jan 19 05:14 ./
drwxr-xr-x 13 root root 4096 Jan 19 04:52 ../
drwxr-xr-x 10 501 staff 4096 Nov 5 2012 zookeeper-3.4.5/
-rwxr-xr-x 1 root root 16402010 Jan 19 05:14 zookeeper-3.4.5.tar.gz*
#进入到zookeepr-3.4.5/conf文件夹中去
aaa@qq.com:/usr/zookeeper# cd zookeeper-3.4.5/conf/
#查看文件
aaa@qq.com:/usr/zookeeper/zookeeper-3.4.5/conf# ll
total 20
drwxr-xr-x 2 501 staff 4096 Jan 19 05:14 ./
drwxr-xr-x 10 501 staff 4096 Nov 5 2012 ../
-rw-r--r-- 1 501 staff 535 Sep 30 2012 configuration.xsl
-rw-r--r-- 1 501 staff 2161 Sep 30 2012 log4j.properties
-rw-r--r-- 1 501 staff 808 Sep 30 2012 zoo_sample.cfg
#将zoo_sample.cfg复制一份,并改名为zoo.cfg
aaa@qq.com:/usr/zookeeper/zookeeper-3.4.5/conf# cp zoo_sample.cfg zoo.cfg
aaa@qq.com:/usr/zookeeper/zookeeper-3.4.5/conf# ll
total 24
drwxr-xr-x 2 501 staff 4096 Jan 19 05:18 ./
drwxr-xr-x 10 501 staff 4096 Nov 5 2012 ../
-rw-r--r-- 1 501 staff 535 Sep 30 2012 configuration.xsl
-rw-r--r-- 1 501 staff 2161 Sep 30 2012 log4j.properties
-rw-r--r-- 1 root root 808 Jan 19 05:18 zoo.cfg
-rw-r--r-- 1 501 staff 808 Sep 30 2012 zoo_sample.cfg
#编辑zoo.cfg文件
aaa@qq.com:/usr/zookeeper/zookeeper-3.4.5/conf# vi zoo.cfg
#内容如下
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper/data #如果没有这个目录需要自己新建
# the port at which the clients will connect
clientPort=2181
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#新建/tmp/zookeeper/data目录,并修改文件夹权限
aaa@qq.com:/usr/zookeeper/zookeeper-3.4.5/conf# cd /tmp/
aaa@qq.com:/tmp# mkdir zookeeper
aaa@qq.com:/tmp# cd zookeeper/
aaa@qq.com:/tmp/zookeeper# mkdir data
aaa@qq.com:/tmp/zookeeper# cd ../
aaa@qq.com:/tmp# chmod -R 777 zookeeper/
#为zookeeper创建环境变量,在文件开头加入如下两个export地址,保存退出
aaa@qq.com:/tmp# vi /etc/profile
export ZOOKEEPER_HOME=/tmp/zookeeper
export PATH=.:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin:$PATH
#加入结果# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
export JAVA_HOME=/usr/java/jdk1.8.0_131
export CLASSPATH=$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
export ZOOKEEPER_HOME=/tmp/zookeeper
export PATH=.:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin:$PATH
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
~
#配置kafka单机
aaa@qq.com:/usr# mkdir kafka
#将提供的kafka的文件cp复制到我们的kafka文件夹中
aaa@qq.com:/usr/kafka# cp /home/vagrant/Code/kafka_resources/kafka_2.13-2.4.0.tgz ./
#解压
aaa@qq.com:/usr/kafka# tar -zxvf kafka_2.13-2.4.0.tgz
#进入kafka/config目录下:
aaa@qq.com:/usr/kafka# cd kafka_2.13-2.4.0/config/
#编辑server.properties
aaa@qq.com:/usr/kafka/kafka_2.13-2.4.0/config# vi server.properties
#配置server.properties:
#broker.id需改成正整数,单机为1就好
broker.id=1
#自定义指定端口号【新加配置】
port=9092
#自定义加入hostname【新加配置】
#localhost这一项还有其他要修改,详细见下面说明
host.name=localhost
#指定kafka的日志目录
log.dirs=/tmp/kafka-logs
#连接zookeeper配置项,这里指定的是单机,所以只需要配置localhost,若是实际生产环境,需要在这里添加其他ip地址和端口号
zookeeper.connect=localhost:2181
#按照上面的方法,vi几个文件,修改配置
#配置zookeeper.properties:
dataDir=/tmp/zookeeper/data
clientPort=2181
host.name=localhost
#配置producer.properties:
zookeeper.connect=localhost:2181
#配置consumer.properties:
zookeeper.connect=localhost:2181
#在/tmp下新建一个kafka-logs文件夹并修改权限
aaa@qq.com:# cd /tmp/
aaa@qq.com:/tmp# mkdir kafka-logs
aaa@qq.com:/tmp# chmod -R 777 kafka-logs/
6,先启动zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
7.启动kafka
bin/kafka-server-start.sh config/server.properties
8.创建一个名为topTest的topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topTest
文件下载:
链接:https://pan.baidu.com/s/1_aTOLr3jwQ-oref1eBqlKQ
提取码:32b5
上一篇: 使用Puppeteer图像识别技术如何实现百度指数爬虫
下一篇: php下的RSA算法实现