这里启动了三台虚拟机上的zookeeper,可以通过zkServer.sh status来查看zookeeper运行状态
# debian eversilver@debian:/usr/local/zookeeper$ ./bin/zkServer.sh status /usr/local/jdk1.8/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower #debian2 eversilver@debian:/usr/local/zookeeper$ ./bin/zkServer.sh status /usr/local/jdk1.8/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader #debian3 eversilver@debian:/usr/local/zookeeper$ ./bin/zkServer.sh status /usr/local/jdk1.8/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower上面三台机器中,debian2机器被选作了leader
leader主要作用是从客户端接受并且响应请求
follower的主要作用:从leader同步数据,再leader关闭时,进行投票选举出集群中的新的leader
集群搭建中的几个重要的文件分别为:
myid文件:用于zookeeper机器间互相发现彼此
zoo.cfg:集群配置文件
log4j.properties文件:zk集群的日志输出文件,同样在conf目录下
zkEnv.sh以及zkServer.sh:分别用于启动环境配置以及集群启动
注意,/bin目录下的zkCleanUp.sh脚本可以快速的对zookeeper生成的日志进行清理,这里使用crontab定期执行其来进行清理。
crontab -l来查看定时任务是否存在,crontab -e对其进行编辑。这里编辑如下。其中主要需要进行配置的配置项有broker.id,port,hostname,log.dirs,numpartitions, message.max.bytes,default,replication.factor,replica.fetch.max.bytes以及zookeeper.connect
解压缩完成kafka后,打开配置文件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 # # # # 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. # 类似zookeeper的myid字段 broker.id=1 # Switch to enable topic deletion or not, default value is false #delete.topic.enable=true ############################# 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 = security_protocol://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 port=19092 host.name=192.168.128.128 # The number of threads handling network requests num.network.threads=3 # The number of threads doing disk I/O # 设置成多个的时候一般下面的logDir也设置成多个,这样一个线程处理一个目录,性能会好很多 # 注意下面的多个目录往往以逗号来分割 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) # 这个数不能超过java的堆栈大小 socket.request.max.bytes=104857600 ############################# Log Basics ############################# # A comma seperated list of directories under which to store log files log.dirs=/home/eversilver/silverTest/kafka/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. # topic分区数 num.partitions=2 # 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 # 消息失效期,7天 log.retention.hours=168 # kafka每条消息存放的最大大小 message.max.byte=5048576 #kafka集群保存消息的默认份数(副本数) default.replication.factor=2 # 取消息的最大字节数,设置为5M replica.fetch.max.bytes=5048576 # 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 # 每隔这么多毫秒查看是否有失效的消息,(上面是168小时)。有的话就删除消息 log.retention.check.interval.ms=300000 # 是否启用log压缩 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=192.168.128.128:2181,192.168.128.129:2181,192.168.128.130:2181 # Timeout in ms for connecting to zookeeper zookeeper.connection.timeout.ms=6000