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

Kafka集群配置

程序员文章站 2023-01-13 11:47:41
kafka_2.11-0.9.0.1.tgz 1.进入项目前的目录 cd /home/dongshanxia mkdir kafka #创建项目目录 cd kafka #进入项目目录 mkdir kafkalogs #创建kafka消息目录,主要存放kafka消息 2.进入配置文件目录 cd /ho ......

kafka_2.11-0.9.0.1.tgz

1.进入项目前的目录

cd /home/dongshanxia

mkdir kafka #创建项目目录

cd kafka #进入项目目录

mkdir kafkalogs #创建kafka消息目录,主要存放kafka消息

2.进入配置文件目录

cd /home/dongshanxia/kafka/kafka_2.11-0.9.0.1/config

//打开配置文件

vim server.properties

------------------------配置文件-------------------------------------

broker.id=1

/* 这是这台虚拟机上的值,在另外两台虚拟机上应该是2或者3,这个值是唯一的,每台虚拟机或者叫服务器不能相同。 */

listeners=plaintext://cjmaster01:9092

/设置本机ip和端口。我这里设置的是listeners,

也可以直接设置host.name=cjmaster01,port=9092,

这个ip地址也是与本机相关的,每台服务器上设置为自己的ip地址。/

 

log.dirs=/home/dongshanxia/kafka/kafkalogs

 

#指定其与另外几台一样的ip

zookeeper.connect=cjmaster01:2181,cjdatanode01:2181,cjdatanode02:2181

delete.topic.enable=true

~~~~~别人的配置文件

broker.id=0 #当前机器在集群中的唯一标识,和zookeeper的myid性质一样

port=19092 #当前kafka对外提供服务的端口默认是9092

host.name=192.168.7.100 #这个参数默认是关闭的,在0.8.1有个bug,dns解析问题,失败率的问题。

num.network.threads=3 #这个是borker进行网络处理的线程数

num.io.threads=8 #这个是borker进行i/o处理的线程数

log.dirs=/opt/kafka/kafkalogs/ #消息存放的目录,这个目录可以配置为","逗号分割的表达式,上面的num.io.threads要大于这个目录的个数这个目录,如果配置多个目录,新创建的topic他把消息持久化的地方是,当前以逗号分割的目录中,那个分区数最少就放那一个

socket.send.buffer.bytes=102400 #发送缓冲区buffer大小,数据不是一下子就发送的,先回存储到缓冲区了到达一定的大小后在发送,能提高性能

socket.receive.buffer.bytes=102400 #kafka接收缓冲区大小,当数据到达一定大小后在序列化到磁盘

socket.request.max.bytes=104857600 #这个参数是向kafka请求消息或者向kafka发送消息的请请求的最大数,这个值不能超过java的堆栈大小

num.partitions=1 #默认的分区数,一个topic默认1个分区数

log.retention.hours=168 #默认消息的最大持久化时间,168小时,7天

message.max.byte=5242880 #消息保存的最大值5m

default.replication.factor=2 #kafka保存消息的副本数,如果一个副本失效了,另一个还可以继续提供服务

replica.fetch.max.bytes=5242880 #取消息的最大直接数

log.segment.bytes=1073741824 #这个参数是:因为kafka的消息是以追加的形式落地到文件,当超过这个值的时候,kafka会新起一个文件

log.retention.check.interval.ms=300000 #每隔300000毫秒去检查上面配置的log失效时间(log.retention.hours=168 ),到目录查看是否有过期的消息如果有,删除

log.cleaner.enable=false #是否启用log压缩,一般不用启用,启用的话可以提高性能

zookeeper.connect=192.168.7.100:12181,192.168.7.101:12181,192.168.7.107:1218 #设置zookeeper的连接端口

 

命令集合

启动kafka

bin/kafka-server-start.sh config/server.properties  >/dev/null 2>&1 &

 

//守护模式-daemon

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-server-start.sh -daemon /home/dongshanxia/kafka/kafka_2.11-0.9.0.1/config/server.properties

 

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-server-start.sh /home/dongshanxia/kafka/kafka_2.11-0.9.0.1/config/server.properties  >/dev/null 2>&1 &

 

创建topic

创建一个叫做"test"的topic,它只有一个分区,一个副本

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

例如:

bin/kafka-topics.sh --zookeeper cjmaster01:2181  --create --topic mac-topic --replication-factor 2 --partitions 6

 

 ./bin/kafka-topics.sh  --delete --zookeeper cjmaster01:2181 --topic 【topic name】

 

 

修改topic分区数量

bin/kafka-topics.sh --zookeeper localhost:2182 --alter --partitions 20 --topic test

 

查看topic

可以通过list命令查看创建的topic:

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-topics.sh --list --zookeeper localhost:2181  test

 

查看topic的分区情况

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-topics.sh  --describe --zookeeper localhost:2181 test

 

 

 

消费数据

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-console-consumer.sh --zookeeper 10.168.1.162:2181 --from-beginning --topic  topic_hito_udp_gsm

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic  topic_hito_tcp_mac

 

./kafka-console-consumer.sh --zookeeper localhost:2181  --topic topic_hik_udp_mac

 

 

//测试使用

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

 

/home/dongshanxia/kafka/kafka_2.11-0.9.0.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181  --topic test

 

 

 

修改topic的备份数量

//校验是否正确

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --reassignment-json-file ./topic-test.json  --verify

 

输出信息:修改之前报这个信息,说备份数量和文本不一致(因为文本内容是要添加副本数,肯定不一致),证明文本没有问题,可以执行

 

[root@cjmaster01 kafka_2.11-0.9.0.1]# bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --reassignment-json-file ./topic-test.json  --verify

status of partition reassignment:

error: assigned replicas (0) don't match the list of replicas for reassignment (0,1) for partition [topic-test,1]

error: assigned replicas (2) don't match the list of replicas for reassignment (1,2) for partition [topic-test,3]

error: assigned replicas (2) don't match the list of replicas for reassignment (2,1) for partition [topic-test,0]

error: assigned replicas (1) don't match the list of replicas for reassignment (1,2) for partition [topic-test,2]

error: assigned replicas (1) don't match the list of replicas for reassignment (1,0) for partition [topic-test,5]

error: assigned replicas (0) don't match the list of replicas for reassignment (1,0) for partition [topic-test,4]

reassignment of partition [topic-test,1] failed

reassignment of partition [topic-test,3] failed

reassignment of partition [topic-test,0] failed

reassignment of partition [topic-test,2] failed

reassignment of partition [topic-test,5] failed

reassignment of partition [topic-test,4] failed

 

 

 

//上边返回成功了再执行

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181  --reassignment-json-file ./topic-test.json  --execute