CentOS中配置Kafka集羣

環境:三臺虛擬機Host0,Host1,Host2html

Host0:192.168.10.2java

Host1:  192.168.10.3node

Host2:  192.168.10.4express

在三臺虛擬機上配置zookeeper,具體配置詳見CentOS中配置CDH版本的ZooKeeperapache

下載kafka:http://kafka.apache.org/downloads.html併發

個人kafka版本是kafka_2.10-0.8.2.0app

在各個kafka節點上解壓kafka&進入kafka目錄less

[root@Host0 ~]# tar xfvz kafka_2.10-0.8.2.0.tgz 
[root@Host0 ~]# cd kafka_2.10-0.8.2.0

在各個kafka節點上配置config/server.propertieswen文件socket

# 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 port the socket server listens on
port=9092

# Hostname the broker will bind to. If not set, the server will bind to all interfaces
host.name=211.68.36.127

# Hostname the broker will advertise to producers and consumers. If not set, it uses the
# value for "host.name" if configured.  Otherwise, it will use the value returned from
# java.net.InetAddress.getCanonicalHostName().
advertised.host.name=211.68.36.127

# The port to publish to ZooKeeper for clients to use. If this is not set,
# it will publish the same port that the broker binds to.
#advertised.port=<port accessible by clients>

# The number of threads handling network requests
num.network.threads=3
 
# The number of threads doing 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 seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs

# 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

############################# 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 exceessive 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
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes.
#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

# By default the log cleaner is disabled and the log retention policy will default to just delete segments after their retention expires.
# If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction.
log.cleaner.enable=false

############################# 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=Host0:2181,Host1:2181,Host2:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=9000

delete.topic.enable = true

注意:post

broker.id=0   broker的id,每一個kafka節點配置不能同樣,能夠0,1,2等
host.name=192.168.10.2  broker的hostname;若是hostname已經設置的話,broker將只會綁定到這個地址上;若是沒有設置,它將綁定到全部接口,併發布一份到ZK。每臺節點設置成當前節點的IP地址
advertised.host.name=192.168.10.2  做爲broker的hostname發往producer、consumers以及其餘brokers。每臺節點設置成當前節點的IP地址
log.dirs=/tmp/kafka-logs  消息文件存儲的路徑,並非kafka系統日誌存放路徑,這裏不建議存放在/tmp目錄下,由於/tmp目錄會定時清理
zookeeper.connect=Host0:2181,Host1:2181,Host2:2181  指定鏈接Zookeeper的鏈接串,此處填寫上一節中安裝的三個zk節點的ip和端口便可

在各節點中啓動kafka

[root@Host0 kafka_2.10-0.8.2.0]# bin/kafka-server-start.sh config/server.properties 

建立topic

[root@Host0 kafka_2.10-0.8.2.0]# bin/kafka-topics.sh --create --zookeeper Host0:2181 --replication-factor 2 --partition 3 --topic my-replicated-topic1

模擬生產者

在任意一個節點上打開終端

[root@Host0 kafka_2.10-0.8.2.0]# bin/kafka-console-producer.sh --broker-list Host0:9092 --topic my-replicated-topic1
[2016-09-05 21:51:57,134] WARN Property topic is not valid (kafka.utils.VerifiableProperties)
hello kafka!

模擬消費者

在任意一個節點上打開終端

[root@Host0 kafka_2.10-0.8.2.0]# bin/kafka-console-consumer.sh --zookeeper Host2:2181 --topic my-replicated-topic1
hello kafka!


host.name和advertised.host.name會有坑,見如下,如下爲轉載。

此處的坑:

按 照官方文檔的說法,advertised.host.name和advertised.port這兩個參數用於定義集羣向Producer和 Consumer廣播的節點host和port,若是不定義的話,會默認使用host.name和port的定義。但在實際應用中,我發現若是不定義 advertised.host.name參數,使用Java客戶端從遠端鏈接集羣時,會發生鏈接超時,拋出異 常:org.apache.kafka.common.errors.TimeoutException: Batch Expired

通過debug發現,鏈接到集羣是成功的,但鏈接到集羣后更新回來的集羣meta信息倒是錯誤的: 可以看到,metadata中的Cluster信息,節點的hostname是iZ25wuzqk91Z這樣的一串數字,而不是實際的ip地址 10.0.0.100和101。iZ25wuzqk91Z實際上是遠端主機的hostname,這說明在沒有配置advertised.host.name 的狀況下,Kafka並無像官方文檔宣稱的那樣改成廣播咱們配置的host.name,而是廣播了主機配置的hostname。遠端的客戶端並無配置 hosts,因此天然是鏈接不上這個hostname的。要解決這一問題,把host.name和advertised.host.name都配置成絕對 的ip地址就能夠了。
相關文章
相關標籤/搜索