千家信息网

Kafka安装部署

发表于:2025-01-25 作者:千家信息网编辑
千家信息网最后更新 2025年01月25日,1下载并安装JDK参考jdk的安装,需要jdk1.6+。2 Kafka的安装2.1 下载kafka下载地址:http://mirrors.hust.edu.cn/apache/kafka/0.10.0
千家信息网最后更新 2025年01月25日Kafka安装部署

1下载并安装JDK

参考jdk的安装,需要jdk1.6+

2 Kafka的安装

2.1 下载kafka

下载地址:http://mirrors.hust.edu.cn/apache/kafka/0.10.0.1/kafka_2.11-0.10.0.1.tgz

2.2 解压安装包

$ tar zvxf kafka_2.11-0.10.0.1.tgz

2.3 复制解压后的文件夹到安装目录

$ cp -r kafka_2.11-0.10.0.1 /software/

2.4 创建kafka安装目录的软链接

$ ln -s /software/kafka_2.11-0.10.0.1/software/kafka

2.5 配置环节变量

修改/etc/profile文件内容:

export KAFKA_HOME=/software/kafka

export PATH=$KAFKA_HOME/bin:$FLUME_HOME/bin:$ZOOKEEPER_HOME/bin:$HBASE_HOME/bin:

$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH

export CLASSPATH=.:$KAFKA_HOME/libs/*:$FLUME_HOME/lib/*:$HBASE_HOME/lib/*:$JAVA_

HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

2.6 启动kafka

1)、启动kafka前要先启动zookeeper

$ zookeeper-server-start.sh /software/kafka/config/zookeeper.properties &

2)、启动kafka

$ kafka-server-start.sh /software/kafka/config/server.properties &

3 体验单节点Kafka

3.1 创建topic

$ kafka-topics.sh --create --zookeeperlocalhost:2181 --replication-factor 1 --partitions 1 --topic test

查询创建的topic

$ kafka-topics.sh --list --zookeeperlocalhost:2181

3.2 发送消息

$ kafka-console-producer.sh --broker-listlocalhost:9092 --topic test

This is a message

This is another message

3.3 消费消息

$ kafka-console-consumer.sh --bootstrap-serverlocalhost:9092 --zookeeper localhost:2181 --topic test --from-beginning

This is a message

This is another message

4 搭建多brokerKafka集群

4.1 复制配置文件

$ cd /software/kafka/config/

$ cp server.properties s1.properties

$ cp server.properties s2.properties

$ cp server.properties s3.properties

4.2 修改配置文件

config/s1.properties:

broker.id=1

listeners=PLAINTEXT://:9093

log.dir=/tmp/kafka-logs-1

config/s2.properties:

broker.id=2

listeners=PLAINTEXT://:9094

log.dir=/tmp/kafka-logs-2

config/s3.properties:

broker.id=3

listeners=PLAINTEXT://:9095

log.dir=/tmp/kafka-logs-3

4.3 启动kafka

$ kafka-server-start.sh /software/kafka/config/s1.properties &

$ kafka-server-start.sh /software/kafka/config/s2.properties &

$ kafka-server-start.sh /software/kafka/config/s3.properties&

4.4 创建topic

$ kafka-topics.sh --create--zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic

查看topic描述

$ kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic

4.5 发送消息

$ kafka-console-producer.sh --broker-listlocalhost:9093 --topic my-replicated-topic

my test msg 1

my test msg 2

my test msg 3

4.6 消费消息

$ kafka-console-consumer.sh--zookeeper localhost:2181 --from-beginning --topic my-replicated-topic

5 容错测试

5.1 找到s1进程

$ ps -ef| grep s1.properties

5.2 杀死进程s1

kill pid

5.3 查看主题描述

$>kafka-topics.sh --describe --zookeeperlocalhost:2181 --topic my-replicated-topic

Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:

Topic: my-replicated-topic Partition: 0 Leader: 2 Replicas:1,2,0 Isr: 2,0

5.4 启动消费者消费主题消息

$>kafka-console-consumer.sh--zookeeper localhost:2181 --from-beginning --topic my-replicated-topic

        my test message 1
        my test message 2

6 完全分布式搭建

6.1 kafka安装文件夹分发到其余集群服务器

$ xsync kafka_2.11-0.10.0.1/

$ xsync kafka

6.2 分发环境变量

#xsync /etc/profile

6.3 修改server.properties属性文件并分发

broker.id=203 #修改brokerid

log.dirs=/home/hadoop/kafka/logs #修改日志的存放路径

zookeeper.connect=s201:2181,s202:2181,s203:2181 #修改zookeeper的连接路径

6.4 启动Kafka集群

到部署kafka每台服务器上面启动kafka集群

$ kafka-server-start.sh /software/kafka/config/server.properties &

$ kafka-server-start.sh /software/kafka/config/server.properties &

$ kafka-server-start.sh /software/kafka/config/server.properties &


0