文章目录
前言
提示:搭建集群kafka。
提示:以下是本篇文章正文内容,下面案例可供参考
一、kafka是什么?
示例:在我们大量使用分布式数据库、分布式计算集群的时候,是否会遇到这样的一些问题:
我们想分析下用户行为(pageviews),以便我们设计出更好的广告位
我想对用户的搜索关键词进行统计,分析出当前的流行趋势
有些数据,存储数据库浪费,直接存储硬盘效率又低
这些场景都有一个共同点:
数据是由上游模块产生,上游模块,使用上游模块的数据计算、统计、分析,这个时候就可以使用消息系统,尤其是分布式消息系统!
二、软件环境
2.1、 我的集群环境为三台centos7版本的机器
IP | 系统 |
---|---|
10.5.4.106 | centos7.6 |
10.5.4.43 | centos7.6 |
10.5.2.76 | centos7.6 |
2.2、已经搭建好了ookeeper集群
- 搭建zookeeper请参考:https://blog.csdn.net/Lovely_red_scarf/article/details/119615755
三、搭建集群
3.1下载kafka
- 我这里下载的kafka版本是2.21
- 下载地址 :http://kafka.apache.org/downloads
- 下载好解压并上传到你要安装的目录
- 我的安装目录为 /opt/server/
3.2、配置kafka
- 对每个集群下的安装目录下kafka的conf目录下的server.properties进行配置
代码如下(示例):
vim /opt/server/kafka/config/server.properties
- 每个节点的 conf下的server.properties都需要配置,配置都是不同的
##### 节点1
broker.id=1 # #当前机器在集群中的唯一标识,每一个节点的此配置内的borker.id不同
listeners=PLAINTEXT://10.5.4.106:9092 #当前节点的kafka对外提供服务的ip和端口9092,不同节点的此ip不同
advertised.listeners=PLAINTEXT://10.5.4.106:9092
log.dirs=/opt/data/kafka/ # kafka数据存储目录
message.max.byte=5242880
log.cleaner.enable=true
log.retention.hours=72
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
num.partitions=3
delete.topic.enable=true
replica.fetch.max.bytes=5242880
offsets.topic.replication.factor=3 # 因为集群是3个所以设置为3 默认是1 如果是单节点可以设置为1 或者不设置
transaction.state.log.replication.factor=3 # 因为集群是3个所以设置为3 默认是1 如果是单节点可以设置为1 或者不设置
transaction.state.log.min.isr=3 # 因为集群是3个所以设置为3 默认是1 如果是单节点可以设置为1 或者不设置
default.replication.factor=3 # 自动创建topic时的默认副本的个数
zookeeper.connect=10.5.4.106:2181,10.5.4.43:2181,10.5.2.76:2181 # 调用的zk集群
### 节点2,server.properties
broker.id=2 # broker.id 每个节点不同
listeners=PLAINTEXT://10.5.4.43:9092
advertised.listeners=PLAINTEXT://10.5.4.43:9092 # ip也是当前节点的ip
log.dirs=/opt/data/kafka/
message.max.byte=5242880
log.cleaner.enable=true
log.retention.hours=72
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
num.partitions=3
delete.topic.enable=true
replica.fetch.max.bytes=5242880
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
default.replication.factor=3
zookeeper.connect=10.5.4.106:2181,10.5.4.43:2181,10.5.2.76:2181
### 节点3 server.properties 配置
broker.id=3
listeners=PLAINTEXT://10.5.2.76:9092
advertised.listeners=PLAINTEXT://10.5.2.76:9092
log.dirs=/opt/data/kafka/
message.max.byte=5242880
log.cleaner.enable=true
log.retention.hours=72
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
num.partitions=3
delete.topic.enable=true
replica.fetch.max.bytes=5242880
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
default.replication.factor=3
zookeeper.connect=10.5.4.106:2181,10.5.4.43:2181,10.5.2.76:2181
- 配置解析
- 因为kafka集群有3个节点,所有需要改成3个,单节点就需要改为1
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
num.partitions=1
default.replication.factor=3
offsets.topic.replication.factor 用于配置offset记录的topic的partition的副本个数
transaction.state.log.replication.factor 事务主题的复制因子
transaction.state.log.min.isr 覆盖事务主题的min.insync.replicas配置
num.partitions 新建Topic时默认的分区数
default.replication.factor 自动创建topic时的默认副本的个数
注意:这些参数,设置得更高以确保高可用性!
其中 default.replication.factor 是真正决定,topi的副本数量的
- 关于kafka配置文件的更多解释,请参考链接:https://blog.csdn.net/memoordit/article/details/78850086
3.3 启动Kafka集群并测试
3.3.1 、启动服务
3.3.1.1 后台不打印日志启动
#从后台启动Kafka集群(3台都需要启动)
cd /opt/server/kafka/bin #进入到kafka的bin目录
./kafka-server-start.sh -daemon ../config/server.properties
3.3.1.2 :将启动日志打印到nohup中
nohup /opt/server/kafka/bin/kafka-server-start.sh /opt/server/kafka/config/server.properties &
3.3.2、检查服务是否启动
- 执行jsp
四、kafka配置分析
- 上面我在配置文件中指定了kafka的数据存储目录为/opt/data/kafka
- 我们看出这个目录下有一个文件为meta.properties
- 这个文件确实也会导致启动不起来,我们要检测节点的kafka数据存储目录是否有此文件,此文件是否配置正确
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了kafka的搭建。