欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux下搭建zookeeper+kafka集群

程序员文章站 2022-05-27 16:45:54
...

linux版本:centos7.2

链接:https://pan.baidu.com/s/1jjHoz_GQNe9pwTT40ZWryg
提取码:4mtp

kafka版本:kafka_2.12-2.7.0.tgz

链接:https://pan.baidu.com/s/1b5yOLpyPh5rQy9hEGDq2Ag
提取码:ciai

zookeeper环境:

https://blog.csdn.net/weixin_43914685/article/details/113762882

1.解压安装包

[[email protected] local]# tar -zxvf kafka_2.12-2.7.0.tgz 

2.创建软连接

[[email protected] local]# ln -s kafka_2.12-2.7.0 kafka0
[[email protected] local]# ls
apache-tomcat-8.5.63         bin  games    kafka0            kafka_2.12-2.7.0.tgz  lib64    lz-server  share     src
apache-tomcat-8.5.63.tar.gz  etc  include  kafka_2.12-2.7.0  lib                   libexec  sbin       software

3.修改kafka配置文件

[[email protected] local]# vim kafka0/config/server.properties 

3台机器分别修改id为0 1 2,listeners修改为自己的ip地址

broker.id=0

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://192.168.1.108:9092

4.日志生成目录

log.dirs=/tmp/kafka-logs

5.设置Zookeeper集群地址

zookeeper.connect=192.168.1.108:2181,192.168.1.109:2181,192.168.1.110:2181

6.Topic的默认Partition数量

num.partitions 为新建Topic的默认Partition数量,partition数量提升,一定程度上可以提升并发性

num.partitions=3

offsets和transaction
两个topic,分组元数据的复制因子,为了保证可用性,在生产上建议设置大于1。

default.replication.factor为kafka保存消息的副本数,如果一个副本失效了,另一个还可以继续提供服务,是在自动创建topic时的默认副本数,可以设置为3

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3

7.启动kafka

[[email protected] kafka0]# bin/kafka-server-start.sh -daemon config/server.properties

8.查看日志

进入kafka的log目录,结尾输出日志started 启动成功

[[email protected] logs]# cd logs/
[[email protected] logs]# cat kafkaServer.out 
[2021-02-13 06:48:06,563] INFO [KafkaServer id=2] started (kafka.server.KafkaServer)
[2021-02-13 06:48:09,168] INFO [broker-2-to-controller-send-thread]: Recorded new controller, from now on will use broker 2 (kafka.server.BrokerToControllerRequestThread)

9.查看进程

[[email protected] logs]# jps
2768 Jps
2248 QuorumPeerMain
2682 Kafka

10.停止kafka命令

[[email protected] logs]# bin/kafka-server-stop.sh config/server.properties

11.配置环境变量

[[email protected] ~]# vim /etc/profile
[[email protected] ~]# source /etc/profile
[[email protected] ~]# echo $KAFKA_HOME

12.kafka和Zookeeper已启动完成

  1. 创建topic
[[email protected] ~]# kafka-topics.sh --create --zookeeper 192.168.1.108:2181 --replication-factor 3 --partitions 3 --topic kafka0
Created topic kafka0.
  1. 查看主题
[[email protected] ~]# kafka-topics.sh --list --zookeeper 192.168.1.108:2181
__consumer_offsets
kafka0
  1. 发送消息

通过192.168.1.109给192.168.1.108发送消息

[[email protected] kafka1]# bin/kafka-console-producer.sh --broker-list 192.168.1.108:9092 --topic test
  1. 接收消息

192.168.1.108主机接收消息

[[email protected] ~]# kafka-console-consumer.sh --bootstrap-server 192.168.1.108:9092 --topic kafka0 --from-beginning
hello
  1. 查看主题
[[email protected] ~]# kafka-topics.sh --zookeeper localhost:2181 --describe  --topic kafka0
Topic: kafka0   PartitionCount: 3       ReplicationFactor: 3    Configs: 
        Topic: kafka0   Partition: 0    Leader: 1       Replicas: 1,2,0 Isr: 1,2,0
        Topic: kafka0   Partition: 1    Leader: 2       Replicas: 2,0,1 Isr: 2,0,1
        Topic: kafka0   Partition: 2    Leader: 0       Replicas: 0,1,2 Isr: 0,1,2

从中可以看到,test主题分了三个区,复制因子是3。

  1. 删除主题
[[email protected] ~]# kafka-topics.sh --zookeeper localhost:2181 --delete  --topic test
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.