环境:
CentOS release 6.7 (Final)
kafka_2.11-0.11.0.1.tgz
zookeeper-3.4.8.tar.gz
jdk-7u2-linux-x64.tar.gz(其他版本可能会出错,Javase9出错)
1、安装jdk
解压
[root@localhost jdk1.7.0_02]# ls
bin COPYRIGHT db include jre lib LICENSE man README.html release src.zip THIRDPARTYLICENSEREADME.txt
[root@localhost jdk1.7.0_02]# pwd
/usr/jdk1.7.0_02
添加环境变量
[root@localhost jdk1.7.0_02]# vim /etc/profile
#jdk
JAVA_HOME=/usr/jdk1.7.0_02/
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
[root@localhost jdk1.7.0_02]# source /etc/profile
[root@localhost jdk1.7.0_02]# java -version
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)
2、安装zookeeper
解压
[root@localhost ~]# tar zxvf zookeeper-3.4.8.tar.gz
配置环境变量
[root@localhost ~]# vim /etc/profile
#zookeeper环境变量设置
ZOOKEEPER_HOME=/usr/local/logdeal/zookeeper-3.4.6
PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$ZOOKEEPER_HOME/lib:
export ZOOKEEPER_HOME
配置
配置文件在$ZOOKEEPER_HOME/conf/ 目录下,将zoo_sample.cfg文件名改成zoo.cfg。
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
[root@localhost conf]# cat 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
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# 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
配置说明:
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataLogDir: log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
3、启动zookeeper
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ...STARTED
查看zookeeper进程
[root@localhost bin]# ps -ef|grep zookeeper
root 7158 1 4 20:12 pts/6 00:00:05 /usr/jdk1.7.0_02/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /usr/zookeeper-3.4.8/bin/../build/classes:/usr/zookeeper-3.4.8/bin/../build/lib/*.jar:/usr/zookeeper-3.4.8/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/zookeeper-3.4.8/bin/../lib/slf4j-api-1.6.1.jar:/usr/zookeeper-3.4.8/bin/../lib/netty-3.7.0.Final.jar:/usr/zookeeper-3.4.8/bin/../lib/log4j-1.2.16.jar:/usr/zookeeper-3.4.8/bin/../lib/jline-0.9.94.jar:/usr/zookeeper-3.4.8/bin/../zookeeper-3.4.8.jar:/usr/zookeeper-3.4.8/bin/../src/java/lib/*.jar:/usr/zookeeper-3.4.8/bin/../conf:.:/usr/jdk1.7.0_02/lib/tools.jar:/usr/jdk1.7.0_02/lib/dt.jar -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /usr/zookeeper-3.4.8/bin/../conf/zoo.cfg
查看zookeeper端口
[root@localhost bin]# netstat -anput|grep 7158
tcp 0 0 :::2181 :::* LISTEN 7158/java
tcp 0 0 :::45554 :::* LISTEN 7158/java
tcp 0 0 ::ffff:192.168.196.128:2181 ::ffff:192.168.196.12:52431 ESTABLISHED 7158/java
tcp 0 0 ::ffff:192.168.196.128:2181 ::ffff:192.168.196.12:52432 ESTABLISHED 7158/java
tcp 0 0 ::ffff:192.168.196.128:2181 ::ffff:192.168.196.12:52434 ESTABLISHED 7158/java
tcp 0 0 ::ffff:127.0.0.1:2181 ::ffff:127.0.0.1:38755 ESTABLISHED 7158/java
4、安装kafka
[root@localhost ~]# tar zxvf
kafka_2.11-0.11.0.1.tgz
kafka目录介绍
/bin 操作kafka的可执行脚本,还包含windows下脚本
/config 配置文件所在目录
/libs 依赖库目录
/logs 日志数据目录,目录kafka把server端日志分为5种类型,为:server,request,state,log-cleaner,controller
5、启动kafka
[root@localhost bin]# ./kafka-server-start.sh /usr/kafka_2.11-0.11.0.1/config/server.properties &
[1] 7234
随后会显示配置信息,和日志信息。在此省略
查看kafka进程
[root@localhost ~]# ps -ef|grep kafka
root 5542 3244 1 19:30 pts/3 00:02:36 /usr/jdk1.7.0_02/bin/java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+DisableExplicitGC -Djava.awt.headless=true -Xloggc:/usr/kafka_2.11-0.11.0.1/bin/../logs/kafkaServer-gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dkafka.logs.dir=/usr/kafka_2.11-0.11.0.1/bin/../logs -Dlog4j.configuration=file:/usr/kafka_2.11-0.11.0.1/bin/../config/log4j.properties -cp .:/usr/jdk1.7.0_02/lib/tools.jar:/usr/jdk1.7.0_02/lib/dt.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/aopalliance-repackaged-2.5.0-b05.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/argparse4j-0.7.0.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/commons-lang3-3.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/connect-api-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/connect-file-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/connect-json-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/connect-runtime-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/connect-transforms-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/guava-20.0.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/hk2-api-2.5.0-b05.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/hk2-locator-2.5.0-b05.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/hk2-utils-2.5.0-b05.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-annotations-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-core-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-databind-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-jaxrs-base-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-jaxrs-json-provider-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jackson-module-jaxb-annotations-2.8.5.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javassist-3.21.0-GA.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javax.annotation-api-1.2.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javax.inject-1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javax.inject-2.5.0-b05.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javax.servlet-api-3.1.0.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/javax.ws.rs-api-2.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-client-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-common-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-container-servlet-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-container-servlet-core-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-guava-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-media-jaxb-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jersey-server-2.24.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-continuation-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-http-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-io-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-security-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-server-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-servlet-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-servlets-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jetty-util-9.2.15.v20160210.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/jopt-simple-5.0.3.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka_2.11-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka_2.11-0.11.0.1-sources.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka_2.11-0.11.0.1-test-sources.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka-clients-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka-log4j-appender-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka-streams-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka-streams-examples-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/kafka-tools-0.11.0.1.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/log4j-1.2.17.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/lz4-1.3.0.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/maven-artifact-3.5.0.jar:/usr/kafka_2.11-0.11.0.1/bin/../libs/metrics-core-2.2.0.jar:/usr
查看kafka端口
[root@localhost ~]# netstat -anput|grep 5542
tcp 0 0 :::9092 :::* LISTEN 5542/java
tcp 0 0 :::35127 :::* LISTEN 5542/java
tcp 0 0 ::ffff:127.0.0.1:38755 ::ffff:127.0.0.1:2181 ESTABLISHED 5542/java
6、创建topic
[root@localhost ~]# /usr/kafka_2.11-0.11.0.1/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
验证topic是否创建成功
[root@localhost ~]# /usr/kafka_2.11-0.11.0.1/bin/kafka-topics.sh --list --zookeeper localhost:2181
test
7、测试
Xshell 打开两个窗口,一个是producer发送消息窗口,一个是console消费消息窗口。
producer发送消息窗口:
[root@localhost ~]# /usr/kafka_2.11-0.11.0.1/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>this is a message
>>hello
>
console消费消息窗口:
[root@localhost ~]# /usr/kafka_2.11-0.11.0.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
This is a message
This is another messagethis is a message
hello
经测试消息可以转发