kafka安装及配置

转载地址:https://blog.csdn.net/arpls20/article/details/80959260
https://www.cnblogs.com/justuntil/p/8033792.html(伪集群)
http://www.cnblogs.com/smartloli/p/4538173.html

1、下载kafka
官网下载地址:https://www.apache.org/dyn/closer.cgi?path=/kafka/1.1.0/kafka_2.12-1.1.0.tgz

2、解压

tar -xzf kafka_2.12-1.1.0.tgz /usr/local/kafka

Kafka目录介绍

  • /bin 操作kafka的可执行脚本,还包含windows下脚本
  • /config 配置文件所在目录
  • /libs 依赖库目录
  • /logs 日志数据目录,目录kafka把server端日志分为5种类型,分为:server,request,state,log-cleaner,controller

3、配置环境变量

vim /etc/profile
#set kafka
export KAFKA_HOME=/usr/local/kafka
export PATH=/usr/local/kafka/bin:$PATH

使用环境变量生效

source /etc/profile

说明:kafka集群中每个节点都需要配置

4、配置 server.properties
kafka最为重要三个配置依次为:broker.id、log.dir、zookeeper.connect。

cd /usr/local/kafka/config
vim server.properties

在192.168.17.64机器上配置server.properties。

# broker.id就是指各台服务器对应的id,所以各台服务器值不同
# 64机器的broker.id用1,64机器的broker.id用2,68机器的broker.id用3
broker.id=1 
# broker server服务器端口号,无需改变
port=9092
# 当前服务器的IP,各台服务器值不同
host.name=192.168.17.64
# Zookeeper集群的ip和端口号
# 默认使用的2181端口,可在zookeeper配置文件conf/zoo.cfg中修改。
zookeeper.connect=192.168.17.64:2181,192.168.17.65:2181,192.168.17.68:2181 # 可以将ip换位主机名
# 日志目录,在指定的位置创建好文件夹logs
log.dirs=log.dirs=/data1/platform/kafka/logs
# 非本地生产者和消费者访问Kafka,记得修改 config/server.properties中的listeners
listeners=PLAINTEXT://192.168.17.134:9092 

5、把192.168.17.64配置好的kafka拷贝到192.168.17.65和192.168.17.68上

[root@localhost local]# scp -rp kafka [email protected]:/usr/local/
[root@localhost local]# scp -rp kafka [email protected]:/usr/local/

6、在65和68机器上创建log目录,修改broker.id,host.name

扫描二维码关注公众号,回复: 2234610 查看本文章
# 65机器
mkdir /data1/platform/kafka/
mkdir /data1/platform/kafka/logs
# 修改65机器server.properties的broker_id
broker_id=2
host.name=192.168.17.65

# 68机器
mkdir /data1/platform/kafka/
mkdir /data1/platform/kafka/logs
# 修改68机器server.properties的broker_id
broker_id=3
host.name=192.168.17.68

7、启动Kafka
(1)启动
启动kafka之前需启动zookeeper,然后启动kafka,各个节点需单独启动。
进入kafka目录,敲入命令

 nohup bin/kafka-server-start.sh config/server.properties >log 2>&1 &

(2)检测2181与9092端口

[root@nimbus kafka]# netstat -tunlp|egrep "(2181|9092)"
tcp6       0      0 192.168.17.64:9092      :::*                    LISTEN      40748/java          
tcp6       0      0 :::2181                 :::*                    LISTEN      37853/java       

Kafka的进程ID为40748,占用端口为9092
QuorumPeerMain为对应的zookeeper实例,进程ID为37853,在2181端口监听

使用jps命令查看kafka是否运行:

[root@nimbus kafka]# jps
46167 Jps
40748 Kafka
37853 QuorumPeerMain

8、创建topic
使用kafka-topics.sh 创建单分区单副本的topic test:

[root@nimbus kafka]# bin/kafka-topics.sh --create --zookeeper 192.168.17.64:2181 --replication-factor 1 --partitions 1 --topic test  
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Created topic "test". 

查看创建的topic test:

[root@nimbus kafka]# bin/kafka-topics.sh --list --zookeeper localhost:2181
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
test

其中,localhost为zookeeper地址

topic描述:

[root@nimbus kafka]# bin/kafka-topics.sh --describe --zookeeper 192.168.17.68:2181 --topic test 
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Topic:test      PartitionCount:1        ReplicationFactor:1     Configs:
        Topic: test     Partition: 0    Leader: 2       Replicas: 2     Isr: 2

下面解释一下这些输出。第一行是对所有分区的一个描述,然后每个分区都会对应一行,因为我们只有一个分区所以下面就只加了一行。
- Leader:负责处理消息的读和写,Leader是从所有节点中随机选择的。
- Replicas:列出了所有的副本节点,不管节点是否在服务中。
- Isr:是正在服务中的节点
-
9、产生消息命令
使用kafka-console-producer.sh 命令向topic test 发送消息

bin/kafka-console-producer.sh --broker-list 192.168.17.64:9092 --topic test

10、接收消息

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

猜你喜欢

转载自blog.csdn.net/learn_tech/article/details/80924529