序言
安装kafka伪集群的初衷是我只想安装一个kafka测试可是安装一个启动起来会说找不到平衡broker,意思就是没有集群... 结果就想到配置给伪集群,其实和真实集群差别就是改改ip端口,可以查看zookepper配置->https://my.oschina.net/u/3730149/blog/3071737
-
下载和安装 win、linux、mac下载是一样的, 官网:http://kafka.apache.org/ 安装也是和zookeeper一样得选http网站下载,国外的网站有的无法访问,下载时候推荐不带src的,下载的文件小 例如:kafka_2.11-2.3.0.tgz (asc, sha512)
-
配置伪集群: 复制三份kafka文件分别命名为kafka0、kafka1、kafka2。修改下边config文件夹中server.properties文件 这是win的配置linux和mac配置需要修改路径。集群需要修改zookeeper的IP端口kafka的ip端口 我的kafka0中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. #当前机器在集群中的唯一标识,和zookeeper的myid性质一样 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 #这个是borker进行网络处理的线程数 num.network.threads=3 # The number of threads that the server uses for processing requests, which may include disk I/O #这个是borker进行I/O处理的线程数 num.io.threads=8 # The send buffer (SO_SNDBUF) used by the socket server #发送缓冲区buffer大小,数据不是一下子就发送的,先回存储到缓冲区了到达一定的大小后在发送,能提高性能 socket.send.buffer.bytes=102400 # The receive buffer (SO_RCVBUF) used by the socket server #kafka接收缓冲区大小,当数据到达一定大小后在序列化到磁盘 socket.receive.buffer.bytes=102400 # The maximum size of a request that the socket server will accept (protection against OOM) #这个参数是向kafka请求消息或者向kafka发送消息的请请求的最大数,这个值不能超过java的堆栈大小 socket.request.max.bytes=104857600 ############################# Log Basics ############################# # A comma separated list of directories under which to store log files #消息存放的目录,这个目录可以配置为“,”逗号分割的表达式,上面的num.io.threads要大于这个目录的个数这个目录,如果配置多个目录,新创建的topic他把消息持久化的地方是,当前以逗号分割的目录中,那个分区数最少就放那一个 log.dirs=D:/kafka0/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. #默认的分区数,一个topic默认1个分区数 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 #默认消息的最大持久化时间,168小时,7天 log.retention.hours=168 # 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. #这个参数是:因为kafka的消息是以追加的形式落地到文件,当超过这个值的时候,kafka会新起一个文件 log.segment.bytes=1073741824 # The interval at which log segments are checked to see if they can be deleted according # to the retention policies #每隔300000毫秒去检查上面配置的log失效时间(log.retention.hours=168 ),到目录查看是否有过期的消息如果有,删除 log.retention.check.interval.ms=300000 ############################# 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的连接端口 zookeeper.connect=127.0.0.1:4180,127.0.0.1:4280,127.0.0.1:4380 # 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. #当前kafka对外提供服务的端口默认是9092 port=9092 ##这个参数默认是关闭的,在0.8.1有个bug,DNS解析问题,失败率的问题。 #host.name=127.0.0.1 #advertised.host.name=127.0.0.1 # 该服务监听的端口 #advertised.listeners=PLAINTEXT://127.0.0.1:9092 listeners=PLAINTEXT://:9092 #delete.topic.enable=true #message.max.byte=5242880 #消息保存的最大值5M #default.replication.factor=2 #kafka保存消息的副本数,如果一个副本失效了,另一个还可以继续提供服务 #replica.fetch.max.bytes=5242880 #取消息的最大直接数 #log.cleaner.enable=false #是否启用log压缩,一般不用启用,启用的话可以提高性能
-
kafka1和2文件中的server.properties文件只需要
修改broker.id=0为broker.id=1和broker.id=2 log.dirs=D:/kafka0/logs为log.dirs=D:/kafka1/logs和log.dirs=D:/kafka2/logs port=9092为port=9093和port=9094 listeners=PLAINTEXT://:9092为listeners=PLAINTEXT://:9093和listeners=PLAINTEXT://:9094
-
-
验证Kafka集群正常工作
3.0 启动 kafka
这是windows mac和linux改为正斜杠,和.sh文件 .\bin\windows\kafka-server-start.bat .\config\server.properties
3.1 创建一个Topic,其中的xxx、yyy、zzz是Zookeeper的地址,形式为IP:PORT
bin/kafka-topics.sh --create --zookeeper xxx,yyy,zzz --replication-factor 3 --partitions 1 --topic my-replicated-topic
3.2 我们现在已经搭建了一个Kafka集群,并创建了一个Topic,但我们如何知道每个broker在做什么呢?可使用describe topics 命令:
bin/kafka-topics.sh --describe --zookeeper xxx,yyy,zzz --topic my-replicated-topic Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs: Topic: my-replicated-topic Partition: 0 Leader: 1 Replicas: 1,3,2 Isr: 1,3,2
-
其中,第一行是对所有partition的摘要,每个附加行给出了每个partition的详情,这里只有一个partition,所以只有一行。 leader是负责给定partition所有读写的节点,每个节点将称为partition随机选择部分的leader。 replicas是复制此partition日志的节点列表,无论它们是否leader,即使它们处于活动状态。 isr是一组in-sync 副本,这是replicas列表的子集,这些副本当前活动,并被引导到leader。
笔者按:isr常被翻译成“副本同步队列”。 leader、replicas、isr可参考:https://www.cnblogs.com/mengyou0304/p/4836555.html
3.3 创建一个生产者:
bin/kafka-console-producer.sh --broker-list kafka集群地址列表 --topic my-replicated-topic 地址为IP:PORT的形式,多个用逗号分隔
另启一个窗口,创建消费者:
bin/kafka-console-consumer.sh --bootstrap-server kafka地址 --from-beginning --topic my-replicated-topic
kafka地址为IP:PORT的形式,为Kafka集群中的任意一个节点。
在生产者窗口输入任意文字,看在消费者窗口能否接收。如果能正常接收,说明Kafka集群搭建成功。
-