1. Zookeeper在win10上安装启动:

1.先下载JDK,配置环境变量。
2.安装Zookeeper
① 下载地址:https://zookeeper.apache.org/releases.html ② 解压放在D:/Software目录下。注意:关于zookeeper以及kafka的目录,路径中最好不要出现空格,比如D:\Program Files,尽量别用,运行脚本时会有问题。
③ 进入zookeeper的相关设置所在的文件目录,例如本文的:D:\Software\zookeeper-3.4.10\conf。将"zoo_sample.cfg"重命名为"zoo.cfg"。
④ 打开zoo.cfg(至于使用什么编辑器,根据自己喜好选即可),找到并编辑:
dataDir=/tmp/zookeeper
改变为:D:/Software/zookeeper-3.4.10/data或 D:\Software\zookeeper-3.4.10\data(路径仅为示例,具体可根据需要配置)
这里注意,路径要么是"/“分割,要么是转义字符”\",这样会生成正确的路径(层级,子目录)。
⑤ 与配置jre类似,在系统环境变量中添加:
a.系统变量中添加ZOOKEEPER_HOME=D:\Software\zookeeper-3.4.10
b.编辑系统变量中的path变量,增加%ZOOKEEPER_HOME%\bin
⑤ 在zoo.cfg文件中修改默认的Zookeeper端口(默认端口2181),可以修改成其他的端口号。
⑤ 这是本文最终的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=D:/bigdata/zookeeper-3.4.10/data
 #dataDir=D:\bigdata\zookeeper-3.4.10\data
 #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

⑥ 打开cmd窗口,输入zkserver,运行Zookeeper,运行结果如下,则Zookeeper已经安装完成,已在2181端口运行。

kafka 查看zookeeper状态 kafka启动zookeeper_kafka 查看zookeeper状态

2. kafka在win10上安装启动:

1.下载地址:http://kafka.apache.org/downloads 2.要下载Binary downloads这个类型,不要下载源文件,这种方便使用。下载后,解压放在D:\Software目录下。
① 进入kafka配置文件所在目录,D:\bigdata\kafka_2.11-0.9.0.1\config
② 编辑文件"server.properties",找到并编辑:
log.dirs=/tmp/kafka-logs
改变为:
log.dirs=D:/Software/kafka_2.11-0.9.0.1/kafka-logs
或者D:\Software\kafka_2.11-0.9.0.1\kafka-logs
同样注意:路径要么是"/“分割,要么是转义字符”\",这样会生成正确的路径(层级,子目录)。错误路径情况可自行尝试,文件夹名为这种形式:bigdatakafka_2.11-0.9.0.1kafka-logs
③ 在server.properties文件中,zookeeper.connect=localhost:2181代表kafka所连接的zookeeper所在的服务器IP以及端口,可根据需要更改。本文在同一台机器上使用,故不用修改。
④ kafka会按照默认配置,在9092端口上运行,并连接zookeeper的默认端口2181。

  1. 运行kafka
    提示:请确保启动kafka服务器前,Zookeeper实例已经在运行,因为kafka的运行是需要zookeeper这种分布式应用程序协调服务。
    ① 进入kafka安装目录D:\Software\kafka_2.11-0.9.0.1
    ② 按下shift+鼠标右键,选择"在此处打开命令窗口",打开命令行。
    ③ 在命令行中输入:.\bin\windows\kafka-server-start.bat .\config\server.properties 回车。
    ④ 正确运行的情况为:

    zookeeper以及kafka都已正确运行。保持运行状态,不要关闭。

重要(操作日志的处理):

1.kafka启动后,如果你去查看kafka所在的根目录,或者是kafka本身的目录,会发现已经默认生成一堆操作日志(这样看起来真心很乱):

kafka 查看zookeeper状态 kafka启动zookeeper_Software_02


2.而且会不断生成不同时间戳的操作日志。刚开始不知所措,一番研究后,看了启动的脚本内容,发现启动的时候是会默认使用到这个log4j.properties文件中的配置,而在zoo.cfg是不会看到本身的启动会调用到这个,还以为只有那一个日志路径:

kafka 查看zookeeper状态 kafka启动zookeeper_zookeeper_03


3.在这里配置一下就可以了,找到config下的log4j.properties:

kafka 查看zookeeper状态 kafka启动zookeeper_Software_04


kafka 查看zookeeper状态 kafka启动zookeeper_zookeeper_05


4.将路径更改下即可,这样就可以归档在一个文件夹下边了,路径根据自己喜好定义:

kafka 查看zookeeper状态 kafka启动zookeeper_kafka 查看zookeeper状态_06


5.另外如何消除不断生成日志的问题,就是同一天的不同时间会不停生成。

修改这里,还是在log4j.properties中:

kafka 查看zookeeper状态 kafka启动zookeeper_Software_07


本身都为trace,字面理解为会生成一堆跟踪日志,将其改为INFO即可。

3. 如何创建主题Topic

①创建主题,命名为"test0811",replicationfactor=1(因为只有一个kafka服务器在运行)。可根据集群中kafka服务器个数来修改replicationfactor的数量,以便提高系统容错性等。

②在D:\bigdata\kafka_2.11-0.9.0.1\bin\windows目录下打开新的命令行

③输入命令:

kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test0811

回车。

kafka 查看zookeeper状态 kafka启动zookeeper_kafka_08


该窗口可以关闭。

4. 创建生产者(producer)和消费者(consumer)

①在D:\bigdata\kafka_2.11-0.9.0.1\bin\windows目录下打开新的命令行。
②输入命令,启动producer:

kafka-console-producer.bat --broker-list localhost:9092 --topic test0811

该窗口不要关闭。
③同样在该目录下打开新的命令行。
④输入命令,启动consumer:

kafka-console-consumer.bat --zookeeper localhost:2181 --topic test0811
0.90版本之后启动消费者的方法:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

注意:在做kafka测试的时候,使用命令bin/kafka-console-consumer.sh --zookeeper 192.168.0.140:2181,192.168.0.141:2181 --topic test --from-beginning启动消费者,发现一只报错consumer zookeeper is not a recognized option,搜索了半天,一只没有解决,最后,换了一个低版本的kakfa,发现在启动的时候说使用 --zookeeper是一个过时的方法,此时,才知道原来在最新的版本中,这种启动方式已经被删除了,
最后附上0.90版本之后启动消费者的方法: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

kafka 查看zookeeper状态 kafka启动zookeeper_kafka 查看zookeeper状态_09


现在生产者、消费者均已创建完成。⑤ 在producer命令行窗口中任意输入内容,回车 在consumer命令行窗口中即可看到相应的内容

kafka 查看zookeeper状态 kafka启动zookeeper_Software_10