准备工作:
1.准备好Kafka的压缩包,这里选用的是kafka_2.10-0.8.2.2.tgz。
2.准备两台CentOS 6.5位虚拟机。分别是192.168.1.163(主机名为 0)、192.168.1.174(主机名为 1)。
3.安装好JDK1.7
一、zookeeper集群(192.168.1.163,192.168.1.174两台服务器配置一样,除了myid文件内容不一样)
1.tar -xzf kafka_2.10-0.8.2.0.tgz
2.cd kafka_2.10-0.8.2.2
3.修改zookeeper配置文件kafka_2.10-0.8.2.0/config/zookeeper.properties
syncLimit=2
server.0=192.168.1.163:2888:3888
server.1=192.168.1.174:2889:3889
4.启动zookeeper服务
/zookeeper-server-start.sh config/zookeeper.properties &
5.在目录/data/zookeeper/下,创建一个文件myid,文件内容为主机名:
即192.168.1.163服务器的myid文件内容为:0
即192.168.1.174服务器的myid文件内容为:1
二、kafka集群(两台服务器同样配置,但参数broker.id、host.name不一样,需要注意)
1.修改kafka配置文件kafka_2.10-0.8.2.0/config/server.properties
broker.id=1 #改为当前主机名,192.168.1.163(主机名为 0)、192.168.1.174(主机名为 1)。
host.name=192.168.1.163 #改为当前kafa服务器的IP地址192.168.1.163或192.168.1.174
zookeeper.connect=192.168.1.163:2181,192.168.1.174:2181 #两台zookeeper服务器的地址
2.启动kafka的脚本
bin/kafka-server-start.sh config/server.properties &
3.生产者配置文件修改
# list of brokers used for bootstrapping knowledge about the rest of the cluster
# format: host1:port1,host2:port2 ...
metadata.broker.list=192.168.1.163:9092,192.168.1.174:9092
# name of the partitioner class for partitioning events; default partition spreads data randomly
#partitioner.class=
# specifies whether the messages are sent asynchronously (async) or synchronously (sync)
producer.type=async
4.消费者配置文件修改
# Zookeeper connection string
# comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
zookeeper.connect=192.168.1.163:2181,192.168.1.174:2181
# timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=60000
三、测试集群是否成功
1、创建主题
./kafka-topics.sh --create --replication-factor 1 --partitions 1 --topic testtopic --zookeeper 192.168.1.163:2181,192.168.1.174:2181
2、启动两台机器的消费者
(192.168.1.163服务器上启动脚本)./kafka-console-consumer.sh --zookeeper 192.168.1.163:2181 --from-beginning --topic testtopic
(192.168.1.174服务器上启动脚本)./kafka-console-consumer.sh --zookeeper 192.168.1.174:2181 --from-beginning --topic testtopic
3、启动一个生产者
(192.168.1.163服务器上启动脚本)./kafka-console-producer.sh --broker-list 192.168.1.163:9092 --topic testtopic
然后在生产者中输入信息,两个消费者都会有消息,则kafka集群成功。
1.准备好Kafka的压缩包,这里选用的是kafka_2.10-0.8.2.2.tgz。
2.准备两台CentOS 6.5位虚拟机。分别是192.168.1.163(主机名为 0)、192.168.1.174(主机名为 1)。
3.安装好JDK1.7
一、zookeeper集群(192.168.1.163,192.168.1.174两台服务器配置一样,除了myid文件内容不一样)
1.tar -xzf kafka_2.10-0.8.2.0.tgz
2.cd kafka_2.10-0.8.2.2
3.修改zookeeper配置文件kafka_2.10-0.8.2.0/config/zookeeper.properties
dataDir=/tmp/zookeeper
clientPort=2181
initLimit=5syncLimit=2
server.0=192.168.1.163:2888:3888
server.1=192.168.1.174:2889:3889
4.启动zookeeper服务
/zookeeper-server-start.sh config/zookeeper.properties &
5.在目录/data/zookeeper/下,创建一个文件myid,文件内容为主机名:
即192.168.1.163服务器的myid文件内容为:0
即192.168.1.174服务器的myid文件内容为:1
二、kafka集群(两台服务器同样配置,但参数broker.id、host.name不一样,需要注意)
1.修改kafka配置文件kafka_2.10-0.8.2.0/config/server.properties
broker.id=1 #改为当前主机名,192.168.1.163(主机名为 0)、192.168.1.174(主机名为 1)。
host.name=192.168.1.163 #改为当前kafa服务器的IP地址192.168.1.163或192.168.1.174
zookeeper.connect=192.168.1.163:2181,192.168.1.174:2181 #两台zookeeper服务器的地址
2.启动kafka的脚本
bin/kafka-server-start.sh config/server.properties &
3.生产者配置文件修改
# list of brokers used for bootstrapping knowledge about the rest of the cluster
# format: host1:port1,host2:port2 ...
metadata.broker.list=192.168.1.163:9092,192.168.1.174:9092
# name of the partitioner class for partitioning events; default partition spreads data randomly
#partitioner.class=
# specifies whether the messages are sent asynchronously (async) or synchronously (sync)
producer.type=async
4.消费者配置文件修改
# Zookeeper connection string
# comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
zookeeper.connect=192.168.1.163:2181,192.168.1.174:2181
# timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=60000
三、测试集群是否成功
1、创建主题
./kafka-topics.sh --create --replication-factor 1 --partitions 1 --topic testtopic --zookeeper 192.168.1.163:2181,192.168.1.174:2181
2、启动两台机器的消费者
(192.168.1.163服务器上启动脚本)./kafka-console-consumer.sh --zookeeper 192.168.1.163:2181 --from-beginning --topic testtopic
(192.168.1.174服务器上启动脚本)./kafka-console-consumer.sh --zookeeper 192.168.1.174:2181 --from-beginning --topic testtopic
3、启动一个生产者
(192.168.1.163服务器上启动脚本)./kafka-console-producer.sh --broker-list 192.168.1.163:9092 --topic testtopic
然后在生产者中输入信息,两个消费者都会有消息,则kafka集群成功。