kafka是制作消息队列的,与redis相似,但是优于redis,支持消息并列与通知,
安装思路及环境:先安装Java环境,再安装zookeeper,后安装kafka,:安装思路及环境:先安装Java环境,再安装zookeeper,后安装kafka,
zookeeper版本下载网址: http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
http://zookeeper.apache.org/doc/current/zookeeperStarted.html中有:
server.1=172.18.9.203:2888:3888 server.2=172.18.9.204:2888:3888 server.3=172.18.9.205:2888:3888
制作zookeeper集群需三台服务器:172.18.9.203,172.18.9.204,172.18.9.205
三台服务器分别配置hosts文件:
#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.18.9.203 linux-host1.exmaple.com
172.18.9.204 linux-host2.exmaple.com
172.18.9.205 linux-host3.exmaple.com
[root@203/204/205 src]#tar xf zookeeper-3.4.14.tar.gz
[root@203/204/205 src]#cd zookeeper-3.4.14/conf
[root@203/204/205 conf]#ll
total 12
-rw-rw-r--. 1 2002 2002 535 Mar 7 00:50 configuration.xsl
-rw-rw-r--. 1 2002 2002 2161 Mar 7 00:50 log4j.properties
-rw-rw-r--. 1 2002 2002 922 Mar 7 00:50 zoo_sample.cfg
[root@203/204/205 conf]#cp zoo_sample.cfg zoo.cfg #把模板cp成配置文件
[root@203/204/205 conf]#vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data
clientPort=2181
maxClientCnxns=4096
server.1=172.18.9.203:2888:3888
server.2=172.18.9.204:2888:3888
server.3=172.18.9.205:2888:3888
[root@203/204/205 conf]#mkdir /usr/local/zookeeper/data
[root@203/204/205 conf]#ln -s /usr/local/src/zookeeper-3.4.14 /usr/local/zookeeper
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@205 conf]#/usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
出现此错的原因是zookeeper的pid没有生成!
[root@203/204/205 zookeeper-3.4.14]#echo 1 2 3 > /usr/local/zookeeper/data/myid
[root@203/204/205 zookeeper-3.4.14]#
[root@203/204/205 zookeeper-3.4.14]#/usr/local/zookeeper/bin/zkServer.sh start
[root@203/204/205 zookeeper-3.4.14]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :::2181 :::*
[root@203 zookeeper-3.4.14]#/usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@204 conf]#/usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@205 conf]#/usr/local/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@203/204/205 zookeeper-3.4.14]#vim /etc/rc.d/rc.local
/usr/local/zookeeper/bin/zkServer.sh start
此时,zookeeper已完成安装,它没有账号认证,任何普户都能用!
[root@203/204/205 src]#tar xf kafka_2.12-2.2.0.tar.gz
[root@203/204/205 src]#ln -s /usr/local/src/kafka_2.12-2.2.0 /usr/local/kafka
[root@203/204/205 src]#vim /usr/local/kafka/config/server.properties
21 broker.id=1 2 3 #设置每个代理全局唯一的整数ID
31 listeners=PLAINTEXT://172.18.9.203:9092
103 log.retention.hours=24 #保留指定小时的日志内容
123 zookeeper.connect=172.18.9.203:2181,172.18.9.204:2181,172.18.9.205:2181 #所有的zookeeper地址
[root@203/204/205 ~]#/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
[root@203 src]#jps
26632 Jps
26602 Kafka
23453 QuorumPeerMain
[root@204 src]#jps
41969 Jps
41939 Kafka
34490 QuorumPeerMain
[root@205 src]#jps
19785 QuorumPeerMain
23066 Kafka
23147 Jps
http://www.kafkatool.com/download.html Windows 64-Bit download
[root@204 src]#/usr/local/kafka/bin/kafka-topics.sh --create --zookeeper 172.18.9.203:2181,172.18.9.204:2181,172.18.9.205:2181 --partitions 3 --replication-factor 3 --topic logstashtest
Created topic logstashtest.
two:测试获取topic:可在任意一台服务器进行测试:
[root@205 src]#/usr/local/kafka/bin/kafka-topics.sh --describe --zookeeper 172.18.9.203:2181,172.18.9.204:2181,172.18.9.205:2181 --topic logstashtest
Topic:logstashtest PartitionCount:3 ReplicationFactor:3 Configs:
Topic: logstashtest Partition: 0 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
Topic: logstashtest Partition: 1 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
Topic: logstashtest Partition: 2 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
状态说明:logstashtest有三个分区分别为0、1、2,分区0的leader是3(broker.id),分区0有三个副本,并且状态都为lsr(ln-sync,表示可以参加选举成为leader)。
three:删除topic:[root@205 src]#/usr/local/kafka/bin/kafka-topics.sh --delete --zookeeper 172.18.9.203:2181,172.18.9.204:2181,172.18.9.205:2181 --topic logstashtest
Topic logstashtest is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
four:获取所有的topic:
[root@205 src]#/usr/local/kafka/bin/kafka-topics.sh --list --zookeeper 172.18.9.203:2181,172.18.9.204:2181,172.18.9.205:2181
logstashtest #此为topic的内容
转载于:https://blog.51cto.com/14128387/2385777