日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Kafka学习:CentOS7下Kafka集群搭建

發(fā)布時(shí)間:2025/3/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kafka学习:CentOS7下Kafka集群搭建 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 準(zhǔn)備
  • 集群安裝
    • 1、創(chuàng)建目錄
    • 2、解壓縮安裝包
    • 3、修改配置文件
    • 4、啟動(dòng)
    • 5、查看集群是否安裝成功
  • 測(cè)試Kafka
    • 1、創(chuàng)建測(cè)試mytopic
    • 2、查看mytopic副本信息
    • 3、查看已創(chuàng)建topic列表信息
    • 4、創(chuàng)建Producer
    • 5、創(chuàng)建Consumer
    • 6、刪除mytopic
    • 7、停止kafka

準(zhǔn)備

1、首先安裝zookeeper作為為集群提供高可用
2、準(zhǔn)備kafka按裝包kafka_2.11-2.1.1.tgz
3、準(zhǔn)備三臺(tái)機(jī)器hadoop-slave1、hadoop-slave2、hadoop-slave3

集群安裝

1、創(chuàng)建目錄

創(chuàng)建kafka和kafkalogs目錄,并將壓縮包存放到/opt/kafka目錄下,創(chuàng)建后的全路徑如下所示:

/opt/kafka /opt/kafka/kafkalogs

2、解壓縮安裝包

tar -zxvf kafka_2.11-2.1.1.tgz

3、修改配置文件

首先打開配置文件位置

cd /opt/kafka/kafka_2.11-2.1.1/config/

這里主要關(guān)注server.properties文件,這里可以通過zookeeper.properties配置Kafka內(nèi)帶的zk集群來啟動(dòng),但是建議使用獨(dú)立的zk集群

下面這個(gè)是hadoop-slave上的完整配置:

[root@hadoop-slave1 config]# cat server.properties # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.# see kafka.server.KafkaConfig for additional details and defaults############################# Server Basics ############################## The id of the broker. This must be set to a unique integer for each broker. 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://:9092# Hostname and port the broker will advertise to producers and consumers. If not set, # it uses the value for "listeners" if configured. Otherwise, it will use the value # returned from java.net.InetAddress.getCanonicalHostName(). #advertised.listeners=PLAINTEXT://your.host.name:9092# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL# The number of threads that the server uses for receiving requests from the network and sending responses to the network num.network.threads=3# The number of threads that the server uses for processing requests, which may include disk I/O num.io.threads=8# The send buffer (SO_SNDBUF) used by the socket server socket.send.buffer.bytes=102400# The receive buffer (SO_RCVBUF) used by the socket server socket.receive.buffer.bytes=102400# The maximum size of a request that the socket server will accept (protection against OOM) socket.request.max.bytes=104857600############################# Log Basics ############################## A comma separated list of directories under which to store log files log.dirs=/opt/kafka/kafkalogs/# The default number of log partitions per topic. More partitions allow greater # parallelism for consumption, but this will also result in more files across # the brokers. num.partitions=1# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown. # This value is recommended to be increased for installations with data dirs located in RAID array. num.recovery.threads.per.data.dir=1############################# 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 for to ensure availability such as 3. offsets.topic.replication.factor=1 transaction.state.log.replication.factor=1 transaction.state.log.min.isr=1############################# Log Flush Policy ############################## Messages are immediately written to the filesystem but by default we only fsync() to sync # the OS cache lazily. The following configurations control the flush of data to disk. # There are a few important trade-offs here: # 1. Durability: Unflushed data may be lost if you are not using replication. # 2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush. # 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks. # The settings below allow one to configure the flush policy to flush data after a period of time or # every N messages (or both). This can be done globally and overridden on a per-topic basis.# The number of messages to accept before forcing a flush of data to disk #log.flush.interval.messages=10000# The maximum amount of time a message can sit in a log before we force a flush #log.flush.interval.ms=1000############################# Log Retention Policy ############################## The following configurations control the disposal of log segments. The policy can # be set to delete segments after a period of time, or after a given size has accumulated. # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens # from the end of the log.# The minimum age of a log file to be eligible for deletion due to age# 默認(rèn)消息的最大持久化時(shí)間,168小時(shí),7天 log.retention.hours=168 # 消息保存的最大值5M message.max.byte=5242880 # kafka保存消息的副本數(shù),如果一個(gè)副本失效了,另一個(gè)還可以繼續(xù)提供服務(wù) default.replication.factor=2 # 取消息的最大直接數(shù) replica.fetch.max.bytes=5242880# A size-based retention policy for logs. Segments are pruned from the log unless the remaining # segments drop below log.retention.bytes. Functions independently of log.retention.hours. log.retention.bytes=1073741824# The maximum size of a log segment file. When this size is reached a new log segment will be created. log.segment.bytes=1073741824# The interval at which log segments are checked to see if they can be deleted according # to the retention policies log.retention.check.interval.ms=300000#log.cleaner.enable=false #是否啟用log壓縮,一般不啟用,啟用的話可以提高性能 ############################# Zookeeper ############################## Zookeeper connection string (see zookeeper docs for details). # This is a comma separated host:port pairs, each corresponding to a zk # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002". # You can also append an optional chroot string to the urls to specify the # root directory for all kafka znodes. zookeeper.connect=hadoop-slave1:12181,hadoop-slave2:12181,hadoop-slave3:12181# Timeout in ms for connecting to zookeeper zookeeper.connection.timeout.ms=6000############################# Group Coordinator Settings ############################## The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance. # The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms. # The default value for this is 3 seconds. # We override this to 0 here as it makes for a better out-of-the-box experience for development and testing. # However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup. group.initial.rebalance.delay.ms=0

