这个系列指南使用真实集群搭建环境,不是伪集群,用了三台腾讯云服务器
或者访问我的个人博客站点,链接
kafka依赖zookeeper,所以先确保集群已经安装zookeeper并且能够正常启动。
浪费了一整天的时间debug结果bug很简单(至少现在集群没有崩溃)
建立目录树 /opt/kafka/kafka2.12
在/root/kafka/kafka-logs/logs建立一个用于存放日志的文件
配置环境变量/etc/profile,添加bin目录
修改配置文件kafka/config/server.properties
1. 修改broker.id id和zookeeper的myid一致(应该是这样,这个bug我查了一天),每个主机的id都不一样,每次修改前,需要确认(或者干脆删除)kafka日志文件(/root/kafka/kafka-logs/metaxxxx中的id是否和broker.id一致)
2. 修改zookeeper.connect和zookeeper.connection.timeout.ms
zookeeper.connect=master:2181,slave1:2181,slave2:2181
zookeeper.connection.timeout.ms=6000
3. 修改logdir(记得必须先创建文件,kafka不会自己创建文件夹)
4. 修改两处listener(vim使用/listener查找),手动添加hostname(例如master,slave1,slave2),其实按照文档,只需要修改一处即可。
# 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://master: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://master: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=/root/kafka/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
############################# 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
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.
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
############################# 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=master:2181,slave1:2181,slave2:2181/kafka
# 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
delete.topic.enable=true
cd /${KAFKA_HOME}
bin/kafka-server-start.sh config/server.properties -daemon > /root/kafka/kafka-logs/logs &
指定日志的存放地点为/root/kafka/kafka-logs/logs
使用jps命令查看kafka是否配置成功。
起因:kafka启动总是异常(kafka进程启动一两分钟后自动退出),日志却没有记录
kafka架构图:
producer是数据源,比如flume架构,consumer是数据的输出,例如storm架构。
kafka服务器支持消息的分主题、分区。不同的子系统可以使用不同的主题。分区的意义在于负载均衡。
bin/kafka-topics.sh --create --zookeeper master:2181 --replication-factor 3 --partitions 1 --topic mytopics
创建的话题名称是有要求的Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
如果在zookeeper里指定了kafka的目录,例如/kafka,那么在用shell进行topic操作的时候,需要指定被操作的topic所属的zookeeper目录,例如bin/kafka-topics.sh –create –zookeeper master:2181 /kafka
–replication-factor 3 –partitions 1 –topic mytopics。(因为kafka的集群化是归zookeeper管的
)
列出当前话题
bin/ kafka-topics.sh --list --zookeeper master:2181,slave1:2181,slave2:2181
kafka-topics.sh --delete --zookeeper master:2181 --topic mytopics
注意由控制台的提示:Note: This will have no impact if delete.topic.enable is not set to true.
可知,需要修改一下server.properties文件,在最后一行加上delete.topic.enable=true
kafka-console-producer.sh --broker-list master:9092 --topic t_test
kafka-console-consumer.sh --zookeeper master:2181 --from-beginning --topic t_test
kafka-topics.sh --describe --zookeeper master:2181 --topic t_test
isr表示现在处于同步状态的broker,如果杀掉某一台服务器,例如杀掉leader:0的服务器,也就是0号服务器:master中的kafka进程。执行kill -9 pid
kafka会立即进行容灾处理,同时,生产和消费并不受影响。
再次恢复kafka进程,三台服务器又会立即同步。
推荐使用maven来构建项目,如果没有使用maven,导入kafka压缩包里的libs中的jar包即可
配置说明
添加完配置之后,producer就可以生产数据,使用producer.send()方法。传入的参数为topic,key,value。如果topic在kafka集群中还没有被创建,那么便会自动创建一个新的topic(新建的topic各个属性我不知道)
package cn.colony.cloudhadoop.kafka;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class ProducerDemo {
public static void main(String[] args) throws InterruptedException{
Properties props = new Properties();//配置项
props.put("bootstrap.servers", "master:9092,slave1:9092,slave2:9092");//使用新的API指定kafka集群位置
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
KafkaProducer producer = new KafkaProducer(props);
String messageStr = null;
for (int i = 1;i<1000;i++){
Thread.sleep(50);
messageStr = "hello, this is "+i+"th message";
producer.send(new ProducerRecord("t_topic","Message",messageStr));
}
producer.close();
}
}
配置说明
首先订阅一个topic,consumer就可以开始消费数据。
package cn.colony.cloudhadoop.kafka;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.errors.InterruptException;
import org.apache.kafka.common.serialization.StringDeserializer;
public class ConsumerDemo implements Runnable{
private final KafkaConsumer consumer;
private ConsumerRecords msgList;
private final String topic;
private static final String GROUDID = "groupA";
public ConsumerDemo(String topicName){
Properties props = new Properties();
props.put("bootstrap.servers", "master:9092,slave1:9092,slave2:9092");
props.put("group.id", GROUDID);
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("session.timeout.ms", "30000");
props.put("auto.offset.reset", "earliest");
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
this.consumer = new KafkaConsumer(props);
this.topic = topicName;
this.consumer.subscribe(Arrays.asList(topic));
}
@Override
public void run(){
int messageNum = 1;
try{
for (;;){
msgList = consumer.poll(500);
if (msgList!=null && msgList.count()>0){
for (ConsumerRecord record : msgList){
if (messageNum % 50 ==0){
System.out.println(messageNum+"=receive: key = " + record.key() + ", value = " + record.value()+" offset==="+record.offset());
}
if (messageNum % 1000 == 0)
break;
messageNum++;
}
}
else{
Thread.sleep(1000);
}
}
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
consumer.close();
}
}
public static void main(String[] args){
ConsumerDemo demo = new ConsumerDemo("t_topic");
Thread thread = new Thread(demo);
thread.start();
}
}
在eclipse中使用两个控制台查看输出,由于先前的配置,可以在本地通过代码来监测云服务器集群中的运行情况。生产者生产出的消息可以被消费者消费。
两个控制台分别对应不同Java程序输出的方法点这里