kafka是一种高吞吐量的分布式发布订阅消息系统,和传统的MQ相比,传统的MQ是基于内存的处理,并且消费即清楚掉,而kafka是基于硬盘存储,且有序的保存。我们先学习一下kafka的集群部署,这里选在在一台机器上部署kafka集群,我已经下载了文件,下面看是进行解压。
[root@www.wulaoer.org ~]# wget http://mirror-hk.koddos.net/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz [root@www.wulaoer.org ~]# tar -zxf kafka_2.12-2.5.0.tgz [root@www.wulaoer.org ~]# mv kafka_2.12-2.5.0 /usr/local/kafka/ [root@www.wulaoer.org ~]# cd /usr/local/kafka/ [root@www.wulaoer.org kafka]# mv kafka_2.12-2.5.0 kafka01 [root@www.wulaoer.org kafka]# mkdir kafka01/{data,logs} [root@www.wulaoer.org kafka]# cp -a kafka01 kafka02 [root@www.wulaoer.org kafka]# cp -a kafka01 kafka03
kafka依赖于java,所以,我们需要设置好java后在进行部署准备,下面开始针对这三个服务,做一下设置,设置kafka集群。
配置kafka
kafka的环境已经做好了,下面配置kafka。
[root@www.wulaoer.org kafka]# echo "1" > kafka01/data/myid [root@www.wulaoer.org kafka]# echo "2" > kafka02/data/myid [root@www.wulaoer.org kafka]# echo "3" > kafka03/data/myid
这里的设置需要依赖每个kafka服务的id,必须要设置,如果不设置在启动的时候启动失败。kafka中自带了zookeeper服务,所以需要先配置zookeeper之后在配置kafka。
[root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka01/config/zookeeper.properties tickTime=2000 initLimit=10 syncLimit=5 clientPort=2181 dataDir=/usr/local/kafka/kafka01/data dataLogDir=/usr/local/kafka/kafka01/logs server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 [root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka02/config/zookeeper.properties tickTime=2000 initLimit=10 syncLimit=5 clientPort=2182 dataDir=/usr/local/kafka/kafka02/data dataLogDir=/usr/local/kafka/kafka02/logs server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 [root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka03/config/zookeeper.properties tickTime=2000 initLimit=10 syncLimit=5 clientPort=2183 dataDir=/usr/local/kafka/kafka03/data dataLogDir=/usr/local/kafka/kafka03/logs server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890
配置kafka服务,这里包含kafka的日志路径和kafka的数据路径,还有就是server后面的id,必须和myid中的一致。kafka依赖zookeeper,不过zookeeper已经在kafka中内置了,所以设置好kafka后也需要设置一下zookeeper,下面设置zookeeper。
[root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka01/config/server.properties broker.id=1 advertised.listeners=PLAINTEXT://10.211.55.36:9091 log.dirs=/tmp/kafka1-logs zookeeper.connect=10.211.55.36:2181,10.211.55.36:2182,10.211.55.36:2183 [root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka02/config/server.properties broker.id=2 advertised.listeners=PLAINTEXT://10.211.55.36:9092 log.dirs=/tmp/kafka1-logs zookeeper.connect=10.211.55.36:2181,10.211.55.36:2182,10.211.55.36:2183 [root@www.wulaoer.org ~]# vim /usr/local/kafka/kafka03/config/server.properties broker.id=3 advertised.listeners=PLAINTEXT://10.211.55.36:9093 log.dirs=/tmp/kafka1-logs zookeeper.connect=10.211.55.36:2181,10.211.55.36:2182,10.211.55.36:2183
kafka已经配置完了,下面启动kafka。
启动kafka
kafka已经配置完成,不过上面已经说了kafka中已经有zookeeper,启动顺序必须先启动zookeeper后在启动kafka。
[root@www.wulaoer.org ~]# /usr/local/kafka/kafka01/bin/zookeeper-server-start.sh /usr/local/kafka/kafka01/config/zookeeper.properties > /dev/null 2>&1 & [root@www.wulaoer.org ~]# /usr/local/kafka/kafka01//bin/kafka-server-start.sh /usr/local/kafka/kafka01/config/server.properties > /dev/null 2>&1 & [root@www.wulaoer.org ~]# /usr/local/kafka/kafka02/bin/zookeeper-server-start.sh /usr/local/kafka/kafka02/config/zookeeper.properties > /dev/null 2>&1 & [root@www.wulaoer.org ~]# /usr/local/kafka/kafka02//bin/kafka-server-start.sh /usr/local/kafka/kafka02/config/server.properties > /dev/null 2>&1 & [root@www.wulaoer.org ~]# /usr/local/kafka/kafka03/bin/zookeeper-server-start.sh /usr/local/kafka/kafka03/config/zookeeper.properties > /dev/null 2>&1 & [root@www.wulaoer.org ~]# /usr/local/kafka/kafka03//bin/kafka-server-start.sh /usr/local/kafka/kafka03/config/server.properties > /dev/null 2>&1 &
kafka服务已经启动完成,我们可以验证一下kafka的功能。
验证kafka
我们需要先创建一个kafka,这里的replication-factor 3就是id的总数。
[root@www.wulaoer.org ~]# /usr/local/kafka/kafka01/bin/kafka-topics.sh --create --bootstrap-server 127.0.0.1:9092 --replication-factor 3 --partitions 2 --topic testkafka Created topic testkafka.
我们在kafka中会真对一个kafka服务创建数据,其他的kafka服务就会同步。下章会真对kafka的详细使用在做说明。
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