hadoop-slave2和hadoop-slave3配置基本和上邊一致,就是標(biāo)紅的地方需要做修改成相應(yīng)的值

4、啟動(dòng)

進(jìn)入3臺(tái)機(jī)器的bin目錄

cd /opt/kafka/kafka_2.11-2.1.1/bin

每一臺(tái)機(jī)器都執(zhí)行啟動(dòng)命令

./kafka-server-start.sh -daemon ../config/server.properties

每一臺(tái)機(jī)器都檢查是否已啟動(dòng)

5、查看集群是否安裝成功

客戶端連接zookeeper

/opt/zookeeper/zookeeper-3.4.12/bin/zkCli.sh -server hadoop-slave1:12181

檢查kafka集群?jiǎn)?dòng)個(gè)數(shù)

ls /brokers/ids

測(cè)試Kafka

1、創(chuàng)建測(cè)試mytopic

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-topics.sh --create --zookeeper hadoop-slave1:12181,hadoop-slave2:12181,hadoop-slave3:12181 --replication-factor 3 --partitions 3 --topic mytopic

選項(xiàng)說明:

–topic 定義topic名

–replication-factor 定義副本數(shù)

–partitions 定義分區(qū)數(shù)

2、查看mytopic副本信息

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-topics.sh --describe --zookeeper hadoop-slave1:12181,hadoop-slave2:12181,hadoop-slave3:12181 --topic mytopic

3、查看已創(chuàng)建topic列表信息

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-topics.sh --list --zookeeper hadoop-slave1:12181,hadoop-slave2:12181,hadoop-slave3:12181

4、創(chuàng)建Producer

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-console-producer.sh --broker-list hadoop-slave1:9092,hadoop-slave2:9092,hadoop-slave3:9092 --topic mytopic


基于mytopic并發(fā)送消息"hello kafka 20201228"

5、創(chuàng)建Consumer

注意:--bootstrap-server后面跟的是borker的地址

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-console-consumer.sh --bootstrap-server hadoop-slave1:9092,hadoop-slave2:9092,hadoop-slave3:9092 --from-beginning --topic mytopic

–from-beginning:會(huì)把TestTopic主題中以往所有的數(shù)據(jù)都讀取出來。根據(jù)業(yè)務(wù)場(chǎng)景選擇是否增加該配置。

注意:消費(fèi)者的命令跟版本有關(guān)系,本文使用的新版本命令基于kafka_2.11-2.1.1

下面是舊版本的啟動(dòng)命令,注意后面跟的是zookeeper集群的地址

./bin/kafka-console-consumer.sh --zookeeper hadoop-slave1:12181,hadoop-slave2:12181,hadoop-slave3:12181 --from-beginning --topic mytopic

6、刪除mytopic

7、停止kafka

/opt/kafka/kafka_2.11-2.1.1/bin/kafka-server-stop.sh

總結(jié)

以上是生活随笔為你收集整理的Kafka学习:CentOS7下Kafka集群搭建的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。