kafka集群搭建

环境:

三台机器vm1,vm2,vm3

zookeeper版本:apache-zookeeper-3.5.5

kafka版本:kafka_2.11-2.3.0 (2.11是scala版本,2.3.0是kafka版本)

安装zookeeper集群

前往下载

解压拷贝三份到目标机器相同目录下,

配置,

mv zoo_sample.cfg zoo.cfg

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=/var/lib/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
server.1=192.168.1.231:2888:3888
server.2=192.168.1.232:2888:3888
server.3=192.168.1.233:2888:3888

除了配置server信息和dataDir,其它默认配置;如果配置了hosts,可以不用输入ip,直接使用主机名也可以。

接着,在dataDir目录下,即/var/lib/zookeeper下创建名为mypid的文件,其中输入序号即可,三台机器都操作一样,这里vm1机器输入1,vm2机器输入2,vm3机器输入3;

比如vm1,

启动与校验

三台机器做如下同样的操作,

启动,

[xs@vm1 bin]$ ./zkServer.sh start

验证,

[xs@vm1 bin]$ ./zkServer.sh status

 见到类似上面的输出,证明启动成功;

可能遇到的问题

1、三台机器确认dataDir存在且有权限,可执行下面命令,这里用户是xs,

sudo chown -R xs:xs /var/lib/zookeeper

2、如果下载使用的包是类似下面第二个较小的包,需要把第一个包下载下来解压后将其lib目录导入,否则启动时可能会报类似包找不到的错误。


安装kafka集群

前往下载所需版本kafka 。

将包解压到三台机器的相同目录下,解压包的config目录下server.properties文件中修改如下参数,

broker.id=1  #vm1这里设置1,vm2这里设置2,vm3这里设置3

zookeeper.connect=vm1:2181,vm2:2181,vm3:2181 #配置zookeeper机器信息

进入bin目录启动,

nohup ./kafka-server-start.sh ../config/server.properties &

创建topic

./kafka-topics.sh --create --zookeeper vm1:2181,vm2:2181,vm3:2181 --replication-factor 1 --partitions 1 --topic test

列出topic

./kafka-topics.sh --list --bootstrap-server localhost:9092

注意上面,使用了--bootstrap-server,它与--zookeeper有相同的效果;

发送消息

 ./kafka-console-producer.sh --broker-list vm1:9092,vm2:9092,vm3:9092 --topic test

接收消息

./kafka-console-consumer.sh --bootstrap-server  vm2:9092 --topic test  --from-beginning

tips : 具体sh文件命令参数不懂,可以--help查看帮助信息。

参考:

https://www.cnblogs.com/cjsblog/p/9409443.html

http://kafka.apache.org/quickstart

原文地址:https://www.cnblogs.com/mylittlecabin/p/11493265.html